插入新行而不是更新

时间:2013-09-17 22:36:01

标签: php mysql insert insert-update

联结表“memberprogram”包含三个字段(Memberid Programid Option_No)。 当我尝试更新行而不是更新时,它会插入新行。

我的代码:

$query_find = "SELECT * FROM memberprogram WHERE ((Memberid == $Memberid) && (Option_No == 'Option A'))";
$result_query_find = mysqli_query($dbc, $query_find);
if (mysqli_num_rows($result_query_find) == 1)
{
$query_update = "UPDATE memberprogram SET memberprogram.Programid = (SELECT programs.Programid FROM programs WHERE programs.Program_Description = '$Program_Description') WHERE ((memberprogram.Memberid == $Memberid) && (memberprogram.Option_No == 'Option A'))";
        $result_query_update = mysqli_query($dbc, $query_update);
     }
        else {

        $query_insert = "INSERT memberprogram SET memberprogram.Memberid = '$Memberid', memberprogram.Option_No = 'Option A',  memberprogram.Programid = (SELECT programs.Programid FROM programs WHERE programs.Program_Description = '$Program_Description')";
        $result_query_insert = mysqli_query($dbc, $query_insert);

}

1 个答案:

答案 0 :(得分:0)

我不相信你的查询功能在phpmyadmin:

尝试此查询:

SELECT * FROM memberprogram WHERE ((Memberid = $Memberid) AND (Option_No = 'Option A'));

等于运算符应该是=而不是==

我建议使用AND代替&&