dsnpjz6907 2018-09-12 00:03
浏览 288
已采纳

检查与您的MySQL服务器版本相对应的手册,以便在'?,?,?,?附近使用正确的语法?

I'm trying to create a form and import submitted data to a database,

<?php
// db has to be manually created first
$host = "localhost";
$userName = "user";
$password = "";
$dbName = "test";

// Create database connection
$conn = new mysqli($host, $userName, $password, $dbName);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))
{

$yourName = $conn->real_escape_string($_POST['your_name']);
$yourEmail = $conn->real_escape_string($_POST['your_email']);
$yourPhone = $conn->real_escape_string($_POST['your_phone']);
$comments = $conn->real_escape_string($_POST['comments']);


$sql = "INSERT INTO contact_form_info (name, email, phone, comments)
        VALUES (?,?,?,?)";



//$stmt = mysqli_prepare($sql);
//if ($stmt = $conn->prepare(" INSERT INTO contact_form_info WHERE name = ?  email = ? phone = ? comments = ? ")) {
if ($stmt = $conn->prepare($sql)) {

$stmt->bind_param("ssss", $yourName, $yourEmail, $yourPhone,$comments);

$stmt->execute();
}

When I submit my form, I get the following error

check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?)"

Can someone please check my code and tell me what's wrong with it?

And thanks in advance

  • 写回答

1条回答 默认 最新

  • dongyong2063 2018-09-12 03:45
    关注

    This error shows that for some reason, the question marks are not being bound to the appropriate variables. MySQL is complaining because the question marks are not encapsulated with "".

    Ass @RiggsFolly pointed out, you don't need to call $conn->real_escape_string as you are using prepared statements.

    (Also, there is no need to check isset() and an empty string, empty() basically does an isset anyway)

    <?php
    // db has to be manually created first
    $host = "localhost";
    $userName = "user";
    $password = "";
    $dbName = "test";
    
    // Create database connection
    $conn = new mysqli($host, $userName, $password, $dbName);
    
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    if(!empty($_POST['your_name']) && !empty($_POST['your_email']) {
    
        $sql = "INSERT INTO contact_form_info (name, email, phone, comments) VALUES (?,?,?,?)";
    
        $stmt = $conn->prepare($sql);
    
        $stmt->bind_param("ssss", $_POST['your_name'], $_POST['your_email'], $_POST['your_phone'],$_POST['comments']);
    
        $stmt->execute();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂