0

Let's read the image file into varialble picture:

$picture = addslashes(fread(fopen($image, "r"), filesize($image))); 

This $picture you can easy insert into database table with no trouble.

*for example*:  INSERT INTO $banners(banner) VALUES( $picture );

For some reason lets create an associative array $final:

$final["banner"] = $picture;
$final["place"] = something...

Later lets decompose $final and insert the obtained values into database:

  $fields = "";      $values = "";

  while (list($name, $value) = each( $final ))
  {
        $fields .= "$name, ";
        $values .= "'$value', ";
  }
  // Cut trailing commas
  $values_fields = ereg_replace(", $", "", $values_fields);
  $values = ereg_replace(", $", "", $values);

  // Execute query
  $query = "INSERT INTO banners($values_fields) VALUES($values)";
  $res = mysql_db_query($database, $query) or mysql_die();     

Now MySQL warns "Something wrong" when comes to insert consecutive $value with $picture into database. Why?

2 Answers2

1

First, don't destroy your data. Read it directly and keep the variable clean:

$picture = file_get_contents($image);

Next, prepare the data for insertion:

$final["banner"] = mysqli_real_escape_string($picture);
$final["place"]  = $something;

Last, there is no need to loop through your array, since it only contains one record. You don't quote the values, causing an error.

$fields = "`" . implode("`, `", array_keys($final)) . "`";
$values = "'" . implode("', '", array_values($final)) . "'";

$query  = "INSERT INTO banners ({$fields}) VALUES ({$values})";
$result = mysqli_query($database, $query) or die(mysqli_error($database));

I'm using MySQLi here, since the mysql_* functions are deprecated (as well as ereg_* functions).

nibra
  • 3,688
  • 2
  • 15
  • 32
  • there is no need to loop through your array, since it only contains one record... – user2279628 Apr 17 '13 at 15:51
  • --- I must explain that array will contain many records since it collects a different information not presented here. Therefore it is an associative array. I can pass the trouble by naming the detailed fields, but the good way is to use the generalized names as $values_fields and $values. (Extra info: I use PHP4.3.9) – user2279628 Apr 17 '13 at 16:01
  • 1. Then you'll have an array with many of these `$final` arrays. Each one has to be treated the same way. – nibra Apr 19 '13 at 16:03
  • You don't have `$values_fields`; you mean `$fields`. - [PHP4](http://www.php.net/archive/2007.php#2007-07-13-1) does not exist anymore. – nibra Apr 19 '13 at 16:09
0

If the code you posted here is exactly the one you are trying to run then please note that you are accumulating field names in $fields variable but "cut trailing commas" from $values_fields which is at this point empty. Putting empty $values_fields into your query might be the cause of mysql error.

Why are you doing addslashes()? Try to use mysql_real_escape_string() instead.

Also make sure that the type of the database column where you are trying to put your image into is BLOB or LONGBLOB.

You may find answer to this question Binary Data in MySQL relevant.

Community
  • 1
  • 1
Kamil Szot
  • 15,477
  • 6
  • 53
  • 62