简体   繁体   English

我如何在数据库中插入图像?

[英]how i can insert a image in the database?

My table name is tbl_selluaed_car and my field is ( S_car_id, S_car_name, S_car_model, S_car_fueltype, S_car_date, S_car_color, S_car_seat, S_car_engine, S_car_description, S_car_image, S_car_price, S_car_cit, S_car_state) I want insert in the database carwala 我的表名是tbl_selluaed_car和我场是( S_car_id, S_car_name, S_car_model, S_car_fueltype, S_car_date, S_car_color, S_car_seat, S_car_engine, S_car_description, S_car_image, S_car_price, S_car_cit, S_car_state)我想在数据库中插入carwala

I am using php 5.3 我正在使用php 5.3
My code is: 我的代码是:

<?php
$hostname = "localhost";
$username = "root";
$password ="";
$dbname="carwala";

$dbc = mysqli_connect($hostname,$username,$password,$dbname);


echo "you are connected";
$S_car_id = $_POST['S_car_id'];
$S_car_name = $_POST['S_car_name'];
$S_car_model = $_POST['S_car_model'];
$S_car_fueltype = $_POST['S_car_fueltype'];
$S_car_date = $_POST['S_car_date'];
$S_car_color = $_POST['S_car_color'];
$S_car_seat = $_POST['S_car_seat'];
$S_car_engine = $_POST['S_car_engine'];
$S_car_description = $_POST['S_car_description'];
$S_car_image=$_FILES["S_car_image"];
$S_car_price = $_POST['S_car_price'];
$S_car_city = $_POST['S_car_city'];
$S_car_state = $_POST['S_car_state'];

mysqli_query($dbc,"INSERT INTO       tbl_selluaed_car(S_car_name,S_car_model,S_car_fueltype,S_car_date,S_car_color,S_car_seat,S_car_engine,S_car_description,S_car_image,S_car_price,S_car_city,S_car_state) VALUES('$S_car_name','$S_car_model','$S_car_fueltype','$S_car_date','$S_car_color','$S_car_seat','$S_car_engine','$S_car_description','$S_car_image','$S_car_price','$S_car_city','$S_car_state')");
$reg=mysqli_affected_rows($dbc);
echo $reg. " your DATA is Send to Carwala";
?>

But image not inserted. 但是没有插入图像。

You don't want to insert the actual image file to the database. 您不想将实际的图像文件插入数据库。 This is horrible inefficient. 这是可怕的低效。

Instead, upload the image to your local file system, and then store the name of the file in your table. 而是将映像上传到本地文件系统,然后将文件名存储在表中。

You must set a BLOB column for S_car_image into your table, after it's very simple, do this before your query : 您必须在表中设置S_car_imageBLOB列,这非常简单,请在查询之前执行此操作:

$fp      = fopen($_FILES["S_car_image"]["tmp_name"], 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);

PS : Look at PDO PHP, it's better to accessing your database. PS:查看PDO PHP,最好访问您的数据库。 PS2 : Perhaps you should look this : http://php.net/manual/en/mysqli.real-escape-string.php ( mysqli_real_escape_string ($connection, $content); PS2:也许您应该看一下: http : mysqli_real_escape_string ($connection, $content);

You have to retrieve the file from $_FILES['userfile']['tmp_name'] , but have to retrieve it with fopen() from the uploaded file; 您必须从$_FILES['userfile']['tmp_name']检索文件,但必须使用fopen()从上传的文件中检索文件; it is not automatically present in the scripts memory. 它不会自动出现在脚本存储器中。 So, in general: 因此,通常:

$FH = fopen($_FILES['S_car_image']['tmp_name'], 'r');
$image = fread($FH, 999999);
fclose($FH);

Since you are inserting binary data, you should escape it with X'<data>' before submitting it to the database: 由于要插入二进制数据,因此在将其提交到数据库之前,应使用X'<data>'将其转义:

$S_car_image = "X'$image'";

Be sure your form has enctype="multipart/form-data". 确保您的表单具有enctype =“ multipart / form-data”。 The database field must be capable of holding binary data, so you have to use a BLOB field. 数据库字段必须能够保存二进制数据,因此您必须使用BLOB字段。 Also you have store the mime type ( $_FILES['S_car_image']['type'] ) in another database field for setting a proper output header. 另外,您已将哑剧类型( $_FILES['S_car_image']['type'] )存储在另一个数据库字段中,用于设置适当的输出标头。

Be sure to have read the documentation on file uploads in PHP throroghly. 一定要仔细阅读PHP中有关文件上传的文档。 And please get informed about SQL injection attacks, as your code is vulnerable to that. 并且请了解有关SQL注入攻击的信息,因为您的代码容易受到攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM