Inserting multiple values using mysqli

蹲街弑〆低调 提交于 2020-01-07 03:53:06

问题


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. 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();

回答1:


The problem is the first parameter to bind param it specifys there are two fields of type integer which is not true.

Bind parameters

if user id and age is int, and rest are string type then it should be i for integer, s for string

-- 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();



回答2:


the problem is that there is 6 (?,?,?,?,?,?) and you 7 parameter in bind_param

try

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


来源:https://stackoverflow.com/questions/13004870/inserting-multiple-values-using-mysqli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!