2014-02-06 81 views
0

我嘗試將代碼更改爲此,因此它在連接到數據庫之前連接到其他任何服務器上,但現在它僅存在於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(); 

    }; 
     ?> 
+0

如果你回聲$查詢將被打印的內容查詢? – Hassan

+0

爲什麼你將這個函數'sprintf'添加到變量'$ query'中?我會想你會想要這樣的:'$ query =(「SELECT * FROM teachers WHERE user_name ='%s'LIMIT 1」);'在開始時執行'$ user = mysql_real_escape_string($ _ POST ['user_name']);'獲取用戶名並清理輸入字符串。 –

+0

然後將初始$查詢變量更改爲'$ query =「SELECT * FROM teachers WHERE user_name ='」。$ user。「'LIMIT 1」;' –

回答

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(count($row) > 0) 

代替:

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, 
} 
+0

同樣,不回答爲什麼沒有東西被髮送到數據庫的問題。你說的替換代碼和我原來的代碼是一樣的還是那個錯誤?我沒有得到你想要我做的事情來改變if else語句,因爲代碼是相同的。我怎樣才能簡化它,忽略字段的檢查,我只是想要註冊登錄用戶的代碼,將數據發送到數據庫,這就是現在的問題。 – buttonitup

0

如果用戶已經存在這樣的事情你應該改變你的支票只是

if($row) 
+0

同意但仍不回答問題爲什麼沒有發送到數據庫。 – buttonitup

0

如果你只想測試數據是否被插入限制你t他編寫這樣:

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

?> 
+0

我嘗試了底層代碼,並找回了一堆我無法在這裏找到的錯誤消息。嘗試在lurnn.com/signup.html註冊,你會看到所有的錯誤信息。 – buttonitup

+0

@MattMcLaren你忘了發佈錯誤。 B.T.W.我想我可能已經解決了這個錯誤。再次嘗試刪除'mysql_connect'中的雙引號。 –

+0

感謝您抽出寶貴時間來幫助我們。我曾經能夠在五秒鐘內編寫代碼,但過去一年顯然很多。 – buttonitup