gpt4 book ai didi

php - 将从表中提取的用户名插入到另一个表中

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:52 26 4
gpt4 key购买 nike

这让我抓狂,我正在学习 PHP,这个问题困扰了我一整天。我正在将一些信息从用户数据库提取到一个文件中,然后在表单中使用它插入到另一个表中。

我想用用户 ID 和用户名来做这件事,我立即尝试了 $userID = $_GET['id']; 效果很好,但在尝试 $username = $_GET['用户名']。为了获得这种形式,我将用户 ID 回显到 url 中,这可能就是为什么它只会获得 ID 的原因吗?

如果我回显它有效的用户名,那么有点难过。我想如果我将用户名回显到 url 中它会起作用而 ID 不会,但老实说我不想回显用户名或两者,除非我也有。

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

include_once '../includes/conn.php';

if(!$user->is_loggedin()){
$user->redirect('../users/login.php');
}


$stmt = $conn->prepare("SELECT id, username FROM users");
$stmt->execute();

$userRow=$stmt->fetch(PDO::FETCH_ASSOC);

$userID = $_GET['id'];
$username = $_GET['username'];

if(isset($_POST['submit'])){
$category = trim($_POST['category']);
$points = trim($_POST['points']);
$reason = trim($_POST['reason']);

if($category==""){
$error[] = "Select category.";
}else if($points==""){
$error[] = "Provide points.";
}else if($reason==""){
$error[] = "Provide reason.";
}else if(strlen($reason) < 6){
$error[] = "Reason must be at least 6 characters<br /><br />";
}else{
try{
$sql = "INSERT INTO infractions(userid,username,category,points,reason)VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($userID, $username, $category, $points, $reason));
}
catch(PDOException $e){
echo $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>EpicOwl UK | CMS Users Add Infraction</title>
<meta charset="utf-8">
<link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
<a href="index.php"><img id="logo" src="../images/logo.png" /></a>
<div id="navigation">
<ul>
<a href="../index.php"><li>Home</li></a>
<a href="../users/profile.php"><li>My Profile</li></a>
<a href="./index.php"><li>Admin Panel</li></a>
</ul>
</div>
</div>
<div id="content">
<form method="$_POST"><br />
<h2>Give <?php echo ($userRow['username']); ?> an Infraction</h2>
<label><strong>Category:</strong></label><br />
<select name="category">
<option value="Select Category">Select Category</option>
<option value="Language Used">Language Used</option>
<option value="Breaking Rules">Breaking Rules</option>
<option value="Double Posting">Double Posting</option>
</select><br /><br />
<label><strong>Number of points to award:</strong></label><br />
<input type="text" name="points" maxlength="50" /><br /><br />
<label><strong>Reason:</strong><label><br />
<textarea name="reason" rows="13" cols="60" maxlength="255"></textarea><br /><br />
<button type="submit" name="submit">Add Infraction</button><br /><br /><br />
</form>
</div>
<div id="footer">
<p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

最佳答案

看起来您正在混淆准备好的语句和查询。查询应该有占位符,PHP 驱动程序将在其中插入值。

这是一种方法..

$sql = "INSERT INTO infractions(userid,username,category,points,reason)VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($userID, $username, $category, $points, $reason));

这是第二种方式......

$sql = ("INSERT INTO infractions(userid,username,category,points,reason)VALUES(:userid, :username, :category, :points, :reason)");
$stmt = $conn->prepare($sql);
$stmt->execute(array(':category'=>$category, ':points'=>$points, ':reason'=>$reason, ':userid' => $userID, ':username'=> $username));

第三种方法

$sql = ("INSERT INTO infractions(userid,username,category,points,reason)VALUES(:userid, :username, :category, :points, :reason)");
$stmt = $conn->prepare($sql);
$stmt->bindParam(":category", $category);
$stmt->bindParam(":points", $points);
$stmt->bindParam(":reason", $reason);
$stmt->bindParam(":userid", $userID);
$stmt->bindParam(":username", $username);
$stmt->execute();

我较少使用绑定(bind),因此可能存在一些问题,但我相信这是正确的用法。

这是关于它的 PHP 手册,http://php.net/manual/en/pdo.prepared-statements.php .

准备语句的原因是将用户提供的数据从 SQL 中分离出来。如果没有分离,您可能会遇到 SQL 注入(inject),或者查询失败,例如 Mr. O'brien 尝试创建帐户/登录。

SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands.

https://www.owasp.org/index.php/SQL_injection

还有一个讨论如何防止这种情况发生的线程

How can I prevent SQL injection in PHP?

form 元素的method 属性告诉表单应该如何传输数据。值为 postget。您似乎也在您的 PHP 中混合了这些。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

post: Corresponds to the HTTP POST method ; form data are included in the body of the form and sent to the server.

get: Corresponds to the HTTP GET method; form data are appended to the action attribute URI with a '?' as separator, and the resulting URI is sent to the server. Use this method when the form has no side-effects and contains only ASCII characters.

PHP 用法:
http://php.net/manual/en/reserved.variables.get.php
http://php.net/manual/en/reserved.variables.post.php

关于php - 将从表中提取的用户名插入到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273881/

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