为什么edit.php不工作?

时间:2015-10-25 16:50:15

标签: php mysql

我上传到MySQL的编辑数据有问题。

这是我的edit.php

  <html>
  <head>
  </head>

 <body>
 <table border=1>
 <tr>
  <td align=center>Form Edit Employees Data</td>
 </tr>
 <tr>
  <td>
   <table>
   <?
   include "dbconfig.php";//database connection
   error_reporting(E_ALL); 

   $query = "SELECT * FROM users where id='$id'";
   $result = mysql_query($query);
   $row = mysql_fetch_array($result);
   ?>
  <form method="post" action="updatepengguna.php">
  <input type="hidden" name="id" value="<?php echo "$row[id]"?>">
    <tr>        
      <td>Username</td>
      <td>
        <input type="text" name="username" 
    size="20" value="<?php echo "$row[username]"?>">
      </td>
    </tr>
    <tr>
      <td>Nama Lengkap</td>
      <td>
        <input type="text" name="namalengkap" size="40" 
      value="<?php echo "$row[namalengkap]"?>">
      </td>
    </tr>
    <tr>        
      <td>NIK</td>
      <td>
        <input type="text" name="nik" 
    size="20" value="<?php echo "$row[nik]"?>">
      </td>
    </tr>
    <tr>        
      <td>Password</td>
      <td>
        <input type="text" name="password" 
    size="20" value="<?php echo "$row[password]"?>">
      </td>
    </tr>
    <tr>        
      <td>Level</td>
      <td>
        <input type="text" name="level" 
    size="20" value="<?php echo "$row[level]"?>">
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" 
      name="submit value" value="Edit">
      </td>
    </tr>
  </form>
  </table>
 </td>
 </tr>
 </table>
 </body>
 </html>

我在这里有updatepengguna.php

 <?

 include "dbconfig.php";
 $id = $_POST['id'];
 $username = $_POST["username"];
 $namalengkap = $_POST["namalengkap"];
 $nik= $_POST["nik"];
 $password= $_POST["password"];
 $level= $_POST["level"];
 $query = "UPDATE users SET username='$username',  namalengkap='$namalengkap', nik='$nik', password='$password', level='$level' WHERE id='$id'";
 mysql_query($query);
 echo mysql_error();
 header("location:daftarpengguna.php");
 ?>

我收到此错误:

  

注意:未定义的变量:第20行的C:\ xampp \ htdocs \ aes \ editpengguna.php中的id

有什么问题?

1 个答案:

答案 0 :(得分:1)

好的,让我们通过代码和您的错误消息,你有20行代码:

  <html>
  <head>
  </head>

 <body>
 <table border=1>
 <tr>
  <td align=center>Form Edit Employees Data</td>
 </tr>
 <tr>
  <td>
   <table>
   <?
   include "dbconfig.php";//database connection
   error_reporting(E_ALL); 

   $query = "SELECT * FROM users where id='$id'";

我认为include "dbconfig.php";有三行代码。

第20行是

$query = "SELECT * FROM users where id='$id'";

所以你的错误信息

  

注意:未定义的变量:第20行的C:\ xampp \ htdocs \ aes \ editpengguna.php中的id

告诉您$id未定义。如果你看看之前的20行代码,那就不是。

也许你打算这样做

$id = (int)$_GET['id'];
你的代码前面的

?可能POST但这是错误。

您还应该考虑使用PDOmysqli作为您的驱动程序。 mysql_函数已弃用,不支持准备/参数化查询。您可以使用http://php.net/manual/en/function.mysql-real-escape-string.php。由于您正在学习,我建议您使用实时和最新的驱动程序进行学习。