所以我在这里遇到了一些问题。我正在尝试做一些非常基本的事情,即通过 Apache localhost 网站将数据存储到 MySQL 数据库中。在我的网站上发送数据后,它并没有存储到数据库中。我会先发布我的代码,然后再给你一些更多的信息:
标题中的代码:
<?php
$link=mysqli_connect("localhost","root","mypassword","database") or die("Couldn't connect to DB");
?>
代码:
<?php include ( "./inc/header.inc.php" ); ?>
<?php
$reg = @$_POST['reg'];
// declaring variables to prevent errors
$fn = ""; // First Name
$ln = ""; // Last Name
$un = ""; // Username
$em = ""; // Email
$emc = ""; // Emailcheck
$pswd = ""; // Password
$pswdc = ""; // Passwordcheck
$d = ""; // Signup Date
$u_check = ""; // Check if username exists
// registration form
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$emc = strip_tags(@$_POST['emailcheck']);
$pswd = strip_tags(@$_POST['password']);
$pswdc = strip_tags(@$_POST['passwordcheck']);
date_default_timezone_set('Europe/Berlin');
$d = date("Y-M-D"); // Year - Month - Day
if ($reg) {
// Check if emails are equal
if ($em==$emc) {
// Check if user already exists
$u_check = mysqli_query($link, "SELECT username FROM users WHERE username= '$un'");
// Count the amount of rows where username = $un
$check = mysqli_num_rows($u_check);
if ($check == 0) {
// Check all of the fields have been filled in
if ($fn&&$ln&&$un&&$em&&$emc&&$pswd&&$pswdc) {
// Check that passwords match
if ($pswd==$pswdc) {
// Check the max length of username/first name/last name does not exeed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "The maximum limit for username/first name/last name is 25 characters!";
} else {
// Check the max length of password does not exeed 25 characters and is not less than 5 characters
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Your password must be between 5 and 30 characters long. So should your ######!";
} else {
// encrypt password and passwordcheck using md5 before sending to database
$pswd = md5($pswd);
$pswdc = md5($pswdc);
$query = mysqli_query($link, "INSERT INTO users (id, username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
die("<h2>Welcome to #######</h2>Login to your account to get ######!");
}
}
} else {
echo "Your passwords don't match.";
}
} else {
echo "Please fill in all of the fields.";
}
} else {
echo "This Username already exists.";
}
} else {
echo "Your Emails don't match.";
}
}
?>
<div id="wrapper2">
<table>
<tr>
<td width="60%" valign="top">
<h2>Join ####### today!</h2>
</td>
<td width="40%" valign="top">
<h2>Sign up below.</h2>
<form action="#" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name" /><br /><br />
<input type="text" name="lname" size="25" placeholder="Last Name" /><br /><br />
<input type="text" name="username" size="25" placeholder="User Name" /><br /><br />
<input type="text" name="email" size="25" placeholder="E-Mail" /><br /><br />
<input type="text" name="emailcheck" size="25" placeholder="E-Mail Check" /><br /><br />
<input type="text" name="password" size="25" placeholder="Password" /><br /><br />
<input type="text" name="passwordcheck" size="25" placeholder="Password Check" /><br /><br />
<input type="submit" name="reg" value="Sign Up!">
</form>
</td>
</tr>
</table>
<?php include ( "./inc/footer.inc.php" ); ?>
因此与数据库的连接正常。我已经检查过了。数据正确存储在变量中。我通过 phpMyAdmin 在数据库中手动插入了一个“测试”用户名,如果我在我的网站中放置了相同的用户名,代码会正确地从数据库中获取用户名并将其与输入进行比较(因此结果是“用户名已经存在”) .就像我已经说过的,当我用数据正确填写表单时,代码会终止,但数据不会存储到数据库中。
这里是一些版本信息: Apache/2.4.16 (Unix) PHP/5.5.29
基本上使用本指南设置 Apache、PHP、MySQL、phpMyAdmin: Guide for Mac OSX
我通过 die(mysqli_error($link));
检查了错误,并得到了这个响应:
Incorrect integer value: '' for column 'id' at row 1 ->Changed argument '' to NULL.
第二次错误检查给了我这个:
Incorrect date value: '2015-Nov-Wed' for column 'sign_up_date' at row 1
NOW()
方法起到了作用。感谢您指出日期错误:应该是 $d = date("Y-m-d");
最佳答案
试试这个:
INSERT INTO users (username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('$un','$fn','$ln','$em','$pswd','$d','0')`
如果您的 id
列是自动递增的,则您不需要自己填写。
关于你的约会,你可以查看这个 link供日期引用。 date
结构化列需要YY-MM-DD
格式,可以通过:
$d = date("Y-m-d");
您系统中的用户可以上传视频吗? :P
关于php - 无法将数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908105/