简体   繁体   English

为什么 php mysql update 返回成功但从未更新该列?

[英]why does php mysql update returns a success but never updated the column?

my basic goal is to update a single row and if it is successful it will insert some data in different table.我的基本目标是更新单行,如果成功,它将在不同的表中插入一些数据。

here is my query这是我的查询

$upd = mysql_query("UPDATE request_tbl SET status='1' WHERE sender='$user_id' AND reciever='$id'", $con);

if($upd){
    $result = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES ('$user_id', '$id')", $con);
    $result2 = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES ('$id', '$user_id')", $con);
    
    if($result && $result2){
        $jsonresult = $myFunctions->jsonRequest("Sent Request");
    }
    else{
        $jsonresult = $myFunctions->jsonRequest("Failed Request");
    
    }

}

note: the scenario is i am updating data to the webserver using android app,注意:场景是我正在使用 android 应用程序将数据更新到网络服务器,

when i try to test the query from using this.当我尝试使用它来测试查询时。

http://----/-----.php?req=accept&user-id=5&id=1 http://----/-----.php?req=accept&user-id=5&id=1

i get a success message and updates, and insert the data in the database.我收到一条成功消息和更新,然后将数据插入到数据库中。 However when i try to send the request from the app it returns a success message and inserts the data, that means that the update was successful but in the database it was never update.但是,当我尝试从应用程序发送请求时,它会返回一条成功消息并插入数据,这意味着更新成功但在数据库中从未更新。

i also tried to change the data type of the column status to int or varchar but still with the same effect, it is weird the insert was excuted but the update was never happened or it was?我还尝试将列状态的数据类型更改为 int 或 varchar,但仍然具有相同的效果,奇怪的是插入已执行但从未发生过更新,还是发生了?

EDIT !编辑 ! ! ! i did try all the recommended solutions like mysql_affected_rows() > 0 and echoing the query but with no luck.. as everyone recommends/insits :) i will try to convert the query into PDO or mysqli thanks for the help everyone.我确实尝试了所有推荐的解决方案,如 mysql_affected_rows() > 0 并回显查询但没有运气..正如每个人推荐/插入的那样:) 我将尝试将查询转换为 PDO 或 mysqli 感谢大家的帮助。

**EDIT 2 ! **编辑2! ! ! ** **

i tried to to convert my queries to PDO here is my code我试图将我的查询转换为 PDO,这是我的代码

    $upd = $con->prepare("UPDATE request_tbl SET `status`='1' WHERE `sender`='$user_id' AND `reciever`='$id' AND `status`='0'");

try{
    $upd->execute();
    $result = $con->prepare("INSERT INTO contact_tbl (id, friend_id) VALUES (?, ?)");
    $result->bindParam(1, $user_id);
    $result->bindParam(2, $id);
    $result2 = $con->prepare("INSERT INTO contact_tbl (id, friend_id) VALUES (?, ?)");
    $result2->bindParam(1, $id);
    $result2->bindParam(2, $user_id);
    
    try{
        $result->execute();
        $result2->execute();
        $jsonresult = $myFunctions->jsonRequest("Sent Request");
    }
    catch(PDOException $e){
        $jsonresult = $myFunctions->jsonRequest("Failed Request");
    }
}
catch(PDOException $e){
    $jsonresult = $myFunctions->jsonRequest("Failed Request");
}

echo $jsonresult;

but i recieve the same result, whenever i try it using the url method the update and insert are successful with the correct value, but when i do it with the app the update and insert returns successful and the insert have the correct value but the update did not change the column im changing..但我收到相同的结果,每当我尝试使用 url 方法更新和插入成功并具有正确的值,但是当我使用应用程序执行此操作时,更新和插入返回成功并且插入具有正确的值但更新没有改变我正在改变的列..

use mysql_affected_rows() to check how many rows were affected.使用mysql_affected_rows()检查有多少行受到影响。 Try this,尝试这个,

if(mysql_affected_rows()){
    // your stuff here.
}

Make sure that values of $user_id and $id getting on the current page you are in.确保$user_id$id值出现在您所在的当前页面上。

Hopefully, you passed the user id variable as user-id and retrieved as $_GET['user-id'] .希望您将用户 id 变量作为user-id传递并检索为$_GET['user-id']

If its perfect then, echo both queries and run the printed query on phpmyadmin manually.如果它完美,那么echo这两个查询并在phpmyadmin手动运行打印的查询。 That will through the erroe那将通过错误

echo $result = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES ('$user_id', '$id')", $con);
echo $result2 = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES ('$id', '$user_id')", $con);

more over mysql_query has been deprecated.更多关于mysql_query已被弃用。 Those functions will be no more exist.那些功能将不复存在。 Try PDO or MySQLi尝试PDOMySQLi

try this folk, use mysql_num_rows().试试这个人,使用 mysql_num_rows()。 mysql_affected_rows() give you a nums of rows afected by the last query executed. mysql_affected_rows() 为您提供受上次执行的查询影响的行数。 and you know how manny rows are afected.并且您知道有多少行受到影响。 Hope this can help you希望这可以帮到你

$upd = mysql_query("UPDATE request_tbl SET status='1' WHERE    sender='$user_id' AND reciever='$id'", $con);

$NumsRowsAfected = mysql_affected_rows();

    if(mysql_affected_rows() > 0){
       $result = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES     ('$user_id', '$id')", $con);
       $result2 = mysql_query("INSERT INTO contact_tbl (id, friend_id) VALUES ('$id', '$user_id')", $con);

       if($result && $result2){
         $jsonresult = $myFunctions->jsonRequest("Sent Request");
       }
       else{
         $jsonresult = $myFunctions->jsonRequest("Failed Request");

       }
    }

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

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