![](/img/trans.png)
[英]Inputted values not saved while inserting data from html form into sql database (html,php,sql)
[英]Getting value from HTML and inserting into PHP SQL to get data from database
我正在尝试使用 POST 从数据库中获取数据。 在示例中,如果 'storage' = 2GB,我想要数据库中的所有笔记本电脑
HTML:
<form action="getdata.php" method="POST">
<input type='hidden' name='storage' value='2GB'/>
<input type="submit">
</form>
PHP:
<?php
$sql = "SELECT * FROM laptops WHERE storage=$_POST["storage"] ;";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['id']. "<br>";
}
}
?>
不知何故, $_POST["storage"]='2GB'
部分无法正常工作。 我一定是做错了什么,因为当我使用以下命令测试 POST 时它正在工作: echo $_POST["storage"];
返回“2GB”。
首先,您的脚本存在很大的攻击风险,您需要在 SQL 查询中使用之前验证和转义数据,更好的是,使用准备好的语句。
现在,您的问题是您试图在字符串中包含一个数组项,并且必须将其括在大括号之间并将值括在单引号之间。
<?php
$sql = "SELECT * FROM laptops WHERE storage='{$_POST['storage']}' ;";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['id']. "<br>";
}
}
?>
所以事实证明我需要
include_once 'testing/databaseconn.php';
$conn 定义的地方............这样的n00b,花1小时在这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.