比较多个数组值并更新表状态


Compare multiple array values and update table status

如果有三条记录,我想比较这些值如果三个记录的"test1"和"test2"值匹配相同,则将数据库更新为"打开"或"关闭"。但是下面的代码不起作用。

.HTML:

    while($row = mysql_fetch_assoc($sql)){
        <input type="text" value="<?php echo $row['test1']; ?>" name="test1[]">
        <input type="text" value="" name="test2[]">
    }
  <input name="Submit" type="submit" value="update">

.PHP:

<?php
if (isset($_POST['Submit']))
{
$test1=$_POST['test1'];
$test2=$_POST['test2'];
if($test1==$test2)
{
$sql1="UPDATE table SET status='close' WHERE test1='$test1' AND test2='$test2' ";
$db=mysql_query($sql1);
}
else
{
$sql1="UPDATE table SET status='open' WHERE test1='$test1' AND test2='$test2' ";
$db=mysql_query($sql1);
}
}
?>

您需要比较数组值。 尝试下面的代码,

<?php
    $three = array_intersect($test1,$test2);
    if(count($three) > 2)
    {
    $sql1="UPDATE table SET status='close' WHERE test1='$test1' AND test2='$test2' ";
    $db=mysql_query($sql1);
    }
    else
    {
    $sql1="UPDATE table SET status='open' WHERE test1='$test1' AND test2='$test2' ";
    $db=mysql_query($sql1);
    }
?>