0

希望你能帮忙。

我有一个运行 2 个部分的脚本。顶部要么不返回任何行,如果返回,则运行 INSERT 和 UPDATE 查询。

第二部分做同样的事情,逻辑明智。

如果顶部不返回任何行,则脚本的第二部分可以正常工作

如果顶部返回行,则在脚本的第二部分中仅发生 UPDATE 查询,但不发生 INSERT(并且它不会给出任何错误)

可能是什么问题?这真的开始困扰我了。

这是脚本

<?php

  require_once('function.sendtxt.php');

  $now = time();
  $received = date("Y-m-d H:i:s", $now);

  mysql_connect("x", "x", "x") or die(mysql_error());
  //echo "Connected to MySQL<br />";
  mysql_select_db("x") or die(mysql_error());
  //echo "Connected to Database<br />";

  //$a = array();
  //$a[] = array("id=0", "text");
  $txt = 'We have cancelled your order as we have not heard back from you in a while. To order, please resend your request';

  // those orders not complete over 30 minutes
  $select = mysql_query("
    SELECT id, received, txtus, link, sender
    FROM incoming_texts
    WHERE id IN (
      SELECT i3.id FROM (
        SELECT DISTINCT i2.link as link, max(i2.id) as id
        FROM incoming_texts i1
        JOIN incoming_texts i2 ON i1.id = i2.link
        WHERE (i1.link = 0)
        GROUP BY i2.link ASC
      ) i3
    )
    AND TIMESTAMPDIFF(MINUTE, received, now()) > 30
  ") or die(mysql_error());

  if (mysql_num_rows($select) > 0) {
    while ($row = mysql_fetch_array($select)) {
      // id, received, txtus, link, sender
      $id .= "," . $row["id"];
      $txtus = $row["txtus"];
      $link = $row["link"];
      $from = $row["sender"];
      //$received = $row["received"];

      //echo $id;

      // cancel the row
      mysql_query("
        INSERT INTO incoming_texts
          (txtus, sender, msg, received, this_step, next_step, link)
        VALUES
          (".$txtus.", ".$from.", 'cancel_inactive_text_orders.php 1', '".$received."', 'cancel', 'order', $link)
      ") or die('Error: ' . mysql_error());
      //sendtxt($txtus, $from, $txt);
    }
    // -2 = cancelled
    mysql_query("
      UPDATE incoming_texts
      SET link = -2
      WHERE link IN (00".$id.")
    ") or die(mysql_error());
  }


  /*
    below is the second part
  */

  $link0 = mysql_query("
    SELECT id, received, txtus, link, sender
    FROM incoming_texts
    WHERE (
      link = 0
      AND TIMESTAMPDIFF(MINUTE, received, now()) > 30
    )
  ") or die(mysql_error());

  if (mysql_num_rows($link0) > 0) {
    while($row = mysql_fetch_array($link0)) {
      // id, received, txtus, link, sender
      $id = $row["id"];
      $txtus = $row["txtus"];
      $link = $id;
      $from = $row["sender"];
      //$received = $row["received"];

      $link0rows = mysql_query("
        SELECT id
        FROM incoming_texts
        WHERE link = ".$id."
        LIMIT 1
      ") or die(mysql_error());

      if (mysql_num_rows($link0rows) < 1) {
        mysql_query("
          UPDATE incoming_texts
          SET link = -2
          WHERE (id = $id)
        ") or die(mysql_error());
        mysql_query("
          INSERT INTO incoming_texts
            (txtus, sender, msg, received, this_step, next_step, link)
          VALUES
            (".$txtus.", ".$from.", 'cancel_inactive_text_orders.php 2', '".$received."', 'cancel', 'order', $id)
        ") or die(mysql_error());

        //echo $id . " ";
        //sendtxt($txtus, $from, $txt);

      } // if
    } // while
  } // if

  mysql_close();

?>
4

2 回答 2

0

一切看起来都很好,除了这个......参数$txtus并且$from没有正确引用

    mysql_query("INSERT INTO incoming_texts 
   (txtus, sender, msg, received, this_step, next_step, link) 
    VALUES ('".$txtus."', '".$from."', 'cancel_inactive_text_orders.php 
    1', '".$received."', 'cancel', 'order',$link) ") 
    or die('Error: ' . mysql_error()); 
于 2012-06-07T10:41:05.710 回答
0

尝试先将查询放入变量中并打印出来,这样您就知道查询实际上是正确的。

我相信您很可能在插入中的某些字符串周围缺少引号,例如 $txtus 和 $from

于 2012-06-07T10:50:13.763 回答