PHP代码从MySQL数据库中删除数据

时间:2013-06-06 17:50:27

标签: php

您好我正在创建一个电影系统,您可以添加编辑和删除电影。我有一个网页,它显示来自MySQL数据库的所有数据和一个edit.php,您输入电影ID并更改值,当您点击更新时,它会从mysql数据库中删除数据(除了ID之外的所有数据)。这是代码:

<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>

我还问了另一个问题,帮助我获取edit.php的代码: Here

1 个答案:

答案 0 :(得分:6)

输入名称与您使用$ _POST检索的名称不匹配。

<input name="moviename" type="text" id="name">

不匹配:

$moviename = $_POST['name']; 

它应该是:

$moviename = $_POST['moviename']; 

id两个地方都匹配,这就是更新成功的原因。