PHP代码从MySQL数据库中删除数据[关闭]
问题描述:
Hi I am creating a movies system that you can add edit and delete movies from. I have a webpage a that displays all data from the MySQL database and a edit.php which you enter the movie ID and change the values, thing is when you click Update it removes the data from the mysql database(all but the ID). Here is the code:
<html>
<head></head>
<body>
<?php
session_start();
error_reporting(error_reporting() & ~E_NOTICE);
$username = $_SESSION['username'];
if ($username)
{
echo "<html><center><font face='arial'><p align=right>Welcome <a href='profile.php'>$username</a>. <a href='login/logout.php'>Logout.</a></p align>";
}
else
{
echo "<html><center><font face='arial'><p align=right><a href=login/index.php>Login</a> or <a href=login/registration/register.php>Sign up!</a></p align>";
}
echo "<a href='index.php'>Home</a> | <a href='movie-add.php'>Add a movie</a> | <a href='movies.php'>Movies list</a><center></html></font face='arial'>";
echo "<html><br></html>";
echo "<html><br></html>";
echo "<html><br></html>";
echo "<html><center><font face=arial><b>If you made a mistake here you can change the information you entered. </center></b><font face=arial></html>";
echo "<html><br></html>";
echo "<html><br></html>";
if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if(! $conn)
{
die('Something went wrong show this to dylan:'.mysql_error());
}
$id = $_POST['id'];
$moviename = $_POST['name'];
$movieage = $_POST['age'];
$moviedate = $_POST['date'];
$moviedescrip = $_POST['description'];
$sql = "UPDATE movies " .
"SET name = '$moviename', " .
"age = '$movieage', " .
"date = '$moviedate', " .
"description = '$moviedescrip' " .
"WHERE id = $id";
mysql_select_db('movie_system');
$retval = mysql_query( $sql, $conn);
if(! $retval )
{
die('Sorry, could not update the data show this to Dylan: '.mysql_error());
}
echo "The values have been changed!";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>Movie ID:</td>
<td><input name="id" type="text" id="id"></td>
</tr>
<tr>
<td>Movie name:</td>
<td><input name="moviename" type="text" id="name"></td>
</tr>
<tr>
<td>Movie age rating:</td>
<td><input name="movieage" type="text" id="age"></td>
</tr>
<tr>
<td>Date avilable on DVD:</td>
<td><input name="moviedate" type="text" id="date"></td>
</tr>
<tr>
<td>Description from back of case:</td>
<td><input name="moviedescrip" type="text" id="description"></td>
</tr>
<tr>
<td></td>
<td><input name="update" type="submit" id="update" value="Update"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
I also asked another question helping me get the code for edit.php: Here
答
The input names don't match what you're retrieving with $_POST.
<input name="moviename" type="text" id="name">
doesn't match:
$moviename = $_POST['name'];
it should be:
$moviename = $_POST['moviename'];
id DOES match in both places, which is why the update is successful.