注册表单数据未显示在DB中

时间:2014-02-06 14:34:43

标签: php mysql sql

我尝试将代码更改为此,因此它先连接到数据库,但现在它只是在verify.php上存在,没有重定向,没有数据发送到数据库。

        <?php 
if(isset($_POST['submit'])){ 
 # connect to the database here
$host="XXXXXXX"; // Host name 
   $username="XXXX"; // Mysql username 
   $password="XXXX"; // Mysql password 
  $db_name="XXXX"; // Database name 

   mysql_connect("$host", "$username", "$password")or die("cannot connect for insert"); 
    mysql_select_db("$db_name")or die("cannot select DB to insert data");

    $user_name = mysql_real_escape_string($_POST['user_name']);
    $fname =    mysql_real_escape_string($_POST['fname']);
     $lname =     mysql_real_escape_string($_POST['lname']); 
    $email =     mysql_real_escape_string($_POST['email']);
     $user_password =    mysql_real_escape_string($_POST['password']); 

     $insert_query = "INSERT INTO teachers(`user_name`,`fname`,`lname`,`email`,`password`) 
    VALUES('".$user_name."$','".$fname."','".$lname."','".$email."','".$user_password."');";
    mysql_query($insert_query) or die(mysql_error()); 
    mysql_close();

    };
      ?> 

3 个答案:

答案 0 :(得分:1)

您应该将or die(mysql_error());放在以下行中:

$sql = mysql_query($query) or die(mysql_error());

而不是:

mysql_real_escape_string($_POST['password']))or die(mysql_error());

另一件事是你有错误的if-else陈述。

您可以使用以下if语句检查哪个字段为空的代码:

if($row||empty($_POST['user_name'])|| empty($_POST['fname'])||empty($_POST['lname'])|| empty($_POST['email'])||empty($_POST['password'])|| empty($_POST['re_password'])||$_POST['password']!=$_POST['re_password']){ 
# if a field is empty, or the passwords don't match make a message
# YOU SHOULD PUT YOUR CODE TO CHECK EMPTY FIELDS SEPARATELY HERE
}
else { 
    # If all fields are not empty, and the passwords match, 
}

答案 1 :(得分:0)

如果用户已经存在,您应该更改支票:

if(count($row) > 0)

而不仅仅是

if($row)

答案 2 :(得分:0)

如果您只想测试是否插入了数据,请将代码限制为:

        <?php 
        if(isset($_POST['submit'])){ 

    $host=""; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name="lurnn"; // Database name 

    /* sanitize post variables */    
    $user_name = mysql_real_escape_string($_POST['user_name']);
    $fname =    mysql_real_escape_string($_POST['fname']);
    $lname =     mysql_real_escape_string($_POST['lname']); 
    $email =     mysql_real_escape_string($_POST['email']);
    $user_password =   mysql_real_escape_string($_POST['password']); 

    /* Database insert query */
mysql_connect($host, $username, $password)or die("cannot connect for insert"); 
mysql_select_db($db_name)or die("cannot select DB to insert data");
$insert_query = "INSERT INTO teachers(`user_name`,`f_name`,`l_name`,`email`,`password`) 
VALUES('".$user_name."','".$fname."','".$lname."','".$email."','".$user_password."')";
mysql_query($insert_query) or die(mysql_error()); 
mysql_close();


        };
            ?>

MYSQLI_版本:

    <?php 


if(isset($_POST['submit'])){ 
    $host = "host";
    $user = "user";
    $password = "password";
    $database = "database";   


    /* sanitize post variables */    
    $user_name = mysql_real_escape_string($_POST['user_name']);
    $fname =    mysql_real_escape_string($_POST['fname']);
    $lname =     mysql_real_escape_string($_POST['lname']); 
    $email =     mysql_real_escape_string($_POST['email']);
    $user_password =   mysql_real_escape_string($_POST['password']); 


    // open connection to database
    $link = mysqli_connect($host, $user, $password, $database);
        IF (!$link){
            echo ("Unable to connect to database!");
        }
        ELSE {
           //INSERT VALUES INTO DATABASE
           $query = "INSERT INTO teachers(`user_name`,`f_name`,`l_name`,`email`,`password`) 
VALUES('".$user_name."','".$fname."','".$lname."','".$email."','".$user_password."')";
         mysqli_query($link,$query)  or die(mysql_error());
         echo var_dump($query);
        }
//close connection to database
        mysqli_close($link);

    };


    ?> 

如果全部失败,请尝试以下操作:

    <?php


function submit_form(){
    $host = "";
    $user = "";
    $password = "";
    $database = "";   

    /* sanitize post variables */    
    $user_name = mysql_real_escape_string($_POST['user_name']);
    $fname =    mysql_real_escape_string($_POST['fname']);
    $lname =     mysql_real_escape_string($_POST['lname']); 
    $email =     mysql_real_escape_string($_POST['email']);
    $user_password =   mysql_real_escape_string($_POST['password']);


    // open connection to database
    $link = mysqli_connect($host, $user, $password, $database);
        IF (!$link){
            echo ("Unable to connect to database!");
        }
        ELSE {
           //INSERT VALUES INTO DATABASE
           $query = "INSERT INTO teachers(`user_name`,`f_name`,`l_name`,`email`,`password`) 
VALUES('".$user_name."','".$fname."','".$lname."','".$email."','".$user_password."')";
         mysqli_query($link,$query)  or die("Insert query failed");
         echo var_dump($query);

        }
//close connection to database
        mysqli_close($link);

    }


$form = <<<EODuserform
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Form</title>
    </head>
        <form action="{$_SERVER['PHP_SELF']}" method="POST" name="userform">
            <label for='user_name'>Username:</label></br>
            <input type="text" name="user_name" id="first" maxlength="25" tabindex='1' VALUE="user_name" /></br>
            <label for='fname'>First Name:</label></br>
            <input type="text" name="fname" id="first" maxlength="25" tabindex='2' VALUE="firstname" /></br>
            <label for='lname'>Last Name:</label></br>
            <input type="text" name="lname" id='lastname' maxlength="25" tabindex='3' VALUE="lastname" /></br>
            <label for='email'>E-mail:</label></br>
            <input type="text" name="email" id='email' maxlength="100" tabindex='4' VALUE="email" /></br>
            <label for='password'>Password:</label></br>
            <input type="password" name="password" id='password' maxlength="25" tabindex='5' VALUE="password" /></br>
            <label for='re-password'>Re-type password:</label></br>
            <input type="password" name="re-password" id='re-password' maxlength="25" tabindex='6' VALUE="re-password" /></br>
            <input type="submit" name="submit" value="Sign Up" tabindex='6' />
            </form>
    </body>
</html>
EODuserform;


IF(!IsSet($_POST['submit'])){ // Check if form is not send, if not display empty form.

    echo $form;
}

ELSE{
// in the case you want to send something to the database use 
submit_form();
echo ('Thanks for submitting your form');
}

?>