gpt4 book ai didi

php - 将 SQL 转换为 PDO 以防止 SQL 注入(inject)

转载 作者:行者123 更新时间:2023-11-29 00:30:19 25 4
gpt4 key购买 nike

<分区>

就开发而言,我对 php 和 MySQL 还是个新手。我希望得到一些建议,以确保我做的每件事都是正确的。我有一页我认为已正确转换,还有一页需要转换。

这是我设置的那个正确吗?

<?php $title = 'SEARCH'; $page = '';include 'includes/header.php';?>

<body>

<?php include 'includes/nav.php'; ?>

<?php
$q = $_GET['q'];

// CONNECT TO THE DATABASE
$DB_NAME = 'code_storage';
$DB_HOST = 'localhost';
$DB_USER = 'user';
$DB_PASS = 'pass';

try {
$dbcon = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASS);
//echo 'Connected to database';

$sql = <<<SQL
SELECT *
FROM snippets
WHERE CODE_NAME LIKE '%$q%' OR
CODE_DESC LIKE '%$q%' OR
CODE_TAGS LIKE '%$q%' OR
CODE_USAGE LIKE '%$q%'
SQL;
echo '<div class="row">';
echo '<div class="panel">';
printf("Your search for <b>$q</b> returned %d records.\n", $dbcon->query($sql)->rowCount());
echo '</div>';
echo '</div>';

foreach ($dbcon->query($sql) as $row) {
//print $row['CODE_NAME'] . '<br/>' . "\n";
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <b><a href="results.php?id=' . $row['_ID'] . '">' . $row['CODE_NAME'] . '</a></b><br/><br/>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . '<br/><br/>' . "\n";
}


$dbcon = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<?php include 'includes/footer.php';?>

这是我仍然需要转换的那个,这是我真正需要一些帮助的一次

<?php

// CONNECT TO THE DATABASE
$DB_NAME = 'code_storage';
$DB_HOST = 'localhost';
$DB_USER = 'user';
$DB_PASS = 'pass';


$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// Fix for the ' and "

$_POST['name'] = $mysqli->real_escape_string($_POST['name']);
$_POST['desc'] = $mysqli->real_escape_string($_POST['desc']);
$_POST['usage'] = $mysqli->real_escape_string($_POST['usage']);
$_POST['code'] = $mysqli->real_escape_string($_POST['code']);
$_POST['tags'] = $mysqli->real_escape_string($_POST['tags']);

$sql = <<<SQL
INSERT
INTO snippets
(CODE_NAME,CODE_DESC,CODE_USAGE,CODE_SYNTAX,CODE_TAGS)
VALUES
('$_POST[name]', '$_POST[desc]', '$_POST[usage]', '$_POST[code]', '$_POST[tags]');
SQL;

if(!$result = $mysqli->query($sql)){
echo '<br/><br/><br/><br/>' . "\n";
echo '<div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <div data-alert class="alert-box alert">' . "\n";
echo ' There was an error' . "\n";
echo ' <a href="../site/upload.php" class="close">&times;</a>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo '</div>' . "\n";
echo '<br/><br/><br/><br/>' . "\n";
echo '<div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
die('There was an error with the code [' . $mysqli->error . ']');
echo ' </div>' . "\n";
echo '</div>' . "\n";

}
echo '<br/><br/><br/><br/>' . "\n";
echo '<div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <div data-alert class="alert-box success">' . "\n";
echo ' Code Successfully Added' . "\n";
echo ' <a href="../site/" class="close">&times;</a>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo '</div>' . "\n";
echo '<br/><br/><br/><br/>' . "\n";
*/

?>

*编辑*到目前为止,这是我所拥有的,但它似乎没有返回任何记录,有什么想法吗?

 $q = $_GET['q'];

$DB_NAME = 'code_storage';
$DB_HOST = 'localhost';
$DB_USER = 'user';
$DB_PASS = 'pass';


$dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db = new PDO($dsn, $DB_USER, $DB_PASS);

$query = "SELECT * FROM `SNIPPETS` WHERE `CODE_NAME` LIKE :name OR `CODE_DESC` LIKE :name OR `CODE_TAGS` LIKE :name OR `CODE_USAGE` LIKE :name";
$prep = $db->prepare($query);
$prep->execute(array(":name" => "%" . $q . "%"));

echo '<div class="row">';
echo '<div class="panel">';
printf("Your search for <b>$q</b> returned %d records.\n", $prep->rowCount());
echo '</div>';
echo '</div>';

while ($row = $prep->fetch()) {
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";

echo $row['CODE_NAME'] . '<br/><br/>' . "\n";
echo $row['CODE_DESC'] . '<br/><br/>' . "\n";
echo $row['CODE_USAGE']. '<br/><br/>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . '<br/><br/>' . "\n";


}

$db = null;

我发现我遇到了这个错误

Connection failed: SQLSTATE[HY093]: Invalid parameter number: number of bound    variables does not match number of tokens

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