Reputation: 17
I am writing out a form in php,html... witch should normally update to my database but the form isnt showing up on the screen. Help!!! I am not a very expirianced coder so pleases if you could tell meif there is any other probleme with my code. Thanks :-)
<html>
<head>
<title>help</title>
</head>
<body>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = '*********';
$dbpass = '*****';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
$Userid = $_POST['UserID'];
$TableID = $_POST['tableID'];
$Life_points = $_POST['Life_points'];
$xp_points = $_POST['xp_points'];
$sql = "UPDATE points SET TableID = " . $TableID . " WHERE UserID = ". $Userid . " AND life_points = " . $Life_points . " AND xp_points= " . $xp_points;
mysqli_select_db('womath');
$retval = mysqli_query( $conn, $sql );
<?php echo $_SERVER['PHP_SERVER'] ?>
if(! $retval ) {
die('Could not update data: ' . mysqli_error());
}
echo "Updated data successfully\n";
mysqli_close($conn);
} else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border =" 0" cellspacing = "1" cellpadding = "2">
<tr>
<td width = "100">UserID</td>
<td><input name = "UserID" type = "number" id = "UserID"></td>
</tr>
<tr>
<td width = "100">TableID</td>
<td><input name = "TableID" type = "number" id = "TableID"></td>
</tr>
<tr>
<td width = "100">life_points</td>
<td><input name = "life_points" type = "number" id = "life_points"></td>
</tr>
<tr>
<td width = "100">xp_points</td>
<td><input name = "xp_points" type = "number" id = "xp_points"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td><input name = "update" type = "submit" id = "update" value = "Update"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
Upvotes: 0
Views: 111
Reputation: 94642
You have issued a query to the database for compilation and execution before you have told MYSQL which database you are trying to gain access to.
The mysqli_select_db('womath');
must happen before your first query
In fact that function is more for use when you want to switch from one database to another during a scripts execution, you can add the database name to the
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
like this
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,'womath');
line if you like and forget about the line
mysqli_select_db('womath');
completely.
<html>
<head>
<title>help</title>
</head>
<body>
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = '*********';
$dbpass = '*****';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,'womath');
// --------------------------^^^^^^^^
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
// move this calll to here or add database to the connect line
//mysqli_select_db('womath');
$Userid = $_POST['UserID'];
$TableID = $_POST['tableID'];
$Life_points = $_POST['Life_points'];
$xp_points = $_POST['xp_points'];
$sql = "UPDATE points SET TableID = '$TableID'
WHERE UserID = '$Userid'
AND life_points = '$Life_points'
AND xp_points = '$xp_points'";
$retval = mysqli_query( $conn, $sql );
// this following line also need a `;`
echo $_SERVER['PHP_SERVER'];
if(! $retval ) {
die('Could not update data: ' . mysqli_error());
}
echo "Updated data successfully\n";
mysqli_close($conn);
// remove this else, as with it in place
// you only show the form when you are NOT updating the database
// }else {
// just terminate the IF so the form will show after an update
// AND when page is first loaded and there is no user input
}
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border =" 0" cellspacing = "1"
cellpadding = "2">
<tr>
<td width = "100">UserID</td>
<td><input name = "UserID" type = "number"
id = "UserID"></td>
</tr>
<tr>
<td width = "100">TableID</td>
<td><input name = "TableID" type = "number" id = "TableID"></td>
</tr>
<tr>
<td width = "100">life_points</td>
<td><input name = "life_points" type = "number" id = "life_points"></td>
</tr>
<tr>
<td width = "100">xp_points</td>
<td><input name = "xp_points" type = "number" id = "xp_points"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "update" type = "submit" id = "update" value = "Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements
Upvotes: 3