希望你能帮忙。
我有一个运行 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();
?>