我正在尝试使用mysqli将表单中的数据插入到我的MYsql表中,但每当我提交表单时,它只显示已成功连接到数据库服务器。有任何想法吗?它应该显示我的错误插入不应该吗?
<?php
if ($_POST['submit']) {
$errormsg = "";
$name = $_POST['name'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$connection = @mysqli_connect("$hostname","$username", "$password", "$database");
if(!$connection){
die("<p>The database server is not available.</p>".mysqli_error());
}
echo "<p>Sucessfully connected to the database server.</p>";
if((!$name)||(!$password)||(!$password2)||(!$email)||(!$phone)){ /* Checks if all inputs are filled*/
$errormsg = "Please insert the required fields below<br />";
if ($name == "")
{
$errormsg = $errormsg . "Enter a name.<br />";
}
if ($password == "")
{
$errormsg = $errormsg . "Please enter a password.<br />";
}
if ($password2 =="")
{
$errormsg = $errormsg . "Please re-enter your password.<br />";
}
if ($email =="")
{
$errormsg = $errormsg . "Please enter an email address.<br />";
}
if ($phone =="")
{
$errormsg = $errormsg . "Please enter a phone number.<br />";
}
if($errormsg){
echo "$errormsg";
}
}
if($password != $password2) {
$errormsg = $errormsg. "Your passwords must match!<br/>";
}
function checkEmail($email){ //begin email check function
$sql = "SELECT count(email) FROM CUSTOMER WHERE email='$email'";
$result = mysqli_result(mysqli_query($sql),0);
if( $result > 0 ){
$errormsg = $errormsg. "There is already a user with that email!";
}//end email check function
if(!$errormsg)
{
$insertquery = "INSERT INTO CUSTOMER (customer_no, name, password, email, phone)
VALUES(NULL, '$name', '$password', '$email', '$phone')";
$queryresult = mysqli_query($insertquery);
if($queryresult)
{
echo("<br>Registration sucessful");
}
else
{
echo("<br>registration was not sucessful");
}
}
}
}
?>
答案 0 :(得分:-1)
可能这可行。你可以试试这个:
<?php
if ($_POST['submit']) {
$errormsg = "";
$name = isset($_POST['name'])?$_POST['name']:NULL;
$password = isset($_POST['password'])?$_POST['password']:NULL;
$password2 = isset($_POST['password2'])?$_POST['password2']:NULL;
$email = isset($_POST['email'])?$_POST['email']:NULL;
$phone = isset($_POST['phone'])?$_POST['phone']:NULL;
//Try by removing @, since it ignores any error that may occur here while connecting to mysql
$connection = mysqli_connect("$hostname","$username", "$password", "$database");
if(!$connection)
die("<p>The database server is not available.</p>".mysqli_error());
echo "<p>Sucessfully connected to the database server.</p>";
if(empty($name)||empty($password)||empty($password2)||empty($email)||empty($phone))
{ // Checks if all inputs are filled
$errormsg = "Please insert the required fields below<br />";
if ($name == "")
$errormsg = $errormsg . "Enter a name.<br />";
if ($password == "")
$errormsg = $errormsg . "Please enter a password.<br />";
if ($password2 =="")
$errormsg = $errormsg . "Please re-enter your password.<br />";
if ($email =="")
$errormsg = $errormsg . "Please enter an email address.<br />";
if ($phone =="")
$errormsg = $errormsg . "Please enter a phone number.<br />";
if($errormsg)
echo "$errormsg";
}
if($password != $password2)
$errormsg = $errormsg. "Your passwords must match!<br/>";
function checkEmail($email)
{ //begin email check function
$sql = "SELECT count(email) FROM CUSTOMER WHERE email='$email'";
$result = mysql_result(mysqli_query($connection,$sql),0);//see the difference
if( $result > 0 )
$errormsg = $errormsg. "There is already a user with that email!";
}//end email check function
if(!$errormsg)
{
$insertquery = "INSERT INTO CUSTOMER (customer_no, name, password, email, phone)
VALUES(NULL, '$name', '$password', '$email', '$phone')";
$queryresult = mysqli_query($connection,$insertquery);//see the difference
if($queryresult)
echo("<br>Registration sucessful");
else
echo("<br>registration was not sucessful");
}
}
}
?>
您可以从here了解更多信息。享受!