因此,我已经使所有这些工作正常了,但是由于某些原因,每列中的按钮仍会影响该列中的所有行。因此,例如,如果我在数据库中有3条记录,然后单击CallAttemptOne(行3)中的按钮,它将影响行1,2,3。我在这里做错了什么?谢谢
(也是,我意识到代码已被弃用。这是第二步!)
//superfluous code removed
$table = 'Project_Submissions';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
//Display all fields
$result = mysql_query("SELECT * FROM {$table} ORDER BY ID DESC");
//superfluous code removed
while ($row = mysql_fetch_array($result))
{
echo "<tr>
<td style='font-size:12px;'><center>{$row['ID']}</center></td>
<td style='font-size:12px;'>{$row['First_Name']} {$row['Last_Name']}</td>
<td style='font-size:12px;'><center>";
//-------------------------------------------------
if(empty($row['CallAttemptOne']))
{
echo"
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formCalledOne' id='formCalledOne' value='Called' />
</form>
{$row['CallAttemptOne']}";
}
else
{
echo "{$row['CallAttemptOne']}";
}
echo "</center></td><td style='font-size:12px;'><center>";
//-------------------------------------------------
if(empty($row['CallAttemptTwo']))
{
echo"
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formCalledTwo' id='formCalledTwo' value='Called' />
</form>
{$row['CallAttemptTwo']}";
}
else
{
echo "{$row['CallAttemptTwo']}";
}
echo "</center></td><td style='font-size:12px;'><center>";
//-------------------------------------------------
if(empty($row['CallAttemptThree']))
{
echo"
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formCalledThree' id='formCalledThree' value='Called' />
</form>
{$row['CallAttemptThree']}";
}
else
{
echo "{$row['CallAttemptThree']}";
}
echo "</center></td><td style='font-size:12px;'><center>";
//-------------------------------------------------
if(empty($row['EmailAttempt']))
{
echo"
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formEmailAttempt' id='formEmailAttempt' value='Emailed' />
</form>
{$row['EmailAttempt']}";
}
else
{
echo "{$row['EmailAttempt']}";
}
echo "</center></td>
//-------------------------------------------------
<td style='font-size:12px;'><center>Text Area</center></td>
<td style='font-size:12px;'><center>{$row['Received_Date']}</center></td>
<td style='font-size:12px;'><center>
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formDelete' id='formDelete' value='Delete' />
</form>
</center></td>
</tr>";
}
//-------------------------------------------------
//Check to see if delete button is pressed
if(isset($_POST['formDelete']))
{
if(isset($_POST['ID']) && !empty($_POST['ID']))
{
$deleteID = $_POST['ID'];
$result = mysql_query("DELETE FROM Project_Submissions WHERE ID ='".$deleteID."'");
}
}
//-------------------------------------------------
if(isset($_POST['formCalledOne']))//Check to see if Call Attempt One button is pressed
{
if(isset($_POST['ID']) && !empty($_POST['ID']))
{
$callattemptoneID = $_POST['ID'];
$callattemptonequery = mysql_query("UPDATE Project_Submissions SET CallAttemptOne=CURDATE() WHERE ID ='".$callattemptoneID."' AND CallAttemptOne IS NULL OR LENGTH(CallAttemptOne)=0");
}
}
//-------------------------------------------------
if(isset($_POST['formCalledTwo']))//Check to see if Call Attempt Two button is pressed
{
if(isset($_POST['ID']) && !empty($_POST['ID']))
{
$callattempttwoID = $_POST['ID'];
$callattempttwoquery = mysql_query("UPDATE Project_Submissions SET CallAttemptTwo=CURDATE() WHERE ID ='".$callattempttwoID."' AND CallAttemptTwo IS NULL OR LENGTH(CallAttemptTwo)=0");
}
}
//-------------------------------------------------
if(isset($_POST['formCalledThree']))//Check to see if Call Attempt Three button is pressed
{
if(isset($_POST['ID']) && !empty($_POST['ID']))
{
$callattemptthreeID = $_POST['ID'];
$callattemptthreequery = mysql_query("UPDATE Project_Submissions SET CallAttemptThree=CURDATE() WHERE ID ='".$callattemptthreeID."' AND CallAttemptThree IS NULL OR LENGTH(CallAttemptThree)=0");
}
}
//-------------------------------------------------
if(isset($_POST['formEmailAttempt']))//Check to see if Email Attempt button is pressed
{
if(isset($_POST['ID']) && !empty($_POST['ID']))
{
$emailattemptID = $_POST['ID'];
$emailattemptquery = mysql_query("UPDATE Project_Submissions SET EmailAttempt=CURDATE() WHERE ID ='".$emailattemptID."' AND EmailAttempt IS NULL OR LENGTH(EmailAttempt)=0");
}
}
?>
</body>
</html>
最佳答案
尝试在查询中使用括号:
UPDATE Project_Submissions SET EmailAttempt=CURDATE() WHERE ID ='".$emailattemptID."' AND (EmailAttempt IS NULL OR LENGTH(EmailAttempt)=0);
您的查询之前不起作用的原因是
WHERE
部分中逻辑运算符的优先级顺序。逻辑是从左到右执行的,因此基本上您的查询与SELECT WHERE (ID ='".$emailattemptID."' AND EmailAttempt IS NULL) OR LENGTH(EmailAttempt)=0;
因此,
LENGTH(EmailAttempt)=0
为true的每一行都包含在结果中。除了将OR
部分括在括号中之外,您还可以颠倒顺序:SELECT WHERE EmailAttempt IS NULL OR LENGTH(EmailAttempt)=0 AND ID ='".$emailattemptID."';
但是除此之外,就像前面的评论中提到的那样,您永远不要直接在查询中使用变量,因为这样会使您的代码容易受到SQL注入攻击的影响。
关于php - 提交按钮影响的范围不应该超过一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30215362/