为什么PHP不会写入MySQL服务器?

时间:2016-11-18 03:31:03

标签: php html mysql database

所以我创建了一个带登录系统的网站,但我的PHP不会将任何内容导入MySQL。

这是我的代码:

注册:

<?php require_once './template/header.php'; ?>
<?
    $reg = @$_POST['reg'];

    //registration form
    $fn = strip_tags(@$_POST['fname']);
    $ln = strip_tags(@$_POST['lname']);
    $un = strip_tags(@$_POST['username']);
    $em = strip_tags(@$_POST['email']);
    $em2 = strip_tags(@$_POST['email2']);
    $pswd = strip_tags(@$_POST['password']);
    $pswd2 = strip_tags(@$_POST['password2']);
    $d = date("Y-m-d"); // Year - Month - Day

    if ($reg) {
    if ($em==$em2) {
    // Check username
    $u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
    $check = mysql_num_rows($u_check);
    // Check email
    $e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
    $email_check = mysql_num_rows($e_check);
    if ($check == 0) {
      if ($email_check == 0) {
    // are rows filled in
    if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
    // passwords match?
    if ($pswd==$pswd2) {
    // how long are names
    if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
    echo "Your names are too long!";
    }
    else
    {
    // check the maximum length of password does not exceed 25 characters and is not less than 5 characters
    if (strlen($pswd)>16||strlen($pswd)<6) {
    echo "Passwords must be between 6 and 16 characters!";
    }
    else
    {
    //encrypt password
    $pswd = md5($pswd);
    $pswd2 = md5($pswd2);
    $query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')");
    die("<h2>Welcome to our community!</h2>Login to your account to begin your journy...");
    }
    }
    }
    else {
    echo "Your passwords don't match!";
    }
    }
    else
    {
    echo "Please fill in all of the fields";
    }
    }
    else
    {
     echo "Sorry, but it looks like someone has already used that email!";
    }
    }
    else
    {
    echo "Username already taken ...";
    }
    }
    else {
    echo "Your E-mails don't match!";
    }
    }
?>
<div class="container" style="width:1000px; margin:0 auto; padding-top:25px;">
    <div id="home">
    <table>
        <tr>
        <td width="50%" valign="top">
            <h2>Welcome back! Login here!</h2>
            <input type="text" name="email" size="25" placeholder="E-Mail Address" /><br /><br />
            <input type="password" name="pass" size="25" placeholder="Password" /><br /><br />
        </td>
        <td width="50%" valign="top">
            <h2>Welcome! Register for free!</h2>
            <form action="#" method="post">
                <input type="text" size="40" name="fname"  class="auto-clear" title="First Name" placeholder="First Name" value="<? echo $fn; ?>"><p />
                <input type="text" size="40" name="lname" class="auto-clear" title="Last Name" placeholder="Last Name" value="<? echo $ln; ?>"><p />
                <input type="text" size="40" name="username" class="auto-clear" title="Username" placeholder="Username" value="<? echo $un; ?>"><p />
                <input type="text" size="40" name="email" class="auto-clear" title="Email" placeholder="Email Address" value="<? echo $em; ?>"><p />
                <input type="text" size="40" name="email2" class="auto-clear" title="Repeat Email" placeholder="Email Address" value="<? echo $em2; ?>"><p />
                <input type="password" size="40" name="password" value="Password ..."><p />
                <input type="password" size="40" name="password2" value="Password ..."><p />
                <input type="submit" name="reg" value="Sign Up!">
            </form>
            <!-- <select name="gender">
                <option value="" disabled selected>Gender</option>
                <option value="male">Male</option>
                <option value="female">Female</option>
            </select><br /><br /> -->
        </td>
        </tr>
    </table>
    </div>
</div>

<?php require_once './template/footer.php'; ?>

连接:

<?php
mysql_connect("localhost","root","");
mysql_select_db("HBN") or die("Couldn't connect to database!")
?>

提前谢谢你们!

1 个答案:

答案 0 :(得分:0)

您需要告诉SQL要将值添加到哪些表列。所以,而不是写

"INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')"

你想写

"INSERT INTO users (col1,col2,col3,col4,col5...) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')"