我创建了一个 HTML 表单,需要将输入表单的数据直接插入 mySQL 中的表中。
newuser.php 文件
<?php
//including the connection page
include('./DB_Connect.php');
//get an instance
$db = new Connection();
//connect to database
$db->connect();
//fetch username and password
$usertype = $_POST['userType'];
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$username = $_POST['userName'];
$password = $_POST['password'];
$address1 = $_POST['add1'];
$address2 = $_POST['add2'];
//write the sql statement
$query = "INSERT INTO USERS (usertype, fname, lname, username, password, add1, add2)
VALUES ('$usertype', '$firstname', '$lastname', '$username', '$password', '$address1', '$address2')";
mysql_query($query,$db);
if (($query) == TRUE) {
echo "New record created successfully";
header("Location: login.php", true);
exit();
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
header("Location: register.php", true);
exit();
}
//close once finished to free up resources
$db->close();
?>
使用以下 html 表单:
<form action="newuser.php" method="POST" class="form" id="registerForm">
<label> Type of user: </label> <br>
<input type="radio" name="userType" id="itemSeeker">Item Seeker </input>
<input type="radio" name="userType" id="itemDonor">Item Donor </input>
<input type="radio" name="userType" id="peopleSeeker">People Seeker </input>
<input type="radio" name="userType" id="peopleDonor">People Donor </input>
<br>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="firstName" placeholder="First Name: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="text" name="lastName" placeholder="Last Name: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<input type="text" name="email" placeholder="Email Address: "align="center" class="form-control" ></input>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="userName" placeholder="Username: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<!-- <label> Address 1: </label>-->
<input type="text" name="add1" placeholder="Address 1: " align="center" class="form-control" ></input>
<!-- <label> Address 2: </label>-->
<input type="text" name="add2" placeholder="Address 2: " align="center" class="form-control" ></input>
<br>
<button class="btn btn-primary" name="submitReg" type="submit">Submit</button><br>
<br>
<a href="login.php" >Already have an account?</a>
</form>
USERS 表
上面的两个代码块和我正在使用的代码。
我的问题是,提交表单时,数据实际上并未输入到表中。请注意,第一次提交确实有效。第一次提交之后的所有提交似乎都没有将任何内容输入数据库。
我不太确定我的代码有什么问题导致它无法工作。它确实会转到“login.php”页面,这意味着没有任何错误并且查询已正确提交。
有人可以告诉我我做错了什么吗,谢谢。
最佳答案
现在你遇到的麻烦比插入问题还要多。你的代码完全不安全。 (mysql注入(inject))。
不要使用 mysql_* 函数,使用 pdo 代替准备好的语句!
在发送 header 之前输出空格,如果有输出,则无法发送 header 。你的重定向不起作用。不要中继客户端重定向,使用可能会禁用它,因此输出您希望用户前往的链接。
另一个你的单选按钮没有值检查 html 语法 var_dump($_POST) 并检查您是否提交了所有内容。在分配变量之前还要检查 isset 或空。进行某种验证。
看看一些 php 框架,它们提供了更多的灵 active 和错误检查
不要在落后程序10年或更长时间的情况下自己编写所有内容来重新发明轮子
关于php - 使用 PHP 将数据从表单插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36978926/