错误消息:Array()警告:mysqli :: query()期望参数1为字符串,对象

时间:2019-03-22 11:39:22

标签: php mysql sql

我有一个运行得非常顺利的查询,使用用户提供的新图像标题更新了数据库。可以在下面看到:

<?php

    $query = "SELECT * FROM `tblImage`";
    $result = $conn -> query($query);
    while($row = $result -> fetch_assoc())
    {
?>
    <form method="post" action="">
    <a href="<?php echo $row['fldFilePath']; ?>" data-lightbox="gallery" data-title="<?php echo $row['fldName']; ?>"><img src="<?php echo $row['fldFilePath']; ?>" class="ImgRound"></a>

    <label>Image Name: <?php echo $row['fldName']; ?></label>

    <input name ="img-title" type ="text" placeholder="Enter New Image Title...">
    <button type="submit" value ="<?php echo $row['fldName'] ?>" name="update_title" class="ImgRound">Update Title</button>


    <button type="submit" value ="<?php echo $row['fldName'] ?>" name="delete" class="ImgRound">Delete</button>
    </form>
<?php

    if(isset($_POST['update_title']))
        {
        $imgTitle = $_POST['img-title'];

        $stmt= "UPDATE `tblImage` SET `fldName` = '$imgTitle' WHERE `fldName` = '$_POST[update_title]' ";   


        $result = $conn -> query($stmt);    
        if($conn -> query($stmt))
            {
                header("Refresh:0");

            }   
        }

    }
?>

但是,当我回到代码中为$stmt绑定参数时,我在代码中的某个地方犯了一个错误,该错误消息告诉我:  Array ( ) Warning: mysqli::query() expects parameter 1 to be string, object given

我尝试给出此消息的更新查询是:

$stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?");
$stmt->bind_param("ss", $imgTitle, $_POST['update-title']);
$stmt->execute();


if($conn -> query($stmt))
    {
        header("Refresh:0");
    }   

我已经在此错误消息下查看了此内容,但仍在努力了解自己所做的事情。

3 个答案:

答案 0 :(得分:1)

您不应该尝试将$stmt变量传递给query()。要检查UPDATE是否成功,只需检查execute()的结果即可:

$stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?") or die($conn->error);
$stmt->bind_param("ss", $imgTitle, $_POST['update_title']) or die($stmt->error);
if ($stmt->execute())
    {
        header("Refresh:0");
    }   
else {
    die($stmt->error);
}

答案 1 :(得分:1)

prepare方法分配一个变量,以便您可以测试是否已成功创建该语句对象,然后将一个变量分配给execute的返回值以测试它是否也是成功。

$stmt = $conn->prepare( "UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?" );
if( $stmt ){
    $stmt->bind_param("ss", $imgTitle, $_POST['update_title']);
    $result = $stmt->execute();

    if( $result )exit( header("Refresh:0") );
}

-更新

<?php

    $query = "SELECT * FROM `tblImage`";
    $result = $conn->query($query);
    while( $row = $result -> fetch_assoc() ) {

?>

    <form method="post">
        <a href="<?php echo $row['fldFilePath']; ?>" data-lightbox="gallery" data-title="<?php echo $row['fldName']; ?>">
            <img src="<?php echo $row['fldFilePath']; ?>" class="ImgRound" alt='' />
        </a>
        <label>Image Name: <?php echo $row['fldName']; ?></label>
        <input name="img-title" type="text" placeholder="Enter New Image Title..." />
        <button type="submit" value="<?php echo $row['fldName'] ?>" name="update_title" class="ImgRound">Update Title</button>
        <button type="submit" value="<?php echo $row['fldName'] ?>" name="delete" class="ImgRound">Delete</button>
    </form>


<?php

    } #close loop here ???

?>


<?php

    if( isset( $_POST['update_title'], $_POST['img-title'] ) ) {

        $imgTitle = filter_input( INPUT_POST, 'img-title', FILTER_SANITIZE_STRING );
        $updateTitle = filter_input( INPUT_POST, 'update_title', FILTER_SANITIZE_STRING );

        if( $imgTitle & $updateTitle ){

            $stmt = $conn->prepare( 'UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?' );

            if( $stmt ){
                $stmt->bind_param( 'ss', $imgTitle, $updateTitle );
                $result = $stmt->execute();
                if( $result )exit( header("Refresh:0") );
            }

            $stmt->close();
        }
    }
?>

答案 2 :(得分:0)

dim slice as string = cat.Substring(0, 4)

您在这里给出3个参数。

应该不是这样..

$stmt->bind_param("ss", $imgTitle, $_POST['update-title']);

假设,  $stmt = $conn->prepare("UPDATE `tblImage` SET `fldName` = ? WHERE `fldName` = ?"); $stmt->bind_param($_POST['update-title'], $imgTitle); -更新后的值。  $_POST['update-title']-要更新的值