通过单击 ID 为 USING MYSQLI 的 link/button 从数据库中插入和删除数据
INSERT and DELETE data from database by clicking a link/button with ID USING MYSQLI
我正在尝试 INSERT
同时 DELETE
来自数据库的数据。每当我在 URL 中通过他的 ID 查看成员的信息时,就会有一个 buttom/link 和他的信息来接受该成员。如果管理员单击接受 button/link,该成员将不再位于 requests
的 table 中(这就是应该删除的地方),同时他的详细信息或数据将被插入到另一个名为 "members" 的 table 中。但是当我点击 ACCEPT 时,它只回显 javascript 进行确认,而当我点击 "Yes" 时,什么也没有发生,它只是让我回到 pendingRequests.php。我将在这里分享我的代码。
这是link点击查看会员详情来自pendingRequests.php
<a href="/test/admin/view_request.php?view_id=<?php echo $row['id']; ?>" >VIEW</a>
这是会员数据显示的地方:view_requests.php
<?php
include ('dbcontroller.php');
if(isset($_GET['view_id']))
{
$id=$_GET['view_id'];
$sql = mysqli_query($conn, "SELECT * from requests where id='$id'");
$row = mysqli_fetch_array($sql);
?>
ID: <?php echo $row['id']; ?>
<div class=" col-md-9 col-lg-9 ">
<table class="table table-user-information">
<tbody>
<tr>
<td><h4><b>Profile Info</b></h4></td>
</tr>
<tr>
<td>Name:</td>
<td><?php echo $row['firstname']; ?> <?php echo $row['MI']; ?> <?php echo $row['lastname']; ?></td>
</tr>
<tr>
<td>Email-address:</td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Gender:</td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Status:</td>
<td><?php echo $row['status']; ?></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><?php echo $row['bday']; ?></td>
</tr>
<tr>
<td>Contact Number:</td>
<td><?php echo $row['contactno']; ?></td>
</tr>
<tr>
<td><a href="javascript:view_id(<?php echo $row['id']; ?>) ">ACCEPT</a></td>
<td>DECLINE</td>
</tr>
<?php
}
$conn->close();
?>
</tbody>
</table>
</div>
<script type="text/javascript">
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id=='+view_id;
}
}
</script>
这是管理员点击“ACCEPT”的link/button代码以及拥有该代码的成员的数据来自 "requests" table 的 ID 将被删除,成员的信息将被插入 "members" table.
<a href="javascript:view_id(<?php echo $row['id']; ?>) ">ACCEPT</a>
这是我的acceptRequest.php代码
<?php
include('dbcontroller.php');
if(isset($_GET['view_id']))
{
$sql = "SELECT * FROM requests WHERE id=".$_GET['view_id'];
$firstname = $row['firstname'];
$MI = $row['MI'];
$lastname = $row['lastname'];
$gender = $row['gender'];
$status = $row['status'];
$maiden = $row['maiden'];
$bday = $row['bday'];
$contactno = $row['contactno'];
$email = $row['email'];
mysqli_query($conn, "INSERT INTO members(id,'$firstname','$MI','$lastname','$gender','$status','$bday','$contactno','$email')");
mysqli_query($conn, "DELETE FROM requests WHERE id=".$_GET['view_id']);
header("Location: pendingrequests.php");
}
?>
这是您的 javascript 代码:
<script type="text/javascript">
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id=='+view_id;
}
}
</script>
你将一个参数传递给这个函数,它被称为 id
但在这个脚本的主体中你使用了一个名为 view_id
的不存在的变量
改变
window.location='acceptRequest.php?view_id=='+view_id;
到
window.location='acceptRequest.php?view_id='+id;
另请注意,我在该行
中将 ==
更改为 =
同样这个说法是错误的
mysqli_query($conn, "INSERT INTO members
(id,'$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
我建议将 id
保留在外面,就好像它是一个 auto increment
列一样,它会自动创建,您不需要传递值。
mysqli_query($conn, "INSERT INTO members VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
或者,如果它不是 auto increment
列,您将需要创建一个名为 $id
的变量,并将查询语法更改为使用 $id
而不是 id
。
好的,如果它仍然不起作用,您需要像这样从 mysqli
获取错误消息。事实上,您应该始终检查对 mysqli
api.
的所有调用的状态
$result = mysqli_query($conn, "INSERT INTO members VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
if ( $result === FALSE ) {
echo 'Insert ERROR: ' . mysqli_error($conn);
exit;
}
这应该向页面报告查询中的错误。
我注意到您捕获了 $maiden = $row['maiden'];
但您没有在查询中使用它!可能是该列设置为需要一个值,这就是查询失败的原因吗?或者完全缺少该列意味着 maiden
之后的列被加载到与它们现在接收的数据不兼容的列中。
我认为最好使用这种更长但更精确的语法
$result = mysqli_query($conn, "INSERT INTO members
(colname,colname,colname,colname,
colname,colname,colname,colname,)
VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
那么至少您总能将正确的数据加载到正确的列名中。
您需要传递 id
参数而不是函数 view_id
。此外,您的 URL ==
中有错字。您的代码应如下所示:
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id='+id;
}
}
您 SQL 在 members(id,
附近似乎有错字。最好在 SQL 中提及所需的列。您可以保留 id 槽,让 MySQL 自行递增。此外,为了安全起见,您应该将 PHP 代码放在 if 语句中:
$firstname = $row['firstname'];
$MI = $row['MI'];
$lastname = $row['lastname'];
$gender = $row['gender'];
$status = $row['status'];
$maiden = $row['maiden'];
$bday = $row['bday'];
$contactno = $row['contactno'];
$email = $row['email'];
$insert_sql=mysqli_query($conn, "INSERT INTO members (firstname, MI, lastname, gender, status, bday, contactno, email)
VALUES ('$firstname','$MI','$lastname','$gender','$status','$bday','$contactno','$email')");
if($insert_sql){
mysqli_query($conn, "DELETE FROM requests WHERE id=".$_GET['view_id']);
header("Location: pendingrequests.php");
}
我正在尝试 INSERT
同时 DELETE
来自数据库的数据。每当我在 URL 中通过他的 ID 查看成员的信息时,就会有一个 buttom/link 和他的信息来接受该成员。如果管理员单击接受 button/link,该成员将不再位于 requests
的 table 中(这就是应该删除的地方),同时他的详细信息或数据将被插入到另一个名为 "members" 的 table 中。但是当我点击 ACCEPT 时,它只回显 javascript 进行确认,而当我点击 "Yes" 时,什么也没有发生,它只是让我回到 pendingRequests.php。我将在这里分享我的代码。
这是link点击查看会员详情来自pendingRequests.php
<a href="/test/admin/view_request.php?view_id=<?php echo $row['id']; ?>" >VIEW</a>
这是会员数据显示的地方:view_requests.php
<?php
include ('dbcontroller.php');
if(isset($_GET['view_id']))
{
$id=$_GET['view_id'];
$sql = mysqli_query($conn, "SELECT * from requests where id='$id'");
$row = mysqli_fetch_array($sql);
?>
ID: <?php echo $row['id']; ?>
<div class=" col-md-9 col-lg-9 ">
<table class="table table-user-information">
<tbody>
<tr>
<td><h4><b>Profile Info</b></h4></td>
</tr>
<tr>
<td>Name:</td>
<td><?php echo $row['firstname']; ?> <?php echo $row['MI']; ?> <?php echo $row['lastname']; ?></td>
</tr>
<tr>
<td>Email-address:</td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Gender:</td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Status:</td>
<td><?php echo $row['status']; ?></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><?php echo $row['bday']; ?></td>
</tr>
<tr>
<td>Contact Number:</td>
<td><?php echo $row['contactno']; ?></td>
</tr>
<tr>
<td><a href="javascript:view_id(<?php echo $row['id']; ?>) ">ACCEPT</a></td>
<td>DECLINE</td>
</tr>
<?php
}
$conn->close();
?>
</tbody>
</table>
</div>
<script type="text/javascript">
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id=='+view_id;
}
}
</script>
这是管理员点击“ACCEPT”的link/button代码以及拥有该代码的成员的数据来自 "requests" table 的 ID 将被删除,成员的信息将被插入 "members" table.
<a href="javascript:view_id(<?php echo $row['id']; ?>) ">ACCEPT</a>
这是我的acceptRequest.php代码
<?php
include('dbcontroller.php');
if(isset($_GET['view_id']))
{
$sql = "SELECT * FROM requests WHERE id=".$_GET['view_id'];
$firstname = $row['firstname'];
$MI = $row['MI'];
$lastname = $row['lastname'];
$gender = $row['gender'];
$status = $row['status'];
$maiden = $row['maiden'];
$bday = $row['bday'];
$contactno = $row['contactno'];
$email = $row['email'];
mysqli_query($conn, "INSERT INTO members(id,'$firstname','$MI','$lastname','$gender','$status','$bday','$contactno','$email')");
mysqli_query($conn, "DELETE FROM requests WHERE id=".$_GET['view_id']);
header("Location: pendingrequests.php");
}
?>
这是您的 javascript 代码:
<script type="text/javascript">
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id=='+view_id;
}
}
</script>
你将一个参数传递给这个函数,它被称为 id
但在这个脚本的主体中你使用了一个名为 view_id
改变
window.location='acceptRequest.php?view_id=='+view_id;
到
window.location='acceptRequest.php?view_id='+id;
另请注意,我在该行
中将==
更改为 =
同样这个说法是错误的
mysqli_query($conn, "INSERT INTO members
(id,'$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
我建议将 id
保留在外面,就好像它是一个 auto increment
列一样,它会自动创建,您不需要传递值。
mysqli_query($conn, "INSERT INTO members VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
或者,如果它不是 auto increment
列,您将需要创建一个名为 $id
的变量,并将查询语法更改为使用 $id
而不是 id
。
好的,如果它仍然不起作用,您需要像这样从 mysqli
获取错误消息。事实上,您应该始终检查对 mysqli
api.
$result = mysqli_query($conn, "INSERT INTO members VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
if ( $result === FALSE ) {
echo 'Insert ERROR: ' . mysqli_error($conn);
exit;
}
这应该向页面报告查询中的错误。
我注意到您捕获了 $maiden = $row['maiden'];
但您没有在查询中使用它!可能是该列设置为需要一个值,这就是查询失败的原因吗?或者完全缺少该列意味着 maiden
之后的列被加载到与它们现在接收的数据不兼容的列中。
我认为最好使用这种更长但更精确的语法
$result = mysqli_query($conn, "INSERT INTO members
(colname,colname,colname,colname,
colname,colname,colname,colname,)
VALUES
('$firstname','$MI','$lastname','$gender',
'$status','$bday','$contactno','$email')");
那么至少您总能将正确的数据加载到正确的列名中。
您需要传递 id
参数而不是函数 view_id
。此外,您的 URL ==
中有错字。您的代码应如下所示:
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id='+id;
}
}
您 SQL 在 members(id,
附近似乎有错字。最好在 SQL 中提及所需的列。您可以保留 id 槽,让 MySQL 自行递增。此外,为了安全起见,您应该将 PHP 代码放在 if 语句中:
$firstname = $row['firstname'];
$MI = $row['MI'];
$lastname = $row['lastname'];
$gender = $row['gender'];
$status = $row['status'];
$maiden = $row['maiden'];
$bday = $row['bday'];
$contactno = $row['contactno'];
$email = $row['email'];
$insert_sql=mysqli_query($conn, "INSERT INTO members (firstname, MI, lastname, gender, status, bday, contactno, email)
VALUES ('$firstname','$MI','$lastname','$gender','$status','$bday','$contactno','$email')");
if($insert_sql){
mysqli_query($conn, "DELETE FROM requests WHERE id=".$_GET['view_id']);
header("Location: pendingrequests.php");
}