ai didi

PHP 仅在第二次单击后才起作用

转载 作者:行者123 更新时间:2023-11-29 07:26:32 24 4
gpt4 key购买 nike

我编写了这个 php 代码:

$friendbutton;  
$sqla = "SELECT * FROM friends WHERE user1='$usernotif' AND user2='$username' AND accepted='0'";
$query = mysqli_query($link,$sqla) or die(mysqli_error($link));
$row_counter = mysqli_num_rows($query);
if ($row_counter > 0) {
$friendbutton ='<input id="Friend" type="submit" name="Delete" value="Unsend friend request"><br />';
$type="Unsend";
$_SESSION['type']=$type;
} else {
$friendbutton='<input id="Friend" name="Send" type="submit" value="Send friend request"><br />';
}

这在我的 friend.php 中:

if (isset($_SESSION['Send'])){
$sql = "INSERT INTO friends(user1, user2, datemade) VALUES('$usernotif','$username',now())";
$sqla = "INSERT INTO notifications(username,initiator,date_time,type) VALUES('$username','$usernotif',now(),'Friend')";
}

但是第二次单击后“发送”和“删除”都会起作用。

最佳答案

试试这个:

<?php
$sqla = "SELECT * FROM friends WHERE user1='$usernotif' AND user2='$username'
AND accepted='0'";

$query = mysqli_query($link, $sqla) or die(mysqli_error($link));
$row_counter = mysqli_num_rows($query);

if ( !empty($row_counter) ){
$sql = "INSERT INTO friends(user1, user2, datemade) VALUES('$usernotif','$username',now())";
$sqla = "INSERT INTO notifications(username,initiator,date_time,type) VALUES('$username','$usernotif',now(),'Friend')";
}
elseif (isset($_FORM['delete'])){
//delete query goes here
}

?>

<form action="" method="post" accept-charset="utf-8">
<?php
if ( !empty($row_counter) )
echo '<input type="submit" name="Delete" value="Unsend friend request"><br />';
else
echo '<input type="submit" name="Send" value="Send friend request"><br />';
?>
</form>

关于PHP 仅在第二次单击后才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360524/

24 4 0
文章推荐: php - 使用mysqli登录系统
文章推荐: mysql - 在子查询mysql中使用父查询的id列
文章推荐: mysql - 查询完全阻塞 MySQL 服务器
文章推荐: Java MySQL有可能造成死锁吗?
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com