php php从表单更新sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15877314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:07:24  来源:igfitidea点击:

php update sql from form

phpmysql

提问by themoose

I have a php script I'm trying to get working which I've basically just pulled from a tutorial and altered to suit my needs. This is my first attempt at php so please go easy on me.

我有一个 php 脚本,我正在尝试使用它,我基本上只是从教程中提取并进行了更改以满足我的需要。这是我第一次尝试 php,所以请放轻松。

I have 3 files

我有3个文件

  1. list_records.php
  2. update.php
  3. update_ac.php
  1. list_records.php
  2. 更新.php
  3. update_ac.php

List_records reads data from a table in mysql. the table in list_records has an edit function which takes you to update.php where it displays the data in db table.

List_records 从 mysql 的表中读取数据。list_records 中的表有一个编辑功能,它会将您带到 update.php,在那里它显示 db 表中的数据。

Update.php has a submit button which is meant to update mysql using update_ac.php with what ever info you changed using the id field in the url using $_GET['id].

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

I know this script is very open to slq injections but I'm planning to only use this in a local environment, it wont be exposed to the internet and only myself and one other person will be using this page so its not really an issue.

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

Anyway, I've confirmed a couple of things:-

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

  1. the id does get picked up using $_Get, i put in a echo and it printed it out on the update.php page.
  2. i can run the update command within the php and change values but it wont work when using $_GET[id]
  1. id 确实是使用 $_Get 获取的,我输入了一个 echo 并将其打印在 update.php 页面上。
  2. 我可以在 php 中运行 update 命令并更改值,但在使用 $_GET[id] 时它不起作用

Can anyone point me in the right direction?

任何人都可以指出我正确的方向吗?

here are the 3 files with the db connection details altered

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

list_records.php

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

更新.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

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";
}

?>

回答by Mahendra

Your update query should be

您的更新查询应该是

// 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");

回答by themoose

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.

回答by Robert

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

this line is wrong you update it with STRING instead of integer. You should put

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

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