<?php
if(isset($_POST['submit'])){
$data_missing = array();
if(empty($_POST['first_name'])){
// Adds name to array
$data_missing[] = 'First Name';
} else {
// Trim white space from the name and store the name
$f_name = trim($_POST['first_name']);
}
if(empty($_POST['last_name'])){
// Adds name to array
$data_missing[] = 'Last Name';
} else{
// Trim white space from the name and store the name
$l_name = trim($_POST['last_name']);
}
if(empty($_POST['email'])){
// Adds name to array
$data_missing[] = 'Email';
} else {
// Trim white space from the name and store the name
$email = trim($_POST['email']);
}
if(empty($_POST['birth_date'])){
// Adds name to array
$data_missing[] = 'Birth Date';
} else {
// Trim white space from the name and store the name
$b_date = trim($_POST['birth_date']);
}
if(empty($_POST['sex'])){
// Adds name to array
$data_missing[] = 'Sex';
} else {
// Trim white space from the name and store the name
$sex = trim($_POST['sex']);
}
if(empty($data_missing)){
require_once('database.php');
$query = "INSERT INTO etable (first_name, last_name, email,
birth_date, sex) VALUES (?, ?, ?,
?, ?, ?)";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "ssssss", $f_name,
$l_name, $email, $b_date,
$sex);
mysqli_stmt_execute();
$affected_rows = mysqli_stmt_affected_rows();
if($affected_rows == 1){
echo 'Student Entered';
mysqli_stmt_close($stmt);
mysqli_close($dbc);
} else {
echo 'Error Occurred<br />';
echo mysqli_error($dbc);
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
} else {
echo 'You need to enter the following data<br />';
foreach($data_missing as $missing){
echo "$missing<br />";
}
}
}
?>
答案 0 :(得分:2)
如果您获得了boolean
,那么它可能表示您的mysqli_prepare
查询错误。在你的情况下:
$query = "INSERT INTO etable (first_name, last_name, email,
birth_date, sex) VALUES (?, ?, ?,
?, ?, ?)";
我看到您选择了5列,但写了6 ?
(参数),从而使此查询无效。
顺便说一句,连接也可能造成麻烦。可以肯定的是,你可以写:
echo mysqli_connect_error();
检查您的连接是否有效。