我正在创建一个添加用户按钮,但是,当同一页面上有多个用户时,它们都会在点击时添加。
以下是代码:
$ data_emails是一个包含用户电子邮件的数组。
<?php
for ($i = 0; $i < count($data_emails); $i ++) {
echo $data_emails[$i] . ' ' . $data_usernames[$i] . '<br />';
$email2 = $data_emails[$i];
$contact = $data_usernames[$i];
if (relationship_exists($email, $email2) === false) {
?>
<form action="" name="add_user" method="POST">
<input type="submit" name="add_user" value="Add user" />
</form>
</div>
<?php
}
if (isset($_POST['add_user']))
create_relationship($email, $email2, $contact);
}
?>
答案 0 :(得分:0)
这里的问题是你在循环中拥有一切。所以它的功能就像这样。试试以下代码。
<?php
for ($i = 0; $i < count($data_emails); $i ++)
{
echo $data_emails[$i] . ' ' . $data_usernames[$i] . '<br />';
$email2 = $data_emails[$i];
$contact = $data_usernames[$i];
if (relationship_exists($email, $email2) === false)
{
?>
<form action="" name="add_user" method="POST">
<input type="hidden" name="email" value="<?php echo $email?>">
<input type="hidden" name="email2" value="<?php echo $email2?>">
<input type="hidden" name="contact" value="<?php echo $contact?>">
<input type="submit" name="add_user" value="Add user" />
</form>
</div>
<?php
}
}
if (isset($_POST['add_user']))
{
$email = mysqli_real_escape_string($conn, $_POST['add_user']);
$email2 = mysqli_real_escape_string($conn, $_POST['email2']);
$contact = mysqli_real_escape_string($conn, $_POST['contact']);
create_relationship($email, $email2, $contact);
}
?>