Mysqli-PHP从数据库收集数据
我试图从HTML表单收集信息,然后将数据传输到我的sql数据库。我正在使用Zampp phpmyadmin 单击“提交”时,输入的数据将消失,但不会弹出“成功”弹出窗口。没有错误。它只是没有被发送到数据库。在第三个IF语句之后的ELSE语句不起作用-将数据传输到数据库-这在第二个代码文件中。我不知道为什么 此代码是索引页Mysqli-PHP从数据库收集数据,php,mysql,mysqli,Php,Mysql,Mysqli,我试图从HTML表单收集信息,然后将数据传输到我的sql数据库。我正在使用Zampp phpmyadmin 单击“提交”时,输入的数据将消失,但不会弹出“成功”弹出窗口。没有错误。它只是没有被发送到数据库。在第三个IF语句之后的ELSE语句不起作用-将数据传输到数据库-这在第二个代码文件中。我不知道为什么 此代码是索引页 <!DOCTYPE html> <?php include("functions/functions.php"); ?> <html>
<!DOCTYPE html>
<?php
include("functions/functions.php");
?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" media="all"/>
</head>
<body>
<div class = "container">
<div id="head_wrap">
<div id="header">
<!-- logo goes here -->
<img src="images/logo.png"/ style="float:left"/>
<form method="post" action="" id="form1">
<strong>Email:</strong>
<input type="email" name="email" placeholder="Email" required="required"/>
<strong>Password:</strong>
<input type="password" name="pass" placeholder="*****" required="required"/>
<!-- the submit can be a button -->
<button name="login">Login</button>
</form>
</div>
</div>
<!-- this is where the second form and picture comes in on
main page -->
<div id="content">
<!-- second picture-->
<div>
<img src="images/image.png" style="float:left; margin-left:-40px;"/>
</div>
<div id="form2">
<form action="" method="post">
<h2>Sign Up Here</h2>
<table>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="u_name" placeholder="Enter your
name" required="required"/>
</td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="u_pass" placeholder="Enter your
password" required="required"/>
</td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input type="email" name="u_email" placeholder="Enter your
email" required="required"/>
</td>
</tr>
<tr>
<!-- this is where they will pick their state -->
<td align="right">Country:</td>
<td>
<select name="u_country"required="required">
<option>Select a country</option>
<option>USA</option>
<option>Afghanistan</option>
<option>India</option>
</select>
</td>
</tr>
<tr>
<td align="right" required="required">Gender:</td>
<td>
<select name="u_gender">
<option>Select a Gender</option>
<option>Female</option>
<option>Male</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td align="right" required="required">Birthday:</td>
<td><input type="date" name="u_birthday"/>
</td>
</tr>
<tr>
<td colspan="6">
<button name="sign_up">Sign up</button>
</td>
</tr>
</table>
</form>
<?php InsertUser();?>
</div>
</div>
<!-- this is where the second form and picture ends on
main page -->
</body>
</html>
电子邮件:
密码:
登录
在这里注册
姓名:
密码:
电邮:
国家:
选择一个国家
美国
阿富汗
印度
性别:
选择性别
女性
男性
其他
生日:
注册
这段代码是将信息传输到数据库的functions.php
<?php
$con = mysqli_connect("localhost","root","","social_network")
or die("Connection was not established");
function InsertUser(){
global $con;
if(isset($_POST['sign_up'])){
$name = $_POST['u_name'];
$pass = $_POST['u_pass'];
$email = $_POST['u_email'];
$country = $_POST['u_country'];
$gender = $_POST['u_gender'];
$b_day = $_POST['u_birthday'];
$date = date("d-m-y");
$status = "unverified";
$posts = "No";
$get_email = "select * from users where user_email='$email'";
$run_email = mysqli_query($con,$get_email);
$check = mysqli_num_rows($run_email);
if($check==1){
echo "<script>alert('Email has already been registered. Please try another one.')</script>";
exit();
}
if(strlen($pass)<8){
echo "<script>alert('Password should be minimum 8 characters.')</script>";
exit();
}
//this does not work. need to figure out why
else {
$insert = "INSERT into users (user_name,user_pass,user_email,user_country,user_gender,user_b_day,user_image,register_date,last_login,status,posts) values ('$name','$pass','$email','$country','$gender','$b_day','default.jpg','$date','$date','$status','$posts')";
$run_insert = mysqli_query($con,$insert);
if($run_insert){
echo "<script>alert('Registration Successful!')</script>";
}
}
}
}
?>
使用mysqli\u error获取有意义的错误消息:
if($run_insert)
{
echo "<script>alert('Registration Successful!')</script>";
}
else
{
echo sprintf("Error %s executing %s", mysqli_error($con), $insert);
}
if($run\u insert)
{
回显“警报('注册成功!')”;
}
其他的
{
echo sprintf(“错误%s执行%s”,mysqli_错误($con),$insert);
}
警告:当使用mysqli
时,您应该使用和将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST
或$\u GET
数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。谢谢。我刚开始学这个,所以一切对我来说都是新的。好的,谢谢你。你的代码中有一个错误,但在我修复了你的代码后,我发现了我的错误!执行%s项“主”的重复项“0”时出现错误%s。现在我只需要想办法解决这个问题