The scoreboard has not been showing up in the rightHighScore div. It's just not showing anything. I'm making a high score board for my javascript snake game. So is there anything wrong with my MYSQL code here?
<html>
<link href='style.css' type='text/css' rel='stylesheet'>
<!-- Lets make a simple snake game -->
<canvas id="canvas" width="450" height="450" style="border:1px solid #000000;"></canvas>
<!-- Jquery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script>
<script src="snake.js" type="text/javascript"></script>
<?php $score = "<script>document.write(score)</script>"?>
<p id = 'demo'>Your Final Score: <script>score</script></p>
<div id = 'right'>
<div id = 'rightForm'>
<form action="highscoreregistry.php" method="post">
<input type="hidden" name="score" id="score">
</form>
</div>
<div id = 'rightHighScore' style = 'size:9px;'>
<?php
$servername = "-";
$username = "-";
$password = "-";
$dbname = "-";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Name, Score, Date FROM snakehiscore";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["Name"]."</td><td>".$row["Score"]." ".$row["Date"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</html>
Add a semicolon to the query! So:
$sql = "SELECT Name, Score, Date FROM snakehiscore;";
2.Instead of using this:
$result = $conn->query($sql);
try this if you have mysqli installed:
$result = mysqli_query($conn, $sql);
mysqli_num_rows($result) > 0
Related
When I try to load this above php page from my login page (index.php), there is no error but when I try to load it from any other page in my application after login, it gives me an uncaught syntax error on the console. It points to the script and says that the php variable $name is not defined.
<?php
if (isset($_POST["login"])) {
session_start();
$_SESSION["email"] = $_POST["email"];
$email = $_POST["email"];
$password = $_POST["password"];
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'attendance_system';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Failed to connect: " . $conn->connect_error);
} else {
$success = false;
$query = "SELECT * FROM student";
$result = $conn->query($query);
while ($row = $result->fetch_assoc()) {
if ($row["email"] === $email && $row["password"] === $password) {
$name = $row["name"];
$age = $row["age"];
$criteria = $row["attendance_criteria"];
$course = $row["course"];
$college = $row["college_name"];
$attendance = $row["attendance"];
$success = true;
break;
}
}
if ($success === false) {
header("Location: index.php");
}
}
}
if (isset($_POST["signup"])) {
session_start();
$_SESSION["email"] = $_POST["email1"];
$email = $_POST["email1"];
$password = $_POST["password1"];
$name = $_POST["name"];
$age = $_POST["age"];
$criteria = 100;
$course = null;
$college = null;
$attendance = 0;
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'attendance_system';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Failed to connect: " . $conn->connect_error);
} else {
$query = "INSERT INTO student SET email = '$email', name = '$name', age = '$age', password =
'$password' ";
$result = $conn->query($query);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>Attendance Manager - Home</title>
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/home.css">
<link rel="icon" type="image/jpg" href="../media/icon.png">
</head>
<body>
<header>
<div class="left">
<button id="menuButton" onclick="openMenu()">≡</button>
</div>
<div class="center">
<div>
<div id="month">
</div>
<div id="year">
</div>
</div>
<div id="day">
</div>
<img id="icon" src="../media/icon.png" alt="App Icon" width="48" height="48">
<div id="name">
</div>
</div>
<div class="right">
<a id="info" href="../html/index.php">l</a>
</div>
</header>
<main>
<menu>
Home
Subjects
Edit Attendance Criteria
How to Use
Developer Information
Privacy Policy
Contact Us
</menu>
</main>
<section>
<div>
Name :
<span id="user_name"></span>
</div>
<div>
Email :
<span id="user_email"></span>
</div>
<div>
Age :
<span id="user_age"></span>
</div>
<div>
Password :
<span id="user_password"></span>
</div>
</section>
<footer>
<div class="left">
Attendance Manager<br>2020 ©
</div>
<div class="center">
<span>
Official Page -
</span>
<span>
<a href="https://twitter.com/AlokPur32580593?s=08">
<img src="../media/twitter.png" alt="Twitter Icon" height="48px" width="48px">
</a>
</span>
</div>
<div class="right">
<a id="privacyPolicy" href="privacy.html">Privacy Policy</a>
</div>
</footer>
<script type="text/javascript">
if (document.referrer === "http://localhost/AttendanceManager/html/index.php") {
var name = "<?= $name ?>";
var age = "<?= $age ?>";
var email = "<?= $email ?>";
var pasword = "<?= $password ?>";
var criteria = "<?= $criteria ?>";
var course = "<?= $course ?>";
var college = "<?= $college ?>";
var attendance = "<?= $attendance ?>";
localStorage.setItem("name", name);
localStorage.setItem("age", age);
localStorage.setItem("email", email);
localStorage.setItem("password", pasword);
localStorage.setItem("criteria", criteria);
localStorage.setItem("course", course);
localStorage.setItem("college", college);
localStorage.setItem("attendance", attendance);
}
</script>
<script type="text/javascript" src="../javascript/home.js"></script>
</body>
</html>
If your footer <script> always runs and always expects $name to be filled, then you'll have to check whether the session was logged in. If it isn't, redirect the user to the login page, or specify defaults.
Also, you should probably rewrite the SQL you're using to authenticate the user. First, save the password as a hash. Second, use a WHERE statement to fetch any rows that match the email and password, instead of looping through the results in your application.
You probably also want to fix the var pasword typo, and I advise you to rename the $success variable to something more descriptive, like $loggedIn. This might not be critical for this particular purpose, but it's a good thing to get into the habit of properly naming your variables.
I'm having issue in adding multiple rows of data into database for the form name courseoutlineimage are attached here. I have tried use array statement but it still not support my code and i couldn't find where is the mistake. Please help me to correct my code.
Script Code:
<script>
$('document').ready(function(){
$('#btn').click(function(e){
e.preventDefault();
$('.apsection').append('Course Outline: <input type="text" style="width: 800px;" name="courseoutline[]" class="form-contro" placeholder="Enter your Course Outline"><br>');
});
});
</script>
Form Code:
<div class="tab"><h5>Section 3: Course Content Outline</h5>
<div class="apsection">
Course Outline : <input type="text" style="width: 800px;" name="courseoutline[]" class="form-contro" placeholder="Enter your Course Outline"><br>
</div>
<button id="btn" class="btn btn-warning">Add More</button>
</div>
Database Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "adminpanel";
//connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("connection failure " . mysqli_connect_error());
}
$count = count($_POST['courseoutline']);
for ($i=0; $i <$count ; $i++) {
mysqli_query($sqlres);
$sqlres = "INSERT INTO course_content (courseoutline, coursecode) VALUES ('{$_POST['courseoutline'][$i]}','$coursecode')";
}
mysqli_close($conn);
?>
Use foreach instead of.
Database Code:
foreach($_POST['courseoutline'] as $value) {
$sqlres = "INSERT INTO course_content (courseoutline, coursecode) VALUES ($value,'$coursecode')";
}
Whenever I enter an id number in text box and click scan button, i get data from database and it is displayed below the text box in a predefined format. Now when i enter another id and hit scan, the new data thus retreived replaces the older one. I want it to be displayed below the already existing data on the page.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title> ShopNGo </title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header id="header">
<div class="container">
<form name="products" method="POST">
<br><br>
<button type="submit" name="scan" id="scan"> <h1> SCAN! </h1> </button>
<br><br><br>
<input type="text" name="id">
</form>
</div>
</header>
<div class="main">
<table border="0">
<?php
if (isset($_POST["scan"])) {
$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "#";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection Failed:" . mysqli_connect_error());
$query = "SELECT name, price, img FROM product WHERE id = $_POST[id]";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<tr> <table border='0'> <tr>";
echo "<img src='$row[img]'>";
echo "<br>";
echo $row["name"];
echo "<br>";
echo $row["price"];
echo "</tr> </table> </tr>";
}
}
mysqli_close($conn); }
?>
</table>
</div>
</body>
</html>
please help me !!
also it would be a great help if u can suggest some improvements in the existing code other than what i asked for... Thank you so much !!
use ajax to post your id to another php script that checks id for data, put the html into a string variable and then echo it at the end. You can use the ajax success callback to append the data
$.ajax({
type: "POST",
url: url,
data: id,
success: function(data){
$('#targetDiv').append(data);
}
});
php script:
$query = "SELECT name, price, img FROM product WHERE id = $_POST[id]";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0)
{
while($row =
mysqli_fetch_assoc($result))
{
$element = "<tr> <table border='0'> <tr>";
$element .= "<img src='$row[img]'>";
$element .= "<br>";
$element .=$row["name"];
$element .= "<br>";
$element .= $row["price"];
$element .= "</tr> </table> </tr>";
}
}
echo $element;
How would I code into my program using PHP/JavaScript and HTML/CSS to display data from a database I made in MySQL Monitor on the blue section below:
I made buttons that use PHP to go into the database and show the data on the HTML page:
HTML:
<form action="fullridez.php" method="post">
<h4 id="Filter">GPA</h4>
<input id="FilterBox" name="gpa" type="text"/>
<h4 id="Filter">Amount</h4>
<input id="FilterBox" name="amount" type="text"/>
<h4 id="Filter">School</h4>
<input id="FilterBox" name="school" type="text"/>
<input type="submit" id="FilterBox" name="myForm" onkeypress="checkEnter()" ><img src="search.png" width=15 height=15 /></button>
</form>
<script>
</script>
PHP:
<?php
if(isset($_POST['myForm'])) {
$servername = "localhost";
$username = "root";
$password = "";
$database = "scholarshiplist";
$conn = mysqli_connect($servername, $username, $password, $database);
$gpa = $_POST['gpa'];
$amount = $_POST['amount'];
$count = "SELECT * FROM scholarships";
$result = mysqli_query($conn, $count);
if ($result->num_rows > 0) {
$sql = "SELECT * FROM scholarships WHERE GPA <= " . $gpa . " AND Amount <= "
. $amount;
if ($result = mysqli_query($conn, $sql)) {
while ($row=mysqli_fetch_row($result)) {
for($i = 0; $i < count($row); $i++) {
echo $row[$i] . '<br>';
}
}
}
} else {
echo "0 results";
}
$conn->close();
}
SQL:
USE ScholarshipList;
CREATE TABLE Scholarships
(
id int unsigned NOT NULL auto_increment,
School varchar(500) NOT NULL,
GPA decimal(10,2) NOT NULL,
Amount decimal(10,2) NOT NULL,
PRIMARY KEY (id)
);
I am using XAMPP
When I click the button on the HTML file it bring me to the PHP page and all I see is the PHP code. I don't want it to go to the page but stay on the same page showing the data below the buttons.
This is what the page looks like so far
page
What am I doing wrong?
If your HTML form is contained within the 'fullridez.php' file and you are posting the form inputs to that same file, then you need to have some PHP where you'd like to output to be checking for results and then looping through those results while echoing them out:
<table>
<tr><td>Col 1</td><td>Col 2</td><td>Col 3</td></tr>
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<tr><td>"
. $row['col_1'] . "</td><td>"
. $row['col_2'] . "</td><td>"
. $row['col_3'] . "</td></tr>";
}
?>
</table>
You can build a wireframe div table with for loop:
<?php
$num_rows = mysql_num_rows($result);
for ($i=0;$i<$num_rows;$i++) {
//loop through all rows of data
$row = mysql_fetch_assoc($result); // your data is now: $row['fieldName']
?>
<div>
GPA <input name="" value="<?php echo($row['gpa'])?>;" type="text">
AMOUNT <input name="" value="<?php echo($row['amount'])?>;" type="text">
SCHOOL <input name="" value="<?php echo($row['school'])?>;" type="text">
</div>
<?php
} //end of the loop
?>
I've written the code below to set a variable in mysql to either 1 or 0. But somehow whenever i click the first button (1) it's always saving the 0-value assigned to the second button in the mysql table.
<head>
<?php
function update_ziekenwagen($Status) {
$servername = "localhost";
$username = "webapp";
$password = "password";
$dbname = "spoed";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE AlgemeneVars SET value='".$Status."' WHERE id=4";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully:" . $Status;
//echo "UPDATE AlgemeneVars SET value=' " . $Status . " ' WHERE id=4";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();}
?>
</head>
<body>
<input type="button" value="Vertr" id="Vertr" name="Vertr" onclick="document.write('<?php update_ziekenwagen(1); ?>');" />
<input type="button" value="Terug" id="Terug" name="Terug" onclick="document.write('<?php update_ziekenwagen(0); ?>');" />
</body>
<head>
<?php
function update_ziekenwagen() {
$Status = $_POST['status'];
$servername = "localhost";
$username = "webapp";
$password = "sW7HwM225PxrwbZC";
$dbname = "spoed";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE AlgemeneVars SET value='".$Status."' WHERE id=4";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully:" . $Status;
//echo "UPDATE AlgemeneVars SET value=' " . $Status . " ' WHERE id=4";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
if (!empty($_POST)){
update_ziekenwagen();
}
?>
</head>
<body>
<form>
<input type="hidden" name="status" value="1" />
<input type="submit" />
</form>
<form>
<input type="hidden" name="status" value="0" />
<input type="submit" />
</form>
</body>
You can still make it work without using Ajax though using the above code. But this is NOT safe or good practice at all.