Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从表单更新sql_Php_Mysql - Fatal编程技术网

php从表单更新sql

php从表单更新sql,php,mysql,Php,Mysql,我有一个php脚本,我正在努力工作,我基本上只是从一个教程中提取,并根据我的需要进行修改。这是我第一次尝试php,所以请对我放松点 我有3个文件 list_records.php update.php 更新_ac.php List_记录从mysql中的表中读取数据。list_records中的表有一个编辑函数,它可以将您带到update.php,在那里它会在db table中显示数据 Update.php有一个submit按钮,用于使用Update_ac.php更新mysql,其中包含您使用$\

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

我有3个文件

  • list_records.php
  • update.php
  • 更新_ac.php
  • List_记录从mysql中的表中读取数据。list_records中的表有一个编辑函数,它可以将您带到update.php,在那里它会在db table中显示数据

    Update.php有一个submit按钮,用于使用Update_ac.php更新mysql,其中包含您使用$\u GET['id]在url中的id字段更改的任何信息

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

    无论如何,我已经证实了几件事:-

  • id确实是使用$\u get获取的,我输入了一个echo,它在update.php页面上打印出来
  • 我可以在php中运行update命令并更改值,但在使用$\u GET[id]时它不起作用
  • 谁能给我指出正确的方向吗

    下面是更改了db连接详细信息的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>
    
    端口
    挂起的端口请求2
    客户
    数字
    类型
    已完成
    更新
    
    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 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");
    
    如果使用字符串而不是整数更新该行,则该行错误。你应该把

    $sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='".intval($_REQUEST['id'])."'" 
    
    1.在update.php中单击提交时,必须通过
    .
    2.行$id=$\u GET['id']在插入查询之前的update\u ac.php中使用。
    
    您是否在$rows['Customer']处获得所需的值?您的代码使您容易受到SQL注入攻击。请看一个例子。另外,由于
    mysql.*
    函数已被弃用,请查看使用MySQLi或@flov的情况。您是否查看过这个问题?他解释说,他只会在当地使用,这不是问题。谢谢你,马亨德拉,这很管用。谢谢大家的建议,非常感谢:)
    $sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" ;
    
    $sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='".intval($_REQUEST['id'])."'" 
    
    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.