经由可变mysqli_query输入(mysqli_query inputs via variabl

2019-08-18 00:58发布

我试图将信息添加到使用下面的PHP代码一个MySQL表。 (输入从基本的HTML5 Web表单的名称和文本。)可能是一个语法问题?

<?php
include "dbinfo.php"; //contains mysqli_connect information (the $mysqli variable)
//inputs
$name = $_GET["name"];
$text = $_GET["text"];

$sqlqr = 'INSERT INTO `ncool`.`coolbits_table` (`name`, `text`, `date`) VALUES ("$name", "$text", CURRENT_TIMESTAMP);'; //the query. I'm pretty sure that the problem is a syntax one, and is here somewhere.

mysqli_query($mysqli,$sqlqr); //function where the magic happens.
?>

不引发错误。 一个空白的屏幕效果,并以“$ name”和“$文字”行添加到MySQL表。

Answer 1:

这是你的代码应该是什么样子(加入SQL注入防护):

<?php
include "dbinfo.php"; //contains mysqli_connect information (the $mysqli variable)
//inputs
$name = mysqli_real_escape_string($_GET['name']);
$text = mysqli_real_escape_string($_GET['text']);

$sqlqr = "INSERT INTO `ncool`.`coolbits_table` (`name`, `text`, `date`) VALUES ('" . $name . "', '" . $text . "', CURRENT_TIMESTAMP);";

mysqli_query($mysqli,$sqlqr); //function where the magic happens.
?>

看看我做了什么。 首先,我已经逃脱你获取到用户输入$name$text变量(这是相当多出于安全原因必须的)和其他人所说,你最好使用准备好的语句。

问题是,你没有与周围的单引号('),它是SQL语法的要求的字符串值。

我希望这有助于回答你的问题。



Answer 2:

首先:您使用mysqli准备语句以防止SQL注入攻击。 它是不是安全使用查询中输入用户没有适当的转义。 预处理语句是防止这种有用的。

第二:你应该学会如何进行字符串引用在PHP的作品, 单引号字符串和双引号的字符串是不同的

我会推荐阅读PHP文件有关的字符串引用。



Answer 3:

   $sqlqr = 'INSERT INTO `ncool`.`coolbits_table` (`name`, `text`, `date`) VALUES ("'.$name.'", "'.$text.'", CURRENT_TIMESTAMP)';

保持你的瓦尔引号外面。



文章来源: mysqli_query inputs via variable