我有这个表单允许我将用户添加到我的数据库。
当我添加新记录并输入包含数据的每个字段并提交时,它会成功地将数据添加到数据库
但是,当我尝试更新该特定记录并按下提交时,我的memberID字段会自动设置一个值,我不确定它的来源(请参阅此处http://imgur.com/MK07epA)
我在整个代码中都看了一遍,似乎找不到导致它的原因。
在第一次更新后尝试更新或删除记录时,我收到以下错误消息:(http://imgur.com/Qs0IQKi)
添加代码
<!DOCTYPE html>
<html>
<head><title>Insert Users</title></head>
<body>
<h2>Insert User Confirmation</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br>
<?php
include('connection.php');
echo "<label for='memberID' >Member ID:</label>";
echo "<input type='text' name='memberID' id='memberID' />";
echo "<br /><br />";
echo "<label for='username' >Username:</label>";
echo "<input type='text' name='username' id='username' />";
echo "<br /><br />";
echo "<label for='password' >Password:</label>";
echo "<input type='password' name='password' id='password' />";
echo "<br /><br />";
echo "<label for='fName' >Firstname:</label>";
echo "<input type='text' name='fName' id='fName' />";
echo "<br /><br />";
echo "<label for='lName' >Lastname:</label>";
echo "<input type='text' name='lName' id='lName' />";
echo "<br /><br />";
echo "<label for='address' >Address:</label>";
echo "<input type='text' name='address' id='address' />";
echo "<br /><br />";
echo "<label for='email' >Email:</label>";
echo "<input type='text' name='email' id='email' />";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "<input type='reset' value='Clear' />";
echo "<br /><br />";
?>
</form>
</section>
<a href="home.php">Back Home</a><br/>
<?php
if(!isset($_POST['submit'])) {
echo 'Please Register';
}
else {
$memberID = $_POST['memberID'];
$username = $_POST['username'];
$password = $_POST['password'];
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$email = $_POST['email'];
$query = "INSERT INTO `members` (memberID, username, password, firstname, lastName, address, email)
VALUES ('$memberID', '$username', '$password', '$fName', '$lName', '$address', '$email')";
mysqli_query($connection, $query)
or die(mysqli_error($connection));
$rc = mysqli_affected_rows($connection);
if ($rc==1)
{
echo '<h4>The database has been updated with the following details: </h4> ';
echo 'MemberID: '.$memberID.'<br />';
echo 'Username: '.$username.'<br />';
echo 'Password: '.$password.'<br />';
echo 'First Name: '.$fName.'<br />';
echo 'Last Name: '.$lName.'<br />';
echo 'Address: '.$address.'<br />';
echo 'Email: '.$email.'<br />';
}
else
{
echo '<p>The data was not entered into the database this time.</p>';
}
}
?>
</body>
</html>
查看代码
<?php
require_once('connection.php');
$query = 'select * from members order by memberID';
include('includes/query.php');
echo '<table>';
echo '<tr>';
echo ' <th>MemberID</th>
<th>Username </th>
<th>Password</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Address</th>';
echo '</tr>';
foreach ($rows as $row)
{
echo '<tr>';
echo '<td>' . $row['memberID'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['password'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['address'] . '</td>';
echo '<td> <a href="confirm_delete.php?memberID=' . $row['memberID'] . '">Delete</a> </td>';
echo '<td> <a href="update_member_form.php?memberID=' . $row['memberID'] . '">Update</a> </td>';
echo '</tr>';
}
echo '</table>';
?>
更新代码
<?php
require_once('connection.php');
if(!isset($_POST['submit'])) {
echo "Unauthorised access";
}
else {
$memberID = $_POST['memberID'];
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$address = $_POST['address'];
$query = "update members set memberID = '#memberID', username = '$username', password = '$password',
firstname = '$firstname', lastname = '$lastname', email = '$email', address = '$address' where memberID = '$memberID'";
$result = mysqli_query($connection, $query)
or die (mysqli_error($connection));
$rc = mysqli_affected_rows($connection);
if($rc == 1)
{
echo "<p>You have successfully updated memberID: {$memberID} account details</p>";
echo "<p><a href='view_records.php'>Back to User List</a> </p>";
}
else
{
echo "<p>No changes made.</p>";
echo "<p><a href='view_records.php'>Back to User List</a> </p>";
}
}
?>
删除代码
<?php
require_once('connection.php');
$memberID = $_GET['memberID'];
$query = "delete from members where memberID = '$memberID';";
$result = mysqli_query($connection, $query)
or die (mysqli_error($connection));
$rc = mysqli_affected_rows($connection);
if($rc == 1)
{
echo "<p>You have successfully removed MemberID: {$memberID} from the database</p>";
echo "<a href='list_delete_update_users.php'>Back to User List</a>";
}
else
{
echo "<p>No record deleted</p>";
echo "<a href='view_records.php'>Back to User List</a>";
}
?>
答案 0 :(得分:0)
添加和删除与您的问题无关,因此您可以将其置之不理。查看查询:&#34;设置memberID =&#39;#memberID&#39;&#34;。这是你的问题。另外,如何将文本插入单元格?我认为该列应该类似于unsigned int。
最后,查看参数化查询
答案 1 :(得分:0)
我认为这是因为您的更新查询。正确检查它有以下
$query = "update members set memberID = '#memberID', username = '$username', password = '$password',
firstname = '$firstname', lastname = '$lastname', email = '$email', address = '$address' where memberID = '$memberID'";
请注意,您要放 memberID ='#memberID' - 我认为您应该将memberID ='$ memberID'放在那里。
答案 2 :(得分:0)
在更新代码中查看此行:
$query = "update members set memberID = '#memberID', username = '$username', password = '$password',
firstname = '$firstname', lastname = '$lastname', email = '$email', address = '$address' where memberID = '$memberID'";
溶液: 你在$ memberID中通过post获得了变量,但在更新时你正在使用#memberID。这可能是你的一个问题。