0

我正在尝试使用 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");
        }
    }
}
}
?>
4

1 回答 1

-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");
}
}
}
?>

您可以从这里了解更多信息。享受!

于 2013-09-28T16:49:20.990 回答