如何获得"ID" mysql记录中的数字,并使用它使用php更新该记录? [英] How do I get the "ID" number from a mysql record and use it to update that record using php?

查看:77
本文介绍了如何获得"ID" mysql记录中的数字,并使用它使用php更新该记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试表单(已删除表单的链接),如果您单击更新票证链接,它将带您到一个页面,以在记录中添加其他信息.我看到该记录的ID传递到浏览器URL中的表单,因为该URL的末尾说?id = 10或该记录的ID号是什么.这是我遇到的问题,无法更新记录,我在表单中输入信息,然后单击更新"按钮,它告诉我它添加了1条记录,但未将信息添加到MySQL中,我也没有任何错误.所以这是我用来更新记录的代码:

Here is my test form (link to form removed) if you click on the update ticket link it will bring you to a page to add additional information to the record. I see the id for that record passed to the form in the browser url as the end of the url says ?id=10 or whatever the id number for that record is. This is where I'm stuck, I can't get the record to update, I input the info in the form and click the update button and it tells me it added 1 record but it doesn't add the info to MySQL and I don't get any errors either. so here is the code I'm using to update the record:

<?php

include 'db_connect.php';

$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$id=$_REQUEST['id'];

 mysql_query("UPDATE `test` SET 
                                 `city` = '$city',
                                 `state` = '$state',
                                 `zip` = '$zip',
                           WHERE `id` = '$id'") 

?>

我在表单中添加了一个隐藏的输入字段,名称为"id",认为这是错误的,但没有更改.我究竟做错了什么?我需要怎么做才能传递ID?谢谢!

I added a hidden input field to my form with the name of 'id' thinking that was what was wrong but no change. What am I doing wrong? what do I need to do to pass the id? Thanks!

为我的更新表单添加代码:

Adding the code for my update form:

<html>
<body>

<form action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
City <input type="text" name="city">
State<input type="text" name="state">
Zip <input type="text" name="zip">
<input type="submit" value="update">
</form>

</body>
</html> 

修订的更新代码:

<?php

include 'db_connect.php';

$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$id=$_POST['id'];




 mysql_query("UPDATE `test` SET      `city` = '$city', `state` = '$state', `zip` = '$zip' WHERE `id` = '$id'");



?>

推荐答案

您需要使用表单发布ID.

You need to post id with the form.

//Add this hidden field inside form to store and post id
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>

发布表单时ID丢失.因此我们需要将其存储并随表格一起发送.

The id gets lost when form is posted. so we need to store it and send it with the form.

// remove the comma before where in this line
mysql_query("UPDATE `test` SET      `city` = '$city', `state` = '$state', `zip` = '$zip' WHERE `id` = '$id'");

这篇关于如何获得"ID" mysql记录中的数字,并使用它使用php更新该记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆