2013-06-25 295 views
-4

我看不到問題。我的數據都不會插入到註冊數據庫中。請檢查INSERT INTO部分。我是編碼方面的初學者,所以我不知道這些東西。請幫幫我。爲什麼我的數據沒有插入數據庫表中?

<?php 

**my info is here** 

//Connect to database 

mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); 
mysql_select_db($dbname) or die(mysql_error()); 
?> 


<?php 
if(isset($_POST['submit'])){ 
$username = $_POST[user]; 
$password = $_POST[pass]; 

// lets check to see if the username already exists 

$checkuser = mysql_query("SELECT username FROM register WHERE username='$username'"); 

$username_exist = mysql_num_rows($checkuser); 

if($username_exist > 0){ 
echo "I'm sorry but the username you specified has already been taken.Please pick  another one."; 
unset($username); 
exit(); 
} 

// lf no errors present with the username 
// use a query to insert the data into the database. 

$query = ("INSERT INTO register (username, password) 
VALUES ('$username', '$password')"); 
mysql_query($query) or die(mysql_error()); 
mysql_close($query); 


} 


?> 
+3

你有什麼錯誤? –

+4

您正在將原始密碼存儲在數據庫中,而不是轉義輸入或使用參數化查詢。這已經不是一個好的開始。您應該首先通過使用PDO來解決這個問題! – Ryan

+2

你真的不應該使用任何'mysql_ *'函數,而是使用MySQLi或PDO。 –

回答

0

提意見是正確的,你應該使用庫MySQLi或PDO,但嘗試了這一點,看看它是否解決您的問題:

if(isset($_POST)){ 
    $username = mysql_real_escape_string($_POST['user']); 
    $password = mysql_real_escape_string($_POST['pass']); 

    // lets check to see if the username already exists 

    $checkuser = mysql_query("SELECT username FROM register WHERE username='$username'"); 

    $username_exist = mysql_num_rows($checkuser); 

    if($username_exist > 0){ 
     echo "I'm sorry but the username you specified has already been taken. Please pick another one."; 
     unset($username); 
     exit(); 
    } 

    // if no errors present with the username 
    // use a query to insert the data into the database. 

    $query = ("INSERT INTO register (username, password) VALUES ('$username', '$password')"); 
    mysql_query($query) or die(mysql_error()); 
    mysql_close(); 
} else { 
    echo "No form submission detected."; 
} 

如果您仍然有問題,我會喜歡看你提交給這個頁面的表格以及的表格佈局

相關問題