使用mysqli插入多个值 [英] Inserting multiple values using mysqli

查看:91
本文介绍了使用mysqli插入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我这段代码有什么问题吗?我目前是新手,现在尝试使用mysqli prepare语句来连接到数据库后端. 到目前为止,我似乎无法获取它来更新数据库.

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

推荐答案

问题是绑定参数的第一个参数,它指定有两个整数类型的字段,该字段不正确.

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

绑定参数

如果用户ID和age为int,其余为字符串类型,则 它应该是 i代表整数,s代表字符串

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

-更新

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

这篇关于使用mysqli插入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆