2013-01-02 45 views
0

我在PHP編寫腳本插入用戶到一個表在數據庫中。這裏是代碼:插入行到MySQL數據庫從註冊頁面

<?php 
$email  = check_input($_POST['EmailAddress'], "Enter your email address"); 
$pass  = check_input($_POST['Password'], "Enter a password"); 
$first_name = check_input($_POST['FirstName'], "Enter your first name"); 
$last_name = check_input($_POST['LastName'], "Enter your last name"); 
$gender  = check_input($_POST['Gender']); 
$dobmonth = check_input($_POST['dobmonth']); 
$dobday  = check_input($_POST['dobday']); 
$dobyear = check_input($_POST['dobyear']); 
$dob  = $dobmonth.'/'.$dobday.'/'.$dobyear; 
$phone  = check_input($_POST['CellPhone']); 
$fanmail = check_input($_POST['FanMail']); 

if (verify($email) === true) { 

從這裏編輯:

$link = new mysqli("localhost","263764","Corvette88", "256764 "); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} else { 
    echo 'connected to db table '; 
} 
echo $mysqli->host_info . "\n"; 

$query = "INSERT INTO 68_users VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')" or die(mysql_error()); 
if (!mysqli_query($link, $query)) { 
echo 'error: '.mysqli_error($link); 
exit(); 
} 

echo "1 record added "; 

mysqli_close($link); 

到這裏..

echo"connection closed"; 
} else {echo'unverifiable email address '; 
    echo $email; 
} 


function check_input($data, $problem='') { 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) { 
    die($problem); 
} 
return $data; 
} 

function conntodb() { 

} 

function verify($email){ 
$isValid = true; 
$atIndex = strrpos($email,'@'); 
if (is_bool($atIndex) && !$atIndex) { 
    return false; 
} else { 
    $domain = substr($email,$atIndex+1); 
    $local = substr($email, $atIndex); 
} 
$localLen = strlen($local); 
$domainLen = strlen($domain); 
if($localLen < 1 || $localLen > 64) { 
    return false; 
}else if ($domainLen < 1 || $domainLen > 255) { 
    return false; 
} 
if ($local[0] == '.' || $local[$localLen-1] == '.') { 
    return false; 
} elseif (preg_match('/\\.\\./', $local)) { 
    return false; 
} 
if(!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { 
    return false; 
} else if (preg_match('/\\.\\./', $domain)) { 
    return false; 
} else if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain, "A"))) { 
    return false; 
} 
return $isValid; 
} 

?> 

我已經設置了檢查點的代碼來顯示,如果它出現了錯誤,並如果成功。

我的問題是:它告訴我,它已經成功地將行到數據庫,但是當我檢查我的數據的基礎上它沒有顯示爲以任何方式被填充到數據庫中。我不知道我的錯在哪裏,因爲它通過我的檢查點..任何想法?

回答

3

你不檢查你的查詢錯誤:

mysql_query ("INSERT INTO 68_users (email, pass, first_name, last_name, dob, gender,  phone, fanmail) 
VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')"); 

而是執行此操作:

mysql_query ("INSERT INTO 68_users (email, pass, first_name, last_name, dob, gender,  phone, fanmail) 
VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')") or die(mysql_error()); 

此外,你應該甚至不使用mysql_ *。改爲使用PDO或MySQLi。見this

嘗試改變

mysqli_query($link, $query); 

到:

if (!mysqli_query($link, $query)) 
{ 
    echo 'error: '.mysqli_error($link); 
    exit(); 
} 
+0

什麼叫 「mysql_ *」 是什麼意思? –

+0

你知道你是如何使用'的mysql_connect()','的mysql_query()',ECT ECT?那麼,這些功能已被棄用。你應該使用PDO或MySQLi。如果您不喜歡OOP,請使用MySQLi。 – 2013-01-02 20:07:42

+0

'mysql_ *'意味着 「所有的啓動'mysql_'的功能」。 –