【发布时间】:2014-07-08 09:53:37
【问题描述】:
我有一个插入代码,假设使用 mysqli 扩展名保存到数据库。 这是我的mysqli连接
<?php
global $db;
//dbconnector.php
//define the mysql connection variables.
define ("MYSQLHOST", "localhost");
define ("MYSQLUSER", "root");
define ("MYSQLPASS", "password");
define ("MYSQLDB", "sissystem");
// connect to mysql server
$db = new mysqli(MYSQLHOST,MYSQLUSER,MYSQLPASS,MYSQLDB);
// check if any connection error was encountered
if(mysqli_connect_errno()){
echo "Error: Could not connect to database.";
exit;
}
?>
这是我的插入函数代码
function enterStudent($db,$regid,$fName,$lName,$class,$dobirth,$parentName,$addrs,$s_area,$s_city,$s_state,$s_country,$s_zip,$s_phone,$s_email,$spix_link,$s_xinfo){
//sql query
//$query ="insert into students_info(student_regNo,student_firstName,student_lastName,class,student_dateOfBirth,student_fathersName,student_address,student_area,student_city,student_state,student_country,student_zipCode,student_phoneNum,student_email,student_imageLink,student_extraInfo) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$query ="insert into students_info values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//echo $query;
$stmt = $db->prepare($query);
$stmt->bind_param('ssssssssssssssss',$regid,$fName,$lName,$class,$dobirth,$parentName,$addrs,$s_area,$s_city,$s_state,$s_country,$s_zip,$s_phone,$s_email,$spix_link,$s_xinfo);
// execute the insert query
if($stmt->execute()){
$affected_rows = $stmt->rowCount();
echo "New Student Detail Added.";
$stmt->close();
}else{
die("Unable to save.");
}
}
我的进入学生信息页面
<div>
<?php
include("../connect/dbconnector.inc");
//include("../connect/PDOConnect.inc");
//include("../includes/PDOConnect.inc");
include("../functions/db_functions.inc");
include("../functions/validate_functions.inc");
include("../functions/functions.inc");
//$db = connectdb();
//opendatabase();
if(isset($_POST['submit'])){
if((isset($_POST['firstName']) && $_POST['firstName']=="") || (isset($_POST['lastName']) && $_POST['lastName']=="") || (isset($_POST['reg_id']) && $_POST['reg_id']=="") || (isset($_POST['class']) && $_POST['class']=="") || (isset($_POST['birthdate']) && $_POST['birthdate']=="") || (isset($_POST['fatherName']) && $_POST['fatherName']=="") || (isset($_POST['addrs']) && $_POST['addrs']=="") || (isset($_POST['area']) && $_POST['area']=="") || (isset($_POST['city'])&& $_POST['city']=="") || (isset($_POST['state']) && $_POST['state']=="") || (isset($_POST['country']) && $_POST['country']=="") || (isset($_POST['zip']) && $_POST['zip']=="") || (isset($_POST['phone']) && $_POST['phone']=="") || (isset($_POST['email']) && $_POST['email']=="") || (isset($_POST['more_info']) && $_POST['more_info'] == "") || (isset($_FILES['stud_image']) && $_FILES['stud_image'] =="")){
echo "<p style='width:900px;margin:0px;margin-left:auto;margin-right:auto;text-align:center;background-color:#66ff00'><font style='color:#ff0066;font-family:Arial'>all field are required</font></p>";
}else{
$fName = $_POST['firstName'];
$lName = $_POST['lastName'];
$regid = $_POST['reg_id'];
$st_class = $_POST['class'];
$dob = $_POST['birthdate'];
$fatherName = $_POST['fatherName'];
$address = $_POST['addrs'];
$st_area = $_POST['area'];
$st_city = $_POST['city'];
$st_state = $_POST['state'];
$st_country = $_POST['country'];
$st_zip = $_POST['zip'];
$st_phone = $_POST['phone'];
$st_email = $_POST['email'];
$st_xtra = $_POST['more_info'];
$image_link = $_FILES['stud_image'];
//upload the passport to students image folder and extract the image link
$filename = basename($image_link['name']);
$s_passLink = "studentspassport/".$filename;
uploadpassport($image_link,$s_passLink);
$success = enterStudent($db,$regid,$fName,$lName,$st_class,$dob,$fatherName,$address,$st_area,$st_city,$st_state,$st_country,$st_zip,$st_phone,$st_email,$s_passLink,$st_xtra);
if($success){
echo "New Student is added to the database";
}else {
echo "There is an error, please try again";
}
}
}
?>
问题是它无法将值插入数据库我不知道为什么
【问题讨论】:
-
好的,您要插入的字段在哪里?
INSERT INTO table (FIELDS) VALUES(VALUES)但我在您的声明中没有看到任何字段 -
打印 sql 错误而不是一般的“无法保存”消息通常很有帮助。
-
@MostafaTalebi 这些字段在插入语句中是可选的。
-
你没有从你的
enterStudent函数返回任何东西,所以不管它是否成功,它总是被认为是一个错误。如果确实是数据库问题,则应在enterStudent函数中打印$stmt->error_list和$stmt->errno的值,以获取有关问题的更多信息。 -
我添加了 $stmt->error 问题出在数据格式上,我现在使用 $dobirth->format('Y-m-d');格式化日期,但它给了我一个错误