为什么我的数据没有插入到数据库表中? [英] Why isn't my data inserted into the DB table?

查看:49
本文介绍了为什么我的数据没有插入到数据库表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到问题.我的任何数据都不会插入到注册数据库中.请检查插入部分.我是编码初学者,所以我不知道很多这些东西.请帮帮我.

I don't see the problem. None of my data will insert into the registration database. Please check the INSERT INTO part. I'm a beginner at coding so I don't know alot of this stuff. Please help me.

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


} 


?>

推荐答案

评论者认为您应该使用 mysqli 或 PDO 是正确的,但请尝试一下,看看它是否能解决您的问题:

The commenters are correct that you should be using mysqli or PDO, but try this out to see if it solves your issue:

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.";
}

如果您仍有问题,我想查看您提交到此页面的表单以及用于注册的表格布局.

If you're still having issues, I'd like to see the form you're submitting to this page as well as the table layout for register.

这篇关于为什么我的数据没有插入到数据库表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆