1

I am trying below code for upload multiple image with database using php mysql but not working kindly help me how to upload multiple image in php mysql. and coming error 558d45b0b348a

Notice: Undefined variable: sql in D:\xampp\htdocs\app\db.php on line 37

QUERY FAILED !!!

<?php
    session_start();
    include_once('configuration.php');
    include_once('db.php');
        if (isset($_REQUEST['submit'])){
        echo $uname = uniqid();
            move_uploaded_file($_FILES["shop_img"]["tmp_name"][0],
             "uploaded/" . $_FILES["shop_img"]["name"][0]);
          $sql="INSERT INTO shop_list ('shop_img') 
            VALUES (:shop_img);";
            $sql_result = $db->queryPrepared($sql,array(
                ':shop_img' =>$_FILES["shop_img"]["name"][0]
            ));

            $lastId = $db->last_insert_id();
            print_r($_FILES);
            exit;
            foreach($_FILES['shop_img']['name'] as $k=>$v){
                $type = $_FILES['shop_img']['type'][$k];
                $name = $_FILES['shop_img']['name'][$k];
                $temp_name = $_FILES['shop_img']['tmp_name'][$k];
                $imgUniqName = uniqid().'.'.$type;
                $sqlImg = "INSERT INTO shop_images (shop_id, image) 
                VALUES (:shop_id, :img_name);";
                $sqlImgResult = $db->queryPrepared($sqlImg,array(
                ':shop_id' => $lastId,
                ':img_name' => $name
                ));
                move_uploaded_file($temp_name,"uploaded/" . $name);
                }
            $msg = "<b style='color: green;'>Image upload sucessfully</b>";
        }
    ?> 
<form action="" enctype="multipart/form-data" method="post">
    <?php if(isset($msg)) echo $msg; ?>
    <h3><a>Store Image</a></h3>
    <table>
        <tr>
            <td>Store</td>
            <td><input type="file" class="form-control" name="shop_img[]" id="shop_img" required="true" multiple /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Submit" name="submit" class="btn" /></td>
        </tr>
    </table>
</form>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SIL
  • 185
  • 1
  • 2
  • 11
  • 2
    your error looks... 'clear' no? look at db.php on line 37? ^^ – Julo0sS Jun 26 '15 at 12:43
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jun 27 '15 at 01:00

1 Answers1

1
<?php
session_start();
include_once('configuration.php');
include_once('db.php');
    if (isset($_REQUEST['submit'])){
        $sqlMax = "SELECT * FROM `shop_list` ORDER BY `shop_id` DESC LIMIT 0, 1"; 
        $sqlMaxResult = $db->fetchQuery($sqlMax);
        $maxId = (int)$sqlMaxResult[0]['shop_id'] + 1;
        $imageList = array();

        foreach($_FILES['shop_img']['name'] as $k=>$v){
            $type = $_FILES['shop_img']['type'][$k];
            $name = $_FILES['shop_img']['name'][$k];
            $temp_name = $_FILES['shop_img']['tmp_name'][$k];
            $imgStr = $maxId.'-'.$name;
            $imgUniqName = uniqid().'.'.$type;
                if(move_uploaded_file($temp_name,"uploaded/" . $name)){
                    array_push($imageList,$imgStr);
                }
            }
          $sql="INSERT INTO `shop_list` (`shop_img`)
            VALUES (:shop_img);";
            $sql_result = $db->queryPrepared($sql,array(
                ':shop_img' =>implode("#",$imageList)
            ));
        $msg = "<b style='color: green;'>Image upload sucessfully</b>";
    }
?> 
<form action="" enctype="multipart/form-data" method="post">
    <?php if(isset($msg)) echo $msg; ?>
    <h3><a>Store Image</a></h3>
    <table>
        <tr>
            <td>Store</td>
            <td><input type="file" class="form-control" name="shop_img[]" id="shop_img" required="true" multiple /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Submit" name="submit" class="btn" /></td>
        </tr>
    </table>
</form>
Pankaj Upadhyay
  • 2,114
  • 21
  • 22