【发布时间】:2016-05-02 09:14:49
【问题描述】:
我创建了一个 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”页面,这意味着没有任何错误并且查询提交正确。
谁能告诉我我做错了什么,谢谢。
【问题讨论】:
-
使用
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);检查页面错误 -
请分享db表的视图..
-
检查这个条件
if (($query) == TRUE) {我认为$query是INSERT INTO.. -
能否提供数据库表的屏幕。
-
据我所见,您实际上并没有检查查询的状态/成功,现在您正在检查
$query是否为真,因为它包含数据. - 如果您改为检查查询执行的状态,我想您会看到一个错误。