我正在尝试使用优先级和原因代码更新文件OPPSHEDT。似乎代码卡在foreach循环中。它使用Count获取SQL我在浏览器上获得selstring的回显然后我没有获得$ Count的回显并且更新没有完成。我不太确定我是否没有连接并在Count上执行实际的SQL。无论如何要告诉这里发生了什么?
<?php
require_once ('C:/wamp/db/login.php');
// Try to connect to database
try
{
$db = new PDO($db_hostname, $db_user, $db_pass);
}
catch (PDOExcepton $e)
{
echo $e->getMessage();
exit();
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (is_array($_POST['line']))
{
$ohord = $_POST['shedord'];
$ohbord = $_POST['shedbord'];
$date1 = $_POST['sheddat'];
$type = $_POST['shedtyp'];
$prty1 = $_POST['shedpty'];
$resn1 = $_POST['shedrsn'];
foreach($_POST['line'] as $line_no)
{
$type1 = $type[$line_no];
$type2 = substr($type1, 0, 1);
$selstring = "Select Count(*) From LPCUSTTST.OPPSHEDT where sheddat = '$date1[$line_no]' and shedtyp = '$type2' and shedord = '$ohord[$line_no]' and shedbord = '$ohbord[$line_no]'";
echo $selstring;
$s = $db->prepare("$selstring");
$s->execute();
echo $Count;
if($Count > 0)
{
// Update data into detail
$selstring1 = "UPDATE LPCUSTTST.OPPSHEDT SET SHEDPTY = '$prty1[$line_no]', SHEDRSN = '$resn1[$line_no]' where sheddat = $date1[$line_no] and shedtyp = '$type2' and shedord = '$ohord[$line_no]' and shedbord = '$ohbord[$line_no]'";
echo $selstring1;
$s = $db->prepare("$selstring1");
$s->execute();
}
}
}
?>
谢谢
答案 0 :(得分:0)
您的第一个SQL语句包含date1[$line_no]
,而您的第二个SQL语句包含$date1[$line_no]
。您可以通过使用参数化查询来使事情变得更容易(也更安全)。
编辑:您修改了帖子以包含丢失的美元符号,但我建议使用参数化查询仍然存在。
$selstring = 'SELECT COUNT(*) as total
FROM LPCUSTTST.OPPSHEDT
WHERE sheddat = :sheddat
AND shedtyp = :shedtyp
AND shedord = :shedord
AND shedbord = :shedbord';
$stm = $db->prepare($selstring);
$stm->execute(
array(
'sheddat' => $date1[$line_no],
'shedtyp' => $type2,
'shedord' => $ohord[$line_no],
'shedbord' => $ohbord[$line_no]
)
);
我没有得到$ Count的回声并且没有完成更新
在您的代码中,您执行echo $Count;
但永远不会定义$Count
。您需要获取值(我在上面的SQL中添加了total
):
$row = $stm->fetch(PDO::FETCH_ASSOC);
$count = $row['total'];