简体   繁体   English

使用mysqli插入多个值

[英]Inserting multiple values using mysqli

Can anybody tell me what is wrong with this line of code? 谁能告诉我这行代码有什么问题? I am currently new and now trying to use mysqli prepared statement in order to connect to the database back end. 我目前是新的,现在尝试使用mysqli预编译语句,以便连接到数据库后端。 So far I can't seem to get it to update the database. 到目前为止,我似乎无法更新数据库。

  $stmt = $mysqli->prepare("INSERT INTO canada VALUES (?,?,?,?,?,?)");
    $stmt->bind_param('sssiss',$_REQUEST["UserID"],$_REQUEST["FirstName"],
                        $_REQUEST["LastName"],$_REQUEST["Age"],$_REQUEST["WhatParty"],
                        $_REQUEST["Electorate"]);

    $stmt->execute();

the problem is that there is 6 (?,?,?,?,?,?) and you 7 parameter in bind_param 问题是bind_param中有6个(?,?,?,?,?,?)和7个参数

try 尝试

 $stmt = $mysqli->prepare("INSERT INTO canada VALUES (?,?,?,?,?,?,?)");

The problem is the first parameter to bind param it specifys there are two fields of type integer which is not true. 问题是绑定param的第一个参数,它规定有两个integer类型的字段,它们不是真的。

Bind parameters 绑定参数

if user id and age is int, and rest are string type then it should be i for integer, s for string 如果用户id和age是int,而rest是字符串类型那么它应该是i表示整数,s表示字符串

-- Update - 更新

$db = new mysqli($server_host, $server_user, $server_password, $server_db);

if (mysqli_connect_errno()) {
    printf("DB error: %s", mysqli_connect_error());
    exit();
}



$stmt = $db->prepare("INSERT INTO canada
                      userid,firstname,lastname,age,whatparty,electorate)
                      VALUES (?,?,?,?,?,?))");

$stmt->bind_param("ississ",$_REQUEST["UserID"],$_REQUEST["FirstName"],
                  $_REQUEST["LastName"],$_REQUEST["Age"],
                  $_REQUEST["WhatParty"],$_REQUEST["Electorate"]);


$stmt->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM