gpt4 book ai didi

javascript - 表单操作未通过 php/html 发布

转载 作者:行者123 更新时间:2023-12-02 17:07:44 24 4
gpt4 key购买 nike

我在将一些信息从表单发布到删除功能时遇到问题,我可能会为此自责,但我找不到答案。

  • admin/index.php
  • lib/admin_functions.php
  • lib/delete_user.php

如上所述,我有 3 个文件。

它本身的功能如下:

// Display users
function get_all_users() {
global $con;
$sql = "SELECT * FROM users ORDER BY id ASC";
$result = mysqli_query($con, $sql);
echo "<tr><td> </td><td> User ID </td><td> Username </td><td> Email Address </td><td> Zendesk User Id </td><td>
Zendesk View ID </td><td> Firstname </td><td> Surname </td><td> Nickname </td><td> Active
</td><td> Admin </td><td> Display Stats?</td><td> Remove User </td></tr>";
while($row = mysqli_fetch_array($result)) {
// Displaying all user details
echo "<tr><td><img height='40' width='40' src='{$row['user_photo']}'></td><td>" .$row['id']. "</td><td>" .$row['user_name']. "</td><td>" .$row['email_address']. "</td><td>" .$row['zendesk_user_id']."</td><td>"
.$row['zendesk_view_id']. "</td><td>" .$row['user_firstname']. "</td><td>" .$row['user_surname']. "</td><td>" .$row['user_nickname']. "</td><td>"
.$row['is_active']. "</td><td>" .$row['is_admin']. "</td><td>" .$row['display_stats'].
// Form for deleting a user
"</td><td><form id='delete_user' accept-charset='UTF-8' action='/delete_user.php' role='form' method='POST'><input type='checkbox' value='Delete'><input type='hidden' name='user_id' value='{$row['id']}'></td></tr>";
}
echo "<tr><td> Delete Selected User(s) </td><td>";
echo "<input type='submit' name='submit' value='Delete My Account' onClick=\"return confirm('Are you sure you want to delete this account?')\"></td></form></tr>";
}

上面的函数属于admin_functions.php,它显示我的所有用户,并带有一个额外的列,其中带有用于删除的复选框,并且在底部有一个带有删除按钮的表单。

然后我就有了delete_user代码

<?php
session_start();
if( ! $_SESSION['loggedIn'] && ! $_SESSION['isAdmin']) {
// If not admin or logged in, die
die();
}
require_once(dirname(__FILE__).'/admin_functions.php');
require_once(dirname(__FILE__).'/../config.php');

// DELETE USER

$user_id = mysql_real_escape_string( $_POST['user_id'] );

// Insert the customer, including the password hash
$sql = "DELETE FROM users where id = '{$user_id}' limit 1";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}

print "User Deleted";
mysqli_close($con);

?>

最后在索引文件上,我有一些调用该函数的东西。

<?php get_all_users(); ?>

我遇到的问题是代码全部按预期显示,但在警告消息之后没有任何反应。似乎没有任何内容发布到delete_user.php,我在apache2 中没有看到任何错误,在我的控制台中也没有看到任何错误。

有什么想法吗?

最佳答案

根据我们的聊天对话,我提交以下答案以结束问题并将其标记为已解决。

您需要对 HTML/表格进行一些细微的修改。另外,您稍后需要对其进行修改,使其成为更安全的方法。

我没有时间添加额外的东西。

您也没有 <table></table>标签。

使用以下链接>>> mysqli_ with prepared statements ,或PDOprepared statements将有助于 SQL 注入(inject)。

<?php 

$DB_HOST = "xxx"; // replace
$DB_NAME = "xxx"; // replace
$DB_USER = "xxx"; // replace
$DB_PASS = "xxx"; // replace

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($con->connect_errno > 0) {
die('Connection failed [' . $con->connect_error . ']');
}

$sql = "SELECT * FROM users ORDER BY id ASC";
$result = mysqli_query($con, $sql);


echo "<!DOCTYPE html>" . "\n";
echo "<head></head>" . "\n";
echo "<body>" . "\n";

echo "<form id='delete_user' accept-charset='UTF-8' action='' role='form' method='POST'>" . "\n";

echo "<table>" . "\n";
echo "<tr><td> User ID </td><td> Username </td><td> Email Address </td><td> Zendesk User Id </td><td>
Zendesk View ID </td><td> Firstname </td><td> Surname </td><td> Nickname </td><td> Active
</td><td> Admin </td><td> Display Stats?</td><td> Remove User </td></tr>" . "\n";

while($row = mysqli_fetch_array($result)) {
// Displaying all user details
echo "<tr><td>USER PHOTO CODE</td><td>" .$row['id']. "</td><td>" .$row['user_name']. "</td><td>" .$row['email_address']. "</td><td>" .


"</td>\n<td><input type='checkbox' name='user_id[]' value='{$row['id']}'></td></tr>" . "\n";
echo "<tr><td> Delete Selected User(s) </td><td>" . "\n";

}

echo "<input type='submit' name='submit' value='Delete My Account' onClick=\"return confirm('Are you sure you want to delete this account?')\"></td>\n</tr>" . "\n";

echo "</table>" . "\n";

echo "</form>";

echo "</body></html>" . "\n";

if(isset($_POST['submit'])){

foreach($_POST['user_id'] as $id){
$id = (int)$id;
$delete = "DELETE FROM users WHERE id = $id";
mysqli_query($con,$delete);
}

print "User Deleted";
mysqli_close($con);

}

关于javascript - 表单操作未通过 php/html 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25096125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com