php从表单更新sql

时间:2013-04-08 10:53:54

标签: php mysql

我有一个PHP脚本,我正在努力工作,我基本上只是从教程中提取并根据我的需要进行了修改。这是我第一次尝试使用php,所以请放轻松。

我有3个文件

  1. list_records.php
  2. update.php
  3. update_ac.php
  4. List_records从mysql中的表中读取数据。 list_records中的表有一个编辑功能,可以将你带到update.php,它在db表中显示数据。

    Update.php有一个提交按钮,用于使用update_ac.php更新mysql,使用$ _GET ['id]使用url中的id字段更改了所有信息。

    我知道这个脚本对slq注射非常开放,但我打算只在本地环境中使用它,它不会暴露在互联网上,只有我自己和另外一个人会使用这个页面所以它不是真的一个问题。

    无论如何,我已经确认了几件事: -

    1. 使用$ _Get获取id,我输入了一个echo,并将其打印在update.php页面上。
    2. 我可以在php中运行更新命令并更改值,但在使用$ _GET [id]
    3. 时它不会工作

      有人能指出我正确的方向吗?

      以下是更改了数据库连接详细信息的3个文件

      list_records.php

      <title>Ports</title>
      </head>
      
      <?php
      
      // Connect to server and select database.
      mysql_connect("localhost", "username", "passsword")or die("cannot connect"); 
      mysql_select_db("porting")or die("cannot select DB");
      
      
      $sql="SELECT * FROM ports";
      $result=mysql_query($sql);
      
      ?>
      <body>
      
      
      <table width="1200" border="1" cellspacing="1" cellpadding="0">
      <tr>
      <td>
      <table width="1200" border="1" cellspacing="1" cellpadding="3">
      <tr>
      <td colspan="50"><strong>Pending Port Requests 2</strong> </td>
      </tr>
      
      <tr>
      <td align="center"><strong>Customer</strong></td>
      <td align="center"><strong>Number</strong></td>
      <td align="center"><strong>Type</strong></td>
      <td align="center"><strong>Completed</strong></td>
      <td align="center"><strong>Update</strong></td>
      </tr>
      
      <?php
      while($rows=mysql_fetch_array($result)){
      ?>
      <tr>
      <td><?php echo $rows['Customer']; ?></td>
      <td><?php echo $rows['Number']; ?></td>
      <td><?php echo $rows['Type']; ?></td>
      <td><?php echo $rows['Completed']; ?></td> 
      <td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
      </tr>
      
      <?php
      }
      ?>
      
      </table>
      </td>
      </tr>
      </table>
      </body>
      </html>
      

      update.php

      <title>update</title>
      </head>
      
      <?php
      // Connect to server and select database.
      mysql_connect("localhost", "username", "password")or die("cannot connect"); 
      mysql_select_db("porting") or die("cannot select DB");
      
      // get value of id that sent from address bar
      $id=$_GET['id'];
      
      
      
      // Retrieve data from database 
      $sql="SELECT * FROM porting.ports WHERE id = '$id'";
      $result=mysql_query($sql);
      $rows=mysql_fetch_array($result);
      ?>
      <body>
      
      
      <table width="1200" border="0" cellspacing="1" cellpadding="0">
      <tr>
      <form name="form1" method="post" action="update_ac.php">
      <td>
      <table width="100%" border="0" cellspacing="1" cellpadding="0">
      <tr>
      <td>&nbsp;</td>
      <td colspan="6"><strong>Update Porting Details</strong> </td>
      </tr>
      <tr>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      </tr>
      <tr>
      <td align="center">&nbsp;</td>
      <td align="center"><strong>Customer</strong></td>
      <td align="center"><strong>Number</strong></td>
      <td align="center"><strong>Type</strong></td>
      <td align="center"><strong>Completed</strong></td>
      </tr>
      <tr>
      <td>&nbsp;</td>
      <td align="center">
      <input name="Customer" type="text" id="Customer" value="<?php echo $rows['Customer']; ?>"size= "15"/>
      </td>
      <td align="center">
      <input name="Number" type="text" id="Number" value="<?php echo $rows['Number']; ?>" size="15"/>
      </td>
      <td align="center">
      <input name="Type" type="text" id="Type" value="<?php echo $rows['Type']; ?>" size="15"/>
      </td>
      <td align="center">
      <input name="Comments" type="text" id="Completed" value="<?php echo $rows['Comments']; ?>" size="15"/>
      </td>
      <tr>
      </table>
      <input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"/>
      <input type="submit" name="Submit" value="Submit" /></td>
      <td align="center">&nbsp;</td>
      </td>
      </form>
      </tr>
      </table>
      </body>
      </html>
      

      update_ac.php

      <?php
      // Connect to server and select database.
      mysql_connect("localhost", "username", "password")or die("cannot connect"); 
      mysql_select_db("porting")or die("cannot select DB");
      
      // update data in mysql database 
      $sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" or die ("this stuffed up");
      $result=mysql_query($sql) or die ("this stuffedup");
      
      
      // if successfully updated. 
      if($result){
      echo "Successful";
      echo "<BR>";
      echo "<a href='list_records.php'>View result</a>";
      }
      
      else {
      echo "ERROR";
      }
      
      ?>
      

3 个答案:

答案 0 :(得分:8)

您的更新查询应为

// update data in mysql database 
$sql="UPDATE ports SET Customer='".$_POST['Customer']."', Number='".$_POST['Number']."' WHERE id='".$_POST['id']."'";

$result=mysql_query($sql)or 
die ("this stuffedup");

答案 1 :(得分:1)

1.You have to pass a id when clicking a submit in update.php by
<a href="update_ac.php?id=<?php echo $rows['id']; ?>"><input type="submit" name="submit" value="Submit"></a>.

2.The line $id=$_GET['id'] is used in update_ac.php before insert query.

答案 2 :(得分:0)

$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" ;

这一行是错误的,你用STRING而不是整数来更新它。你应该把

$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='".intval($_REQUEST['id'])."'"