- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这让我抓狂,我正在学习 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">© 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
属性告诉表单应该如何传输数据。值为 post
或 get
。您似乎也在您的 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/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!