Query is not working if I add more database column

时间:2016-09-01 06:31:35

标签: mysql

I'm trying to update multiple row in my query but it's not working. But if I try to update only one row, it's working

Here is my working code:

        if(!empty($_POST['include'])) {
            foreach($_POST['include'] as $check) {
                echo "<input type=\"text\" name=\"master[]\" size=\"50\" value=\"$check\"> <br/>";

            $vessel = 'SAMMY';
                $query = "UPDATE `info` SET `vessel` = ? WHERE `id` = ?";
                $stmt = mysqli_prepare($conn, $query);
                mysqli_stmt_bind_param($stmt, 'si', $vessel, $check);
                mysqli_stmt_execute($stmt);
            }
        }

Here is my not working code:

        if(!empty($_POST['include'])) {
            foreach($_POST['include'] as $check) {
                echo "<input type=\"text\" name=\"master[]\" size=\"50\" value=\"$check\"> <br/>";

                $crew_status = 'LINEUP PENDING';
            $vessel = 'SAMMY';
                $query = "UPDATE `info` SET `vessel` = ? AND `crew_status` = ? WHERE `id` = ?";
                $stmt = mysqli_prepare($conn, $query);
                mysqli_stmt_bind_param($stmt, 'ssi', $vessel, $crew_status, $check);
                mysqli_stmt_execute($stmt);
            }
        }

1 个答案:

答案 0 :(得分:2)

请勿在SQL查询中使用AND,而是使用,代替:

UPDATE `info` SET `vessel` = ?, `crew_status` = ? WHERE `id` = ?"

在MySQL中使用Update查询设置多个值时,必须根据有关UPDATE Syntax

的MySQL文档用逗号分隔它们
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]