简体   繁体   English

MySQL中的PHP更新数据不一致,有时仅更新

[英]PHP Update Data in mySQL Inconsistant, only UPDATES sometimes

I have values which use the GET method to send URL variables to a PHP page using Ajax. 我有一些值使用GET方法使用Ajax将URL变量发送到PHP页面。

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. 我开始使用UPDATE SET方法来修改mySQL数据库中的值。

I used: $num_rows= mysql_num_rows($result); 我用过: $num_rows= mysql_num_rows($result);

With an If Else statement to either Insert the values (if not there) or Update the column "Attribute" 使用If Else语句插入值(如果不存在)或更新“属性”列

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.) 但这是非常不一致的,通常不会进行更新,并且开发了多个重复值(even though if($num_rows > 0){ (WHERE Object_ID = '".$mod."' AND Type='".$id."')应该没有插入,但确实插入了。)

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. 我知道变量正在到达页面,因为响应将使用innerHTML写入div中,并且始终正确显示。

How can I ensure that the values are ALWAYS updated/inserted in the database? 如何确保始终将值更新/插入数据库中?

为什么不为条件运行SELECT COUNT(*)并检查返回的内容以及基于该条件运行UPDATE或INSERT的原因?

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. 我真的很困惑您的数据库操作出了什么问题,但是确保的唯一方法是将响应从您的php脚本发送回ajax。 It can be json or simple text informing you that which action was requested and what is performed. 它可以是json或简单的文本,通知您请求了哪个操作以及执行了什么操作。 Depending on the response you can adjust what you want. 根据响应,您可以调整所需的内容。

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 ie 您必须查询表以查明该ID是否已存在(如果存在),然后编写更新查询,即

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

and if the id does not exist then use the insert 如果id不存在,则使用插入

$insert = "Insert into ......" $ insert =“插入......”

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. 而且,您还必须在这里以字符串格式或其他值(在$ .Ajax的成功方法中获得)回显响应。

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. 如果该行不存在,则DELETE不执行任何操作。

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. 并且至少,在使用$ _GET进行任何操作之前,请首先通过mysql_real_escape_string运行它们。 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"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM