简体   繁体   English

如何使用php脚本更新两个不相关的mysql表?

[英]How can I update two unrelated mysql tables with a php script?

How can I update two unrelated mysql tables with a php script? 如何使用php脚本更新两个不相关的mysql表? I use something like this. 我用这样的东西。 If i use two different $sql statements like $sql1 $sql2 its not working. 如果我使用两个不同的$ sql语句,例如$ sql1 $ sql2,则它不起作用。

// mysql update row with matched pid
    $result1 = mysql_query("UPDATE users SET users.p = users.p - '$p', users.n = users.n -'$n'  WHERE users.uid = $uid");
    $result2 = mysql_query("UPDATE names SET names.p = names.p +  '$p', names.n = names.n + '$n'  WHERE names.p_id = $p_id");
    // check if row inserted or not
    if ($result1 && $result2) 
   {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } 

You subtract string from string??? 您从字符串中减去字符串??? users.p = users.p - '$p' users.p = users.p-'$ p'

first use users.p - $p instead of users.p - '$p' second return mysql error if occur 第一次使用users.p - $p代替users.p - '$p'第二个返回mysql错误(如果发生)

$p= intval($_POST['p']);
$n= intval($_POST['n']);

parse data like above 像上面一样解析数据

<?php
// mysql update row with matched pid
$result1 = mysql_query("UPDATE users SET users.p = users.p - $p, users.n = users.n -$n  WHERE users.uid = $uid");
$result2 = mysql_query("UPDATE names SET names.p = names.p +  $p, names.n = names.n + $n  WHERE names.p_id = $p_id");
// check if row inserted or not
if ($result1 && $result2) {
   // successfully updated
    $response["success"] = 1;
    $response["message"] = "successfully updated.";

    // echoing JSON response
    echo json_encode($response);
} else {

   // failed update
    $response["success"] = 0;
    $response["message"] = "Mysql error is : " . mysql_error();

    // echoing JSON response
    echo json_encode($response);
}
?>

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

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