2013-10-21 182 views
0

我看到我的腳本沒有錯,我不明白爲什麼它不會插入到我創建的數據庫中。請看看我的代碼,並告訴可能是錯的。 這是它被用於所有的數據庫連接插入查詢未插入數據庫

<?php 
$host="localhost"; 
$user="root"; 
$password=""; 
$db="cottonboard"; 
$tablename="user"; 
mysql_connect($host,$user,$password) or die(mysql_error()); 
mysql_select_db($db) or die(mysql_error()); 
?> 

這是現在是將輸入插入到數據庫腳本中dbcon.php,

<?php 

include "dbcon.php"; 


extract($_POST); 


/* just to test if all fields will be outputted 
print_r($_POST);*/ 

$sql="INSERT INTO $tablename(id,username,password,accesslevel,sex,contactnmuber,employer) 
    VALUES('null','$usernameuser','$passworduser','$accesslevel','$gender','$contactnumberuser','$employeruser')" or die(mysql_error()); 

if($sql) 
{ 
echo"we got here safe"; 
} 

?> 
+1

那麼你在哪裏執行查詢?注意:避免使用'mysql_'函數,它們已被棄用 –

+3

您正在'$ sql'中創建一個查詢,但您從未真正執行它。 – 2013-10-21 10:03:55

+0

加入 mysql_query($ sql);問題已經解決了。謝謝 – Inventor

回答

0
// ... 
$c = mysql_connect($host,$user,$password) or die(mysql_error()); 
// ... 
mysql_query($sql, $c); 
0
$sql="INSERT INTO $tablename(id,username,password,accesslevel,sex,contactnmuber,employer) VALUES('null','$usernameuser','$passworduser','$accesslevel','$gender','$contactnumberuser','$employeruser')"; 

$mysqli = new mysqli($host, $user, $password, $db); 

if ($mysqli->query($sql) === TRUE) { 
    echo"we got here safe"; 
} 

見另外:http://www.php.net/manual/en/mysqli.query.php