php从表单更新sql
我有一个php脚本,我正在努力工作,我基本上只是从一个教程中提取,并根据我的需要进行修改。这是我第一次尝试php,所以请对我放松点 我有3个文件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,其中包含您使用$\
<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> </td>
<td colspan="6"><strong>Update Porting Details</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </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> </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"> </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.