Php 通过使用MYSQLI单击ID为的链接/按钮,从数据库中插入和删除数据
我正在尝试Php 通过使用MYSQLI单击ID为的链接/按钮,从数据库中插入和删除数据,php,mysql,mysqli,sql-insert,sql-delete,Php,Mysql,Mysqli,Sql Insert,Sql Delete,我正在尝试插入,同时从数据库中删除数据。每当我通过URL中的ID查看某个成员的信息时,就会有一个按钮/链接,其中包含他的信息以接受该成员。如果管理员单击“接受”按钮/链接,该成员将不再位于请求表中(这就是删除的地方),同时他的详细信息或数据将插入另一个名为“成员”的表中。但是,当我单击ACCEPT时,它只回显javascript进行确认,当我单击Yes时,什么都没有发生,它只会将我带回到pendingRequests.php。我将在这里分享我的代码 这是链接,单击该链接可查看来自pendingR
插入
,同时从数据库中删除
数据。每当我通过URL中的ID查看某个成员的信息时,就会有一个按钮/链接,其中包含他的信息以接受该成员。如果管理员单击“接受”按钮/链接,该成员将不再位于请求表中(这就是删除的地方),同时他的详细信息或数据将插入另一个名为“成员”的表中。但是,当我单击ACCEPT时,它只回显javascript进行确认,当我单击Yes时,什么都没有发生,它只会将我带回到pendingRequests.php。我将在这里分享我的代码
这是链接,单击该链接可查看来自pendingRequests.php的成员的详细信息
<a href="/test/admin/view_request.php?view_id=<?php echo $row['id']; ?>" >VIEW</a>
<?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>
在这里,成员的数据将显示:view\u requests.php
<a href="/test/admin/view_request.php?view_id=<?php echo $row['id']; ?>" >VIEW</a>
<?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>
这是您的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>
到
另请注意,我将该行中的==
更改为=
这种说法也是错误的
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')");
或者,如果它不是一个自动递增列
,则需要创建一个名为$id
的变量,并将查询语法更改为使用$id
而不是id
好的,如果它仍然不起作用,您需要像这样从mysqli
获取一条erro消息。事实上,您应该始终检查对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\u 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;
}
}
在成员(id,
)附近的SQL中似乎有一个输入错误。同时提及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");
}
根据这个window.location='acceptRequest.php?view\u id=='+view\u id;
参数将始终为空,使用“id”而不是“view\u id”在你的函数中。对不起,为什么它总是空的?@slash197trywindow.location='acceptRequest.php?view_id='+view_id;
我太笨了。对不起。我知道它在哪里。lol抱歉,先生。希望你能帮我解决我的新问题:((但非常感谢你在上一个问题上帮了我的忙!)先生,我已经解决了这个问题,不过还是要谢谢你:)它最终从我的请求表中删除,但没有插入到我的成员表中。请参阅插入查询的附加修复。先生,id是自动递增的。我已经从插入查询中删除了id,但它仍然不起作用。好的,第三次尝试,添加一些错误检查,您将被告知查询有什么问题。您对还有一个聪明人。你们是天才!谢谢!:)嗨,先生,又是我。idk发生了什么,但是诸如姓名、联系人号码等数据没有插入到成员表中。我以为我刚才做了,但我错了,只是表ib中的id增加了,但是像name这样的字段的值没有插入。我和你有相同的代码。天哪,我刚刚意识到你和我在评论lol中交谈的人是同一个人,不管怎样,我认为我的错误在这里->$sql=“SELECT*FROM requests WHERE id=”.$\u GET['view\u id']代码>但是我已经添加了这个->$row=mysqli\u查询($sql)代码>但名称、状态等值仍不会插入到成员表中,但id会增加。我无法找出问题所在,因为也没有错误:(请尝试echo”“;打印($row);echo”“;
并检查您是否获得了值我遇到了此错误警告:mysqli\u query()至少需要2个参数,给定1个
function view_id(id)
{
if(confirm('Are you sure you want to accept this member request? '))
{
window.location='acceptRequest.php?view_id='+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'];
$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");
}