查询未获得更新

时间:2017-10-03 06:45:43

标签: php mysql

我正在尝试将从文本框中收到的输入插入表格中。我在php页面得到的结果如下: 在输入输入之前,在页面加载我得到 已成功连接错误:INSERT INTO已测试(itemno,item,quantity)VALUES('1',,,)

在输入输入并提交时,我得到结果,连接成功并且item不是空的msg但查询未执行且未插入行。有人能帮助我吗?

<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "learnphp@localhost";
$password = "password";
$dbname = "my_learnphp";
$result="";
$result1="";
$item = $_POST["item"];
$quantity = $_POST["quantity"];

?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = "POST">
 Item name: <input type="text" name="item"><br>
 Quantity : <input type="number" name="quantity"><br>
<input type="submit" name="upd" value="Insert"/>
<input type="submit" name="item_update" value="Update"/>
</form>
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
 if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);} 
echo "Connected successfully";
$sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', $item, $quantity)";
  if (!empty($item)) {
 echo "item is not empty";
 $result = $conn->query($sql);
 if($result){
 echo $_POST["item"];
echo $_POST["quantity"];
    echo "New record created successfully";
    }
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 
</body>
</html>

4 个答案:

答案 0 :(得分:0)

更改语法
$sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', $item, $quantity)";

$sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', '$item', '$quantity')";

答案 1 :(得分:0)

如果在未修改SQL查询的情况下插入用户输入,则应用程序将容易受到Sql Injection的攻击。<​​/ p>

$someVariable = $_POST['user_input'];     
mysql_query("INSERT INTO `table` (`column`) VALUES ('$someVariable')");

用户可以输入类似值'); DROP TABLE表; - ,查询变为:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

所以这不是安全方式。

并且在这个例子中你的错误是你错过了传递查询值的变量中的单引号。

您的查询

  $sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', $item, $quantity)";

新查询

$sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', '$item', '$quantity')";

双引号用于分隔标识符。单引号用于文字。

您可以看到此stack overflow answer

答案 2 :(得分:0)

您应该将值放在单引号下。 这对你有用,但这不会阻止你的代码从SQL注入:

function processReport(){

$customerList = $this->getCustomerList();

foreach ($customerList as $each=>$customerData){

  }
}

所以上面的方法是正确的但不安全。

创建预准备语句时,您可以创建一个查询,在其中添加占位符而不是原始值:

$report->expects($this->any())
        ->method('getCustomerList')
        ->will($this->returnValueMap(array('0'=>array('id'=>'665'))));

    $report->processReport($options);

问号是占位符,后来使用bind_param方法替换:

{{1}}

绑定调用的ss部分告诉mysql db它传递给数据库的两个字符串。

(s代表字符串,i代表int等)。

答案 3 :(得分:0)

使用准备好的陈述。它会阻止sql注入

$sql = "INSERT INTO tested (itemno, item, quantity) VALUES (?,?,?)";//3placeholders for three variables 

$statement = $conn->prepare($sql); 
$statement->bind_param('iss',1,$item, $quantity);//the letter i denotes an integer, while s is a string 
if(statement->execute() === true) {//did it save successfully?
//everything went fine
echo 'saved'; 
} else {
echo $conn->error; 
}