我知道了所有的工作,感谢所有的帮助,并指引我朝着正确的方向发展。PHP不会将表单数据插入到MySQL数据库中
我有一个Web主机,它有MySQL和phpMyAdmin。下面的代码显示了我的.php文件中的PHP。当我提交表单时,数据不会存储在SQL数据库中。
我的数据库是一个名为'登录'的表。它有三个字段:'userID'是一个自动增量类型,'email'是一个VARCHAR,'password'也是一个VARCHAR。
我测试了我的连接,它确实工作,这意味着代码中的某些内容是错误的,但我无法找到它。如果你们能帮助我,我将不胜感激。
这是我的代码:
<form action="index.php" method="post">
<p>Email Address:</p>
<input type="text" name="email" required autofocus pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$" placeholder="Please enter your email">
<p>Password:</p>
<input type="text" name="password" required placeholder="Enter your password">
<p>Confirm Password:</p>
<input type="text" name="confirmpassword" required placeholder="Confirm your password">
<br>
<input class="button" type="submit" value="Register">
</form>
<?php
if($_POST['submit']=="Submit")
{
$email = cleanData($_POST['email']);
$password = cleanData($_POST['password']);
$message = "wrong answer";
addData($email, $pasword);
}
function cleanData($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = strip_tags($data);
return $data;
}
function addData ($email, $password)
{
//include("dbinfo.php");
$dbhost = 'mysql11.000webhost.com';
$dbuser = '************'; //censored for security
$dbpass = '******'; //censored for security
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass");
if(! $conn)
{
die('Could not connect: ' . mysql_error());
}
$sql="INSERT INTO Logins ('userID','email','password') VALUES (null, '$email', '$password')";
$result=mysql_query($sql) or die(mysql_error());
}
?>
登录表是否允许userID字段中有空值? – Russ
userID是主键,我已将它设置为自动增量,因此每次添加新记录时,都会将其增加1.因此,不需要指定用户ID – donfromeway
然后不要指定它。您的代码尝试插入空值,而不是让它默认为下一个自动增量值。 – Russ