我想将名称、描述和图像插入到我的数据库中。这段代码有什么问题?
<?php
$host="localhost";
$user="root";
$password="";
$database="jin";
$sql = mysqli_connect($host,$user,$password,$database) or die("cannot connect");
if(isset($_POST['submit']))
{
$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$name = $_POST['name'];
$description = $_POST['description'];
$query = "INSERT INTO cai(name,description,image,) VALUES ('$name','$description','$file')";
if(mysqli_query($sql,$query))
{
echo"uploaded";
}
else {
echo"not inserted";
}
}
?>
最佳答案
1)
请勿使用 root
在 PHP 中连接到数据库。使用适用于 PHP 且具有最低权限的新“专家”SQL 用户。否则,这是不好的做法并且存在潜在的安全漏洞。
2)
从列名称列表中删除最后一个逗号...image,)
3)
将列名称放在反引号中(这些家伙:“`”;通常在拉丁键盘上的数字 1 旁边)。
3b)
避免使用Reserved words .
4)
5)
正确输出 MySQLi 错误
mysqli_query($sql,$query) or error_log(print_r(mysqli_error($sql),true));
它将向您的错误日志文件输出精确的 MySQLi 错误。
6)
使用准备好的语句。 Huh?
关于php - 我想将名称、描述和图像插入到数据库中。我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625414/