0

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

I am using 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.

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
  • 2
    Is everything else submitting to the db? What type is that image, think that matters, or you could convert to string, but [this](http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code) question will help. – PurpleSmurph Sep 17 '15 at 10:13
  • rather than storing the image to the db you should consider uploading the the image in the local file system and store the image-location-path in the database – benscabbia Sep 17 '15 at 11:37
  • Don's store large images in database, store them, as @gudthing suggested in the local file system and only store the reference to the path in database. Unless the images a quite small, then you could go ahead and store them in database in a BLOB. Question: Is your image posted as BASE64 binary data or ? – DTH Sep 17 '15 at 11:39

3 Answers3

1

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.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

You must set a BLOB column for S_car_image into your table, after it's very simple, do this before your query :

$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. PS2 : Perhaps you should look this : http://php.net/manual/en/mysqli.real-escape-string.php (mysqli_real_escape_string ($connection, $content);

Servuc
  • 328
  • 1
  • 4
  • 16
0

You have to retrieve the file from $_FILES['userfile']['tmp_name'], but have to retrieve it with fopen() from the uploaded file; 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:

$S_car_image = "X'$image'";

Be sure your form has enctype="multipart/form-data". The database field must be capable of holding binary data, so you have to use a BLOB field. Also you have store the mime type ($_FILES['S_car_image']['type']) in another database field for setting a proper output header.

Be sure to have read the documentation on file uploads in PHP throroghly. And please get informed about SQL injection attacks, as your code is vulnerable to that.

syck
  • 2,984
  • 1
  • 13
  • 23