我试图在内容表中插入值。如果我在VALUES中没有PHP变量,它就能正常工作。当我把变量 $type
里面 VALUES
那这就不行了。我到底做错了什么?
$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description)
VALUES($type, 'john', 'whatever')");
在任何MySQL语句中添加一个PHP变量的规则非常简单。
因此,由于你的例子只涉及到数据字面,那么所有的变量都必须通过占位符(也称为参数)来添加。要做到这一点。
而这里是如何用所有流行的PHP数据库驱动来实现的。
这样的司机 不存在.
mysqli
$type = 'testing';
$reporter = "John O'Hara";
$query = "INSERT INTO contents (type, reporter, description)
VALUES(?, ?, 'whatever')";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ss", $type, $reporter);
$stmt->execute();
代码有点复杂,但所有这些操作符的详细解释可以在我的文章中找到。如何使用Mysqli运行INSERT查询?以及一个能大大简化流程的解决方案。
$type = 'testing';
$reporter = "John O'Hara";
$query = "INSERT INTO contents (type, reporter, description)
VALUES(?, ?, 'whatever')";
$stmt = $pdo->prepare($query);
$stmt->execute([$type, $reporter]);
在PDO中,我们可以把绑定和执行部分结合起来,这非常方便。PDO还支持命名占位符,有些人觉得非常方便。
但有时我们添加了一个变量,它代表了查询的另一部分,比如一个关键字或一个标识符(一个数据库、表或字段名)。在这种情况下,你的变量必须与一个值列表进行核对。明确 写在你的脚本中。这在我的另一篇文章里有解释。根据用户的选择,在ORDER BY子句中添加字段名。:
遗憾的是,PDO没有标识符(表和字段名)的占位符,因此开发者必须手动过滤掉它们。这样的过滤器通常被称为 "白名单"(我们只列出允许的值),而不是 "黑名单"(我们列出不允许的值)。
所以我们必须在PHP代码中明确列出所有可能的变体,然后从中选择。
下面是一个例子。
$orderby = $_GET['orderby'] ?: "name"; // set the default value
$allowed = ["name","price","qty"]; // the white list of allowed field names
$key = array_search($orderby, $allowed, true); // see if we have such a name
if ($key === false) {
throw new InvalidArgumentException("Invalid field name");
}
同样的方法也应该用在方向上。
$direction = $_GET['direction'] ?: "ASC";
$allowed = ["ASC","DESC"];
$key = array_search($direction, $allowed, true);
if ($key === false) {
throw new InvalidArgumentException("Invalid ORDER BY direction");
}
经过这样的代码,既 $direction
和 $orderby
变量可以安全地放在SQL查询中,因为它们要么等于允许的变体之一,要么就会出现错误。
最后要提到的是标识符,它们也必须根据特定的数据库语法进行格式化。对于MySQL来说,它应该是 backtick
标识符周围的字符。因此,我们的订单的最终查询字符串将是
$query = "SELECT * FROM `table` ORDER BY `$orderby` $direction";
为了避免SQL注入,在插入语句中加入
$type = 'testing';
$name = 'john';
$description = 'whatever';
$stmt = $con->prepare("INSERT INTO contents (type, reporter, description) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $type , $name, $description);
$stmt->execute();
最好的选择是准备好的报表。 乱用引号和转义符是比较难的工作,一开始就很难维持。迟早你会不小心忘记引用一些东西,或者最后把同一个字符串转义两次,或者把类似的东西弄乱。可能几年后你才会发现这些类型的错误。
$type里面的文字直接代入插入字符串,因此MySQL得到的是这样的结果。
... VALUES(testing, 'john', 'whatever')
请注意,测试周围没有引号,你需要把这些内容像这样放进去。
$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description) VALUES('$type', 'john', 'whatever')");
我也建议你读一读 SQL注入如果你不对所使用的数据进行消毒,这种参数传递很容易被黑客尝试。
试试这个
$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description) VALUES('$type', 'john', 'whatever')");
你需要把 '$type'
不只是$type
这就是简单的答案。
$query="SELECT * FROM CountryInfo WHERE Name = '".$name."'";
你来定义 $name
无论你想要什么。而另一种方式,复杂的方式是这样的。
$query = " SELECT '" . $GLOBALS['Name'] . "' .* " .
" FROM CountryInfo " .
" INNER JOIN District " .
" ON District.CountryInfoId = CountryInfo.CountryInfoId " .
" INNER JOIN City " .
" ON City.DistrictId = District.DistrictId " .
" INNER JOIN '" . $GLOBALS['Name'] . "' " .
" ON '" . $GLOBALS['Name'] . "'.CityId = City.CityId " .
" WHERE CountryInfo.Name = '" . $GLOBALS['CountryName'] .
"'";
如果变量中包含了用户的输入或者其他你不信任的数据 一定要把数据转义。就像这样。
$query = sprintf("INSERT INTO contents (type) VALUES ('%s')", mysql_real_escape_string($type));
$result = mysql_query($query);
这里
$type='testing' //it's string
mysql_query("INSERT INTO contents (type, reporter, description) VALUES('$type', 'john', 'whatever')");//at that time u can use it(for string)
$type=12 //it's integer
mysql_query("INSERT INTO contents (type, reporter, description) VALUES($type, 'john', 'whatever')");//at that time u can use $type
我知道这个问题已经有几个答案了,但我想补充一下,如果你按照下面的语法,我再也没有遇到过错误的问题。 没有问题,你用的是哪个表,追加的是哪个列。
$query = "INSERT INTO contents (type, reporter, description)
VALUES('".$type."', '".$reporter."', '"$whatever."')";