PHP在mySQL中更新数据不一致,有时只更新

发布于 2024-11-02 13:02:28 字数 995 浏览 1 评论 0原文

我有使用 GET 方法将 URL 变量发送到使用 Ajax 的 PHP 页面的值。

在页面上,我有:

$value=$_GET["q"];
$id=$_GET["id"];
$mod=$_GET["mod"];

我开始使用 UPDATE SET 方法来修改 mySQL 数据库中的值。

我使用:$num_rows= mysql_num_rows($result);

一个 If Else 语句来插入值(如果不存在)或更新列“属性”,

但这非常不一致,通常不会更新,并且开发了几个重复的值(甚至虽然 if($num_rows > 0){ (WHERE Object_ID = '".$mod."' AND Type='".$id."') 它不应该插入,但它确实插入了。)

所以我转向这个:

$sql="SELECT * FROM Attributes WHERE Object_ID = '".$mod."' AND Type='".$id."'";
$result = mysql_query($sql);

    mysql_query("DELETE FROM Attributes WHERE Object_ID = '".$mod."' AND Type = '".$id."'");

    mysql_query("INSERT INTO Attributes (Object_ID, Type, Attribute)
        VALUES ('".$mod."', '".$id."', '".$value."')");

我知道,真是个坏主意。但即使这种方法也并不总是正确插入值。

我知道变量正在到达页面,因为响应将使用innerHTML写入div中,并且它总是正确显示。

我如何确保值始终更新/插入数据库?

I have values which use the GET method to send URL variables to a PHP page using Ajax.

On the page, I have:

$value=$_GET["q"];
$id=$_GET["id"];
$mod=$_GET["mod"];

I started out using the UPDATE SET method to modify values in a mySQL database.

I used: $num_rows= mysql_num_rows($result);

With an If Else statement to either Insert the values (if not there) or Update the column "Attribute"

But this was very inconsistant and often would not UPDATE, and there developed several duplicate values (even though if($num_rows > 0){ (WHERE Object_ID = '".$mod."' AND Type='".$id."') it SHOULD NOT have inserted, but it did.)

So I switched to this:

$sql="SELECT * FROM Attributes WHERE Object_ID = '".$mod."' AND Type='".$id."'";
$result = mysql_query($sql);

    mysql_query("DELETE FROM Attributes WHERE Object_ID = '".$mod."' AND Type = '".$id."'");

    mysql_query("INSERT INTO Attributes (Object_ID, Type, Attribute)
        VALUES ('".$mod."', '".$id."', '".$value."')");

I know, really bad idea. But Even this method doesn't always insert the values correctly.

I know that the variables are getting to the page, because the response would be written in a div using innerHTML, and it always showed up correctly.

How can I ensure that the values are ALWAYS updated/inserted in the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终遇你 2024-11-09 13:02:28

为什么不针对条件运行 SELECT COUNT(*) 并检查返回的内容,然后根据该结果运行 UPDATE 或 INSERT?

Why not run a SELECT COUNT(*) for the condition and check what that returns and based on that run your UPDATE or INSERT?

月亮是我掰弯的 2024-11-09 13:02:28

我真的很困惑你的数据库操作出了什么问题,但唯一确保的方法是将你的 php 脚本的响应发送回 ajax。
它可以是 json 或简单文本,通知您请求了哪个操作以及执行了哪些操作。根据反应,您可以调整您想要的内容。

i am really confused what is wrong with your database operation but the only way to ensure is to send back response from your php script to ajax.
It can be json or simple text informing you that which action was requested and what is performed. Depending on the response you can adjust what you want.

永不分离 2024-11-09 13:02:28

您必须查询表以查明 id 是否已经存在,如果存在,则编写一个更新查询,即

 $update = "update tbl_name set column=value where id = $_GET['id']"

如果 id 不存在,则使用 insert

$insert = "Insert into ......"

希望这能解决您的问题。此外,您还必须以字符串格式或其他值(您将在 $.Ajax 的 success 方法中获得)来回显此处的响应。

You have to query the table to find out if the id already exists there or not if it exits then write an Update Query i.e.

 $update = "update tbl_name set column=value where id = $_GET['id']"

and if the id does not exist then use the insert

$insert = "Insert into ......"

hope this solves your issue. And Further more you have to echo out the response here in either a string format or a some other value which you will get in the success method of $.Ajax.

浮世清欢 2024-11-09 13:02:28

无需查询数据库来查看数据是否已存在。只要你有好的数据,你每次都可以删除该行。如果该行不存在,则 DELETE 不执行任何操作。

每次运行这两个查询,就可以了。

mysql_query("DELETE FROM Attributes WHERE Object_ID = '$mod' AND Type='$id'"); 
mysql_query("INSERT INTO Attributes (Object_ID, Type, Attribute) VALUES ('$mod', '$id', '$value'");

并且请至少在使用 $_GET 执行任何操作之前,首先通过 mysql_real_escape_string 运行它们。您可能应该进行更多检查以确保传入的值有效。

$value=mysql_real_escape_string($_GET["q"]); 
$id=mysql_real_escape_string($_GET["id"]); 
$mod=mysql_real_escape_string($_GET["mod"]);

No need to query the database to see if the data exists already. As long as you have good data, you can delete the row every time. If the row doesn't exist, the DELETE does nothing.

Run these two queries every time, and you will be fine.

mysql_query("DELETE FROM Attributes WHERE Object_ID = '$mod' AND Type='$id'"); 
mysql_query("INSERT INTO Attributes (Object_ID, Type, Attribute) VALUES ('$mod', '$id', '$value'");

And please at a minimum, before you do anything with $_GET, run those through mysql_real_escape_string first. You should probably be doing more checking just to make sure that the values passed in are valid.

$value=mysql_real_escape_string($_GET["q"]); 
$id=mysql_real_escape_string($_GET["id"]); 
$mod=mysql_real_escape_string($_GET["mod"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文