为什么我的数据没有插入数据库表?

时间:2013-06-25 21:55:27

标签: php mysql

我没有看到问题。我的数据都不会插入注册数据库。请检查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); 


} 


?>

1 个答案:

答案 0 :(得分: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.";
}

如果您仍然遇到问题,我希望看到您提交到此页面的表单以及 注册 的表格布局。< / p>