我用HTML创建了一个表单,然后创建了一个表单,用于将信息发送到mySQL数据库。但是,信息不会显示在数据库中。我做错了什么?
这是我的HTML:
<form action="#" method="post">
<input type="text" name="fname" placeholder="First Name" class="sign-up-input" value="<?php echo $fn; ?>"><br>
<input type="text" name="lname" placeholder="Last Name" class="sign-up-input" value="<?php echo $ln; ?>"><br>
<input type="text" name="username" placeholder="Username" class="sign-up-input" value="<?php echo $un; ?>"><br>
<input type="text" name="email" placeholder="E-mail" class="sign-up-input" value="<?php echo $em; ?>"><br>
<input type="text" name="email2" placeholder="Repeat E-mail" class="sign-up-input" value="<?php echo $em2; ?>"><br>
<input type="password" name="password" placeholder="Password" class="sign-up-input"><br>
<input type="password" name="password2" placeholder="Repeat Password" class="sign-up-input"><br>
<input type="submit" name="reg" value="Sign Up">
</form>
这是我的PHP:
<?php include ("inc/scripts/mysql_connect.inc.php");
$reg = @$_POST['reg'];
//declaring variable to prevent errors
$fn = ""; //first name
$ln = ""; //last name
$un = ""; //username
$em = ""; //email
$em2 = ""; // email repeated
$pswd = ""; //password
$pswd2 = ""; //pasword repeated
$u_check = ""; //check username
$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) {
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
$check = mysql_num_rows($u_check);
$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) {
if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2) {
if ($pswd == $pswd2) {
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "<br><br><br><br>The maximum limit for username/first name/last name is 25 characters!";
} else {
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "<br><br><br><br>Your password must be between 5 and 30 characters long!";
} else {
$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("Welcome to findFriends Login to your account to get started...");
}
}
} 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!";
}
}
?>
这是我的mySQL连接代码:
<?php
mysql_connect("localhost","root","", "socialnetwork") or die("no such user exists!");
mysql_select_db("socialnetwork") or die("no such database exists!");
echo "Connected to database";
?>
我的网站没有显示任何错误,但数据库没有显示任何用户。我做错了什么?
*免责声明 - 我在youtube教程中找到了此代码。当他这样做时,它起作用了。下面是视频:http://www.youtube.com/watch?v=cCr9TthDtxk
答案 0 :(得分:0)
试试这个。
//change this
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')");
//to
$query = mysql_query("INSERT INTO users (username, fname, lname, email, password, date, about) VALUES ('$un','$fn','$ln','$em','$pswd','$d','Write something about yourself.')");
而不是为字段添加所有数据。除了表名之外定义你的字段,然后设置值对应于定义要插入的字段。
另外,在数据库中将你的id设置为“自动增量”