我正在寫一些PHP腳本來將學生信息添加到學生數據庫。 這是將學生信息添加到數據庫的代碼。如何解決以下PHP警告和錯誤?
<html>
<head>
<title>Add Student</title>
</head>
<body>
<?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['street'])){
// Adds name to array
$data_missing[] = 'Street';
} else {
// Trim white space from the name and store the name
$street = trim($_POST['street']);
}
if(empty($_POST['city'])){
// Adds name to array
$data_missing[] = 'City';
} else {
// Trim white space from the name and store the name
$city = trim($_POST['city']);
}
if(empty($_POST['state'])){
// Adds name to array
$data_missing[] = 'State';
} else {
// Trim white space from the name and store the name
$state = trim($_POST['state']);
}
if(empty($_POST['zip'])){
// Adds name to array
$data_missing[] = 'Zip Code';
} else {
// Trim white space from the name and store the name
$zip = trim($_POST['zip']);
}
if(empty($_POST['phone'])){
// Adds name to array
$data_missing[] = 'Phone Number';
} else {
// Trim white space from the name and store the name
$phone = trim($_POST['phone']);
}
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($_POST['lunch'])){
// Adds name to array
$data_missing[] = 'Lunch Cost';
} else {
// Trim white space from the name and store the name
$lunch = trim($_POST['lunch']);
}
if(empty($data_missing)){
require_once('mysqli_connect.php');
$query = "INSERT INTO students (first_name, last_name, email,
street, city, state, zip, phone, birth_date, sex,
lunch_cost) VALUES (?, ?, ?,
?, ?, ?, ?, ?, ?, ?,?)";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "sssssssissd", $f_name,$l_name, $email, $street, $city,$state, $zip, $phone, $b_date,$sex, $lunch);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
if($affected_rows == 1){
echo 'Student Entered';
mysqli_stmt_close($stmt);
mysqli_close($dbc);
} else {
echo 'Error Occurred<br />';
echo mysqli_error();
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 />";
}
}
}
?>
當我運行程序我得到以下信息:
警告:mysqli_stmt_bind_param()預計參數1被mysqli_stmt,布爾在C中給出:\ XAMPP \ htdocs中\ studentadded.php上線157
警告:mysqli_stmt_execute()預計參數1被mysqli_stmt,在C中給出布爾:\ XAMPP \ htdocs中\ studentadded.php上線159
警告:mysqli_stmt_affected_rows()預計參數1被mysqli_stmt,布爾給定在C:\ XAMPP \ htdocs中\ studentadded.php上線161 出錯
警告:mysqli_error()期望的是1個參數,0在C中給出:\ XAMPP \ htdocs中\ studentadded.php上線174
警告:mysqli_stmt_close()預計參數1被mysqli_stmt,在C中給出布爾:\ XAMPP \ htdocs中\ studentadded.php上線176
mysqli_connect.php:
<?php
$servername = "localhost";
$username = "studentweb";
$password = "turtleneck";
$database = "test3";
$dbc = @mysqli_connect($servername, $username, $password, $database);
if (!$dbc) {
die("Connection failed: " . mysqli_connect_error());
}
echo " Connected Successfully";
?>
此外,當我檢查我的數據庫在phpmyadmin它不是你記錄輸入的數據。
請大家幫忙。