未捕获的异常' mysqli_sql_exception'有消息'没有为准备好的陈述中的参数提供数据'

时间:2017-07-28 05:14:45

标签: php sql mysqli

我正在尝试使用预准备语句编写查询。

mysqli_report(MYSQLI_REPORT_ALL);

    $query = $conn->prepare("INSERT INTO notification(MessageId,TopicArn,Subject,Message,Timestamp,SignatureVersion,Signature,SigningCertURL,UnsubscribeURL,topicid) 
                VALUES (?, ?, ?, ?, ?,?, ?, ?, ?, ?)");

这会引发以下异常

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No data supplied for parameters in prepared statement'

以下查询的位置

$query = $conn->prepare("INSERT INTO notification(MessageId,TopicArn,Subject,Message,Timestamp,SignatureVersion,Signature,SigningCertURL,UnsubscribeURL,topicid) 
        VALUES (".$message['MessageId'].", '".$message['TopicArn']."', '".$message['Subject']."', '".$message['Message']."', '".$message['Timestamp']."',".$message['SignatureVersion'].", '".$message['Signature']."', '".$message['SigningCertURL']."', '".$message['UnsubscribeURL']."', ".$topic.")");

我想在bind_param()函数中使用预准备语句。第一次查询有什么问题?请帮忙。

2 个答案:

答案 0 :(得分:1)

正如我所建议的那样 - 你需要在执行sql之前将参数绑定到值〜或许这样:

mysqli_report(MYSQLI_REPORT_ALL);
$sql='insert into notification
    (messageid,topicarn,subject,message,timestamp,signatureversion,signature,signingcerturl,unsubscribeurl,topicid) 
    values
    (?, ?, ?, ?, ?,?, ?, ?, ?, ?)';

$stmt = $conn->prepare( sql );
if( $stmt ){
    /* assumed mainly strings other than those with `id` in column name */
    $stmt->bind_param('issssssssi',
        $message['MessageId'],
        $message['TopicArn'],
        $message['Subject'],
        $message['Message'],
        $message['Timestamp'],
        $message['SignatureVersion'],
        $message['Signature'],
        $message['SigningCertURL'],
        $message['UnsubscribeURL'],
        $topic
    );
    $result=$stmt->execute();

    /* Other code */

} else {
    /* investigate why "prepare" method failed */
    echo "Error:";
}

答案 1 :(得分:0)

您可以绑定如下数据。

$stmt = $conn->prepare( $sql );
if( $stmt ){ 
    $stmt->bind_param('issssssssi', $message['MessageId'], $message['TopicArn'],
        $message['Subject'], $message['Message'], $message['Timestamp'],
        $message['SignatureVersion'], $message['Signature'],   $message['SigningCertURL'],
        $message['UnsubscribeURL'] ,$message['UnsubscribeURL']  );
}

您忘记在$前面添加sql,请添加并重试。