数据未插入数据库

时间:2015-06-04 04:03:03

标签: php mysql

我正在尝试创建一个注册表单并将它们插入到数据库中。我根据我的知识编写了它,但它显示用户已经退出但我的数据库中没有记录。从我的注册表单

if (isset($_POST['btn_signup'])) {

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$usernme = $_POST['username'];
$pass = $_POST['password'];
$email = $_POST['email'];
$country = $_POST['country'];
$dropdown = $_POST['dropdown'];


$query = "INSERT INTO tbl_users (first_name,last_name,username,password,email,country,user_type)VALUES('{$fname}','{$lname}','{$usernme}','{$pass}','{$email}','{$country}','{$dropdown}')";

$result = mysql_query($query, $con);


if ($result) {

    echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
} else {

    echo "<script type='text/javascript'>alert('User already exist!')</script>";
}

}

表单是

         <form action="signup.php" method="post" class="form">
                <input type="text" name="fname" placeholder="First Name" required="First Name"><br/>
                <input type="text" name="lname" placeholder="Last Name" required="Last Name"><br/>
                <input type="text" name="username" placeholder="Username" required="Username"><br/>
                <input type="password" name="password" placeholder="Password" required="Password"><br/>
                <input type="email" name="email" placeholder="Email" required="Email"><br/>
                <input type="text" name="country" placeholder="Country" required="Country"><br/>

               <select class="wrapper-dropdown" name="dropdown">
                    <option value=1>Player</option>
                    <option value=2>Recruiters</option>


                </select>
                <br/> <br/> <br/>
                <button type="submit" name="btn_signup">Signup</button>
                <p>Get in to your account <a href="index.php">Signin</a></p>
            </form>

数据库按此顺序排列。 users_id,名字,姓氏,用户名,密码,电子邮件,国家,USER_TYPE

请帮助!!

4 个答案:

答案 0 :(得分:0)

您的查询有错误...

$query = "INSERT INTO tbl_users (first_name,last_name,username,password,email,country,user_type)VALUES('{$fname}','{$lname}','{$usernme}','{$pass}','{$email}','{$country}','{$dropdown}')";

它应该是{username}而不是{usernme}

由于rp低,我无法发表评论。如果错字不是问题,我会删除这个答案

答案 1 :(得分:0)

试试这段代码。它会起作用。

$query = "INSERT INTO tbl_users (first_name,last_name,username,password,email,country,user_type)
    VALUES('$fname','$lname','$usernme','$pass','$email','$country','$dropdown')";

$result = mysql_query($query, $con);


if ($result) { 
    echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
} else {
    echo "<script type='text/javascript'>alert('User already exist!')</script>";
}

答案 2 :(得分:0)

请尝试解决您的问题:

代码: -

if(isset($_REQUEST['btn_signup']) && trim($_REQUEST['btn_signup']) == "Signup")
{   
    $fname = addslashes(trim($_REQUEST['fname']));
    $lname = addslashes(trim($_REQUEST['lname']));
    $username = addslashes(trim($_REQUEST['username']));    
    $password = addslashes(trim($_REQUEST['password']));    
    $email = addslashes(trim($_REQUEST['email']));
    $country = addslashes(trim($_REQUEST['country']));
    $dropdown = addslashes(trim($_REQUEST['dropdown']));


    $query = "INSERT INTO tbl_users (`first_name`,`last_name`, `username`, `password`, `email`, `country`, `user_type`)
        VALUES ('".$fname."', '".$lname."','".$username."','".$password."','".$email."','".$country."', '".$dropdown."')";
    $result = mysql_query($query, $con);
    if ($result) {

        echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
    } else {

        echo "<script type='text/javascript'>alert('User already exist!')</script>";
    }
}

您的表格在这里: -

<form action="signup.php" name="signup_form" id="signup_form" method="post" class="form" enctype="multipart/form-data">
    <input type="text" name="fname" placeholder="First Name" required="First Name"><br/>
    <input type="text" name="lname" placeholder="Last Name" required="Last Name"><br/>
    <input type="text" name="username" placeholder="Username" required="Username"><br/>
    <input type="password" name="password" placeholder="Password" required="Password"><br/>
    <input type="email" name="email" placeholder="Email" required="Email"><br/>
    <input type="text" name="country" placeholder="Country" required="Country"><br/>

    <select class="wrapper-dropdown" name="dropdown">
        <option value=1>Player</option>
        <option value=2>Recruiters</option>
    </select>
    <br/> <br/> <br/>
    <button type="submit" name="btn_signup" id="btn_signup" value="Signup">Signup</button>
    <p>Get in to your account <a href="index.php">Signin</a></p>
</form>

答案 3 :(得分:0)

基于此评论:

  

我尝试手动插入值,然后显示不正确的整数值:&#39;&#39;对于列&t; tbl_health_health_id&#39;在第1行

这条评论:

  

我与tbl_users和tbl_bowlAverg建立了关系

您的数据库中有触发器,存储过程或函数。

最简单的检查方式是&#34; SHOW TRIGGERS&#34; (https://dev.mysql.com/doc/refman/5.0/en/show-triggers.html)或&#34;显示程序状态&#34; (https://dev.mysql.com/doc/refman/5.0/en/show-procedure-status.html)或&#34;显示功能状态&#34;

触发器(或触发器将调用的过程)将是

CREATE TRIGGER triggerName BEFORE INSERT ON tbl_users
-> FOR EACH ROW INSERT INTO tbl_bowlAverg(fields) values (values);

第二个查询导致您遇到问题:尝试单独运行它以单独调试该位。

您的问题是tbl_health_health_id将被设置为整数而不是null,但您要么尝试插入字符串(在这种情况下,修改您的触发器)或null(在这种情况下)在插入字符串中指定一个值,在表模式中指定一个默认值,或允许空值。

一个提示:除非您有充分的理由使用空值,否则请始终为您的字段指定默认值,除非您绝对确定要提供一个。