gpt4 book ai didi

php - 用引号将字符串括起来

转载 作者:可可西里 更新时间:2023-10-31 22:53:40 25 4
gpt4 key购买 nike

PHP 中有给字符串添加引号的函数吗?

喜欢 "'".str."'"

这是针对带有 varchars 的 sql 查询。我搜索了一下,没有结果...

我做了以下事情:

$id = "NULL";
$company_name = $_POST['company_name'];
$country = $_POST['country'];
$chat_language = $_POST['chat_language'];
$contact_firstname = $_POST['contact_firstname'];
$contact_lastname = $_POST['contact_lastname'];
$email = $_POST['email'];
$tel_fix = $_POST['tel_fix'];
$tel_mob = $_POST['tel_mob'];
$address = $_POST['address'];
$rating = $_POST['rating'];

$company_name = "'".mysql_real_escape_string(stripslashes($company_name))."'";
$country = "'".mysql_real_escape_string(stripslashes($country))."'";
$chat_language = "'".mysql_real_escape_string(stripslashes($chat_language))."'";
$contact_firstname = "'".mysql_real_escape_string(stripslashes($contact_firstname))."'";
$contact_lastname = "'".mysql_real_escape_string(stripslashes($contact_lastname))."'";
$email = "'".mysql_real_escape_string(stripslashes($email))."'";
$tel_fix = "'".mysql_real_escape_string(stripslashes($tel_fix))."'";
$tel_mob = "'".mysql_real_escape_string(stripslashes($tel_mob))."'";
$address = "'".mysql_real_escape_string(stripslashes($address))."'";
$rating = mysql_real_escape_string(stripslashes($rating));

$array = array($id, $company_name, $country, $chat_language, $contact_firstname,
$contact_lastname, $email, $tel_fix, $tel_mob, $address, $rating);
$values = implode(", ", $array);

$query = "insert into COMPANIES values(".$values.");";

最佳答案

与其将值直接插入查询,不如使用 prepared statements和参数,它们不易受到 SQL injection 的攻击.

$query = $db->prepare('SELECT name,location FROM events WHERE date >= ?');
$query->execute(array($startDate));

$insertContact = $db->prepare('INSERT INTO companies (company_name, country, ...) VALUES (?, ?, ...)');
$insertContact->execute(array('SMERSH', 'USSR', ...));

创建一个 PDO 对象(它也连接到数据库,因此是 mysql_connect 的对应物)很简单:

$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'passwd');

您不应该将其散布在每个需要数据库连接的脚本中。一方面,它更像是一种安全风险。另一方面,您的代码更容易出现拼写错误。该解决方案解决了这两个问题:创建一个函数或方法来设置数据库连接。例如:

function localDBconnect($dbName='...') {
static $db = array();
if (is_null($db[$dbName])) {
$db[$dbName] = new PDO("mysql:host=localhost;dbname=$dbName", 'user', 'passwd');
$db[$dbName]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $db[$dbName];
}

如果您使用的数组超过两个或三个元素,则应使用循环或数组函数,而不是像示例代码中那样使用一长串相似的语句。例如,您的大部分示例都可以替换为:

$array = array();
foreach ($_POST as $key => $val) {
$array[$key] = "'" . mysql_real_escape_string(stripslashes($val)) . "'";
}

下面是创建插入查询的更全面的示例。它远未准备好生产,但它说明了基础知识。

$db = localDBconnect();

// map input fields to table fields
$fields = array(
'company' => 'company_name',
'country' => 'country',
'lang' => 'chat_language',
'fname' => 'contact_firstname',
'lname' => 'contact_lastname',
'email' => 'email',
'land' => 'tel_fix',
'mobile' => 'tel_mob',
'addr' => 'address',
'rating' => 'rating',
);
if ($missing = array_diff_key($fields, $_POST)) {
// Form is missing some fields, or request doesn't come from the form.
...
} else {
$registration = array_intersect_key($_POST, $fields);

$stmt = 'INSERT INTO `dbname`.`Companies` (`'
. implode('`, `', $fields) . '`) VALUES ('
. implode(', ', array_fill(0, count($registration), '?')) . ')';
try {
$query = $db->prepare($stmt);
$query->execute(array_values($registration));
} catch (PDOException $exc) {
// log an
error_log($exc);
echo "An error occurred. It's been logged, and we'll look into it.";
}
}

为了使它的生产准备就绪,应该将代码重构为函数或类,以隐藏代码其余部分与数据库相关的所有内容;这称为“data access layer”。 $fields 的使用展示了一种编写适用于任意表结构的代码的方法。查找“Model-View-Controller”架构以获取更多信息。此外,还应执行验证。

关于php - 用引号将字符串括起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2304317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com