-4

再会

我有一个 PHP 代码应该将数据插入 SQL 数据库并从另一个表中扣除学分。将数据插入到 SQL 中效果很好,直到我从积分字符串中添加了扣除,现在它只是扣除积分但不更新数据库。

这是我的代码:

 function update_lbs($long, $lat, $msisdn, $distance)
 {
     global $host;
     global $username;
     global $password;
     global $db_name;
     $today = date("Y-m-d");
     $insertSuccessful = false;
     $new_msisdn = '0' . substr($msisdn, 2);
     if ($con = mysql_connect($host, $username, $password)) {
         if (mysql_select_db($db_name)) {
             $sql = "insert into johan_lbs_log set lng = '".$long."', lat = '".$lat."',   distance = '".$distance."', msisdn = '".$msisdn."', date_time = '".$today."'";
        $sql = "Update johan_credits set credits=credits-1 Where account_nr=1";
        if (mysql_query($sql, $con)) {
            $insertSuccessful = true;
        } else {
            echo $sql;
            echo "\n" . mysql_error($con);
            echo "mysql err no : " . mysql_errno($con);
        }
      }
  }

    return $insertSuccessful;
 }

我需要它来使用提供的信息更新 johan_lbs_log,并将 johan_credits 减一

任何帮助将不胜感激

4

3 回答 3

1

你只是update在这里执行查询

if (mysql_select_db($db_name)) {
    $sql = "insert into johan_lbs_log set lng = '".$long."', lat = '".$lat."',   distance = '".$distance."', msisdn = '".$msisdn."', date_time = '".$today."'";
    mysql_query($sql, $con); // you have to execute insert query too
    $sql = "Update johan_credits set credits=credits-1 Where account_nr=1";
    if (mysql_query($sql, $con)) {
    .....

注意: mysql_*不推荐使用函数,开始使用mysqliOR PDO

于 2013-08-01T06:18:21.877 回答
0

试试下面的代码

function update_lbs($long, $lat, $msisdn, $distance)
 {
     global $host;
     global $username;
     global $password;
     global $db_name;
     $today = date("Y-m-d");
     $insertSuccessful = false;
     $new_msisdn = '0' . substr($msisdn, 2);
     if ($con = mysql_connect($host, $username, $password)) {
         if (mysql_select_db($db_name)) {
             $sql = "update johan_lbs_log set lng = '".$long."', lat = '".$lat."',   distance = '".$distance."', msisdn = '".$msisdn."', date_time = '".$today."' where id=1"; //Insert is changed to update
             mysql_query($sql, $con); //To execute update query
             $sql1 = "Update johan_credits set credits=credits-1 Where account_nr=1";
        if (mysql_query($sql1, $con)) {
            $insertSuccessful = true;
        } else {
            echo $sql;
            echo "\n" . mysql_error($con);
            echo "mysql err no : " . mysql_errno($con);
        }
      }
  }

    return $insertSuccessful;
 }
于 2013-08-01T06:25:41.777 回答
0

为更新语句使用不同的名称,就像您在此处$sql用于插入和更新一样,但将更新的名称更改为$sql1,然后执行您正在执行的所有操作$sql

于 2013-08-01T06:20:36.957 回答