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

时间:2015-03-11 13:10:36

标签: php html mysql arrays

如果有三条记录,我想比较这些值 'test1'和'test 2'并将数据库更新为'open'或'close',如果三个记录的'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);
}
}

?>

1 个答案:

答案 0 :(得分:1)

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

<?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);
    }
?>