我运行以下代码,但它显示的数组值没有将数据存储到mysqldb中。
<?php
if(! empty( $_POST)){
print_r($_POST);exit;
$mysqli = new mysqli('localhost', 'root','','mydb');
if($mysqli->connect_error){
die('connect error:'. $mysqli->connect_errno .':' . $mysqli->connect_error);
}
}
?>
register.php:
<?php
require('db.php');
$conn=mysqli_connect("localhost","root","");
$name = $_POST['name'];
$name1 = $_POST['skill'];
$name2= $_POST['exp'];
$name3 = $_POST['sele'];
$sql = "INSERT INTO register VALUES('".$name."','".$name1."','".$name2."','".$name3."')";
$insert = $mysqli->query($conn,$sql);
echo $insert;
?>
在浏览器中获取数组结果
答案 0 :(得分:2)
已定义连接对象 - $mysqli
。为什么再次$conn
?只是做 -
$sql = "INSERT INTO register VALUES('".$name."','".$name1."','".$name2."','".$name3."')";
$insert = $mysqli->query($sql);
如果您使用$conn
方法,则需要 procedural
。
答案 1 :(得分:1)
只需替换:$insert = $mysqli->query($conn,$sql);
使用:
$insert = $mysqli->query($sql);
答案 2 :(得分:-1)
让我们从头开始。
$fields = ["name", "skill", "exp", "sele"];
$params = [];
foreach($fields as $field) {
if(!isset($_POST[$field])) {
exit("Field required: $field");
}
$params[":$field"] = $_POST[$field];
}
$pdo = new PDO("mysql:host=localhost;dbname=database", 'username', 'password');
$sth = $pdo->prepare('
INSERT INTO register VALUES(:name, :skill, :exp, :sele);
');
$sth->execute($params);
如果你还想坚持使用mysqli ......
$fields = ["name", "skill", "exp", "sele"];
$params = [];
foreach($fields as $field) {
if(!isset($_POST[$field])) {
exit("Field required: $field");
}
$params[":$field"] = $_POST[$field];
}
$mysqli = new mysqli('localhost','username','password','database');
$query = $mysqli->prepare('
INSERT INTO register VALUES(?, ?, ?, ?);
');
call_user_func_array(array($query, "bind_param"), $params);
$query->execute();