0

I am trying to store URL Image paths into MySQL DB. But i am not getting any results with my code.

  • Why is it not storing anything?
  • Would it be better just to store the image name or the entire path? target example: "www.example.com/medium/imagename.jpg"

Table Name: urlimage

id: autoincrement
image_name 



Php code for inserting data into DB
<?php

$images = explode(',', $_GET['i']);

$path = Configuration::getUploadUrlPath('medium', 'target');


if(is_array($images)){

    $objDb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
    $objDb->exec('SET CHARACTER SET utf8');

    $sql = "INSERT INTO `urlImage` (`image_name`) VALUES ";

  foreach ($images as $image) {

    //echo '<div><p>' . $path . $image . '</p><img src="' . $path . $image . '" /></div>';
    $value[] = "(".$path.$image.")"; // collect imagenames
  }

  $sql .= implode(',', $value).";"; //build query

  $objDb->query($sql);   
}

?>
5
  • 1
    You should store only image file-name in database. Commented Jul 5, 2012 at 5:18
  • debug by print_r($sql) just before $objDb->query($sql); Commented Jul 5, 2012 at 5:19
  • Are you getting any errors? In your php.ini set error_reporting = E_ALL | E_STRICT and display_errors = On if you have not done so. Commented Jul 5, 2012 at 5:21
  • 1
    of course, it's better to store filenames instead of entire path. Think about it: Entire path takes lot's memory for the same string... Define simple const or var instead of path Commented Jul 5, 2012 at 5:33
  • @diEcho yea after putting what you told me i got: "Parse error: syntax error, unexpected T_VARIABLE" for $objDb->query($sql); Commented Jul 5, 2012 at 5:34

2 Answers 2

2

Try this; it may work:

$value[] = "('".$path.$image."')";
Sign up to request clarification or add additional context in comments.

Comments

0

you have error in $sql

it must be $value[] = "('".$path.$image."')";

debug with

 echo "<pre>"; print_r($sql);echo "</pre>";

and if your using PDO function then call

prepare->execute

1 Comment

yea after putting what you told me i got: "Parse error: syntax error, unexpected T_VARIABLE" for $objDb->query($sql);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.