0

mysqli を使用して MYsql テーブルに作成したフォームからデータを挿入しようとしていますが、フォームを送信するたびに、正常に接続された db サーバーが表示されます。何か案は?挿入のエラーが表示されるはずですよね?

<?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 に答える