上传图像并动态存储到数据库中

时间:2015-10-25 08:24:57

标签: php image upload

这是Editprofile.php页面,它动态显示组件。在这里,我从数据库中提取食物类别,并同时向数据库添加多个项目      
                                           

                                  ?>
                                  <tr>
                                  <?php
                             for($j=1;$j<3;$j++)
                              {
                                  ?>
                                  <td ><table width="490" height="445" style="border:solid; margin-left:20%;">
                                    <tr>
                                      <td height="7"></td>
                                      <td height="7"></td>
                                      <td height="7" colspan="2"></td>
                                      <td height="7"></td>
                                      <td height="7"></td>
                                    </tr>
                                    <tr>
                                      <?php

        $db_host='localhost';
        $db_user='root';
        $db_pass='';
        $db_database='foodwrap1';
        $con=mysqli_connect("$db_host", "$db_user", "$db_pass")or die("cannot connect"); 
        mysqli_select_db($con,$db_database)or die("cannot select DB");
        $id=$_SESSION['restid']; 
        $sql="Select cat_id,cat_name from category where rest_id=$id";
        $result=mysqli_query($con,$sql) or die(mysql_error()."no data");

                         ?>
                                      <td width="7" height="39"></td>
                                      <td >Select Category</td>
                                      <?php $scat="cat".''.$k; ?>
                                      <td colspan="3"><select name="<?php echo $scat;?>" onChange="">
                                        <option value="choose">Choose</option>
                                        <?php

//从数据库中获取类别的数据。

while($row=mysqli_fetch_row($result))
                     {


                        $ca = $row[1];
                    ?>
                                        <option value="<?php echo $row[0]; ?>"><?php echo $ca;}?></option>
                                      </select></td>
                                      <td width="7"></td>
                                    </tr>
                                    <tr>
                                      <td></td>
                                      <td width="103" height="46">Item name</td>
                                      <td colspan="3"><input type="Text" name="<?php echo "itemname".''.$k;?>" value=" "></td>//dynamic name allocation eg //itemname1 itemname2
                                      <td></td>
                                    </tr>
                                    <tr>
                                      <td></td>
                                      <td width="103" height="41">Item Price</td>
                                      <td colspan="3"><input type="number" name="<?php echo "itemprice".''.$k;?>" ></td>
                                      <td></td>
                                    </tr>
                                    <tr>
                                      <td></td>
                                      <td width="103" height="209" style="padding-top:8px; vertical-align:text-top">Item description</td>
                                      <td colspan="3"><textarea name="<?php echo "itemdesc".''.$k;?>" rows="5"cols="20" wrap="soft"></textarea></td>
                                      <td></td>
                                    </tr>
                                    <tr>
                                      <td></td>
                                      <td width="103" height="39">Item discount</td>
                                      <td colspan="3"><input type="number" name="<?php echo "itemdis".''.$k;?>" ></td>
                                      <td></td>
                                    </tr>
                                    <tr>
                                      <td></td>
                                      <td width="103" height="40">Item veg</td>
                                      <td width="91"><input type="radio" name="<?php echo "itemveg".''.$k;?>" value="yes" checked >
                                        Yes</td>
                                      <td width="92"><input type="radio" name="<?php echo "itemveg".''.$k;?>" value="no" >
                                        No</td>
                                      <td width="20">&nbsp;</td>
                                      <td></td>
                                    </tr>

          <tr>
          <td></td>
            <td><input name="<?php echo "uploadedimage".''.$k" type="file"></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>

          </tr>
                                </table></td>
                              <td width="150px">
                              </td>
                              <?php
                              $k++;
                              }
                              ?>
                         <br/>
                         <br/>

                              </tr>
                              <tr>
                              <td>&nbsp;

                              </td>
                              </tr>
                              <tr>
                              <td></td>
                              <td></td>
                              <td></td>
                              <td></td>
                          <td><input type="Submit" name="<?php echo "additems".''.$a ?>" value="<?php echo "Additems".''.$a ?>" style="background-color:#ff7143; color:#353534; height:30px;width:100px; border:none; float:right;margin-right:50px; margin-bottom:20px"></td>
                          </tr>

                              <?php
                              //echo $a; 
                             $a++;

                          }
                          ?>

                         </table>


                         <p>&nbsp;</p>
                          <?php 

                          ?>

                        </form>

这里我创建了动态表,其值将存储在数据库中。但是如何上传特定项目的图像很困难。任何人都可以帮忙。 我编写的代码只上传了工作正常的数据,但添加图片上传后,插入数据时出现错误。

    this is the code for items_image.php file when additems is clicked,don't know what the problem is.I first tried to add images as the text itself.but nothing is working.
        items_image.php
        <?php
        session_start();
        if(!(isset($_SESSION['restid']))){
        header("location:Login.php");
        }
        ?> 
        <?php
        require("config.php");
        $rid=$_SESSION['restid']; 

        for($a=1;$a<=5;$a++)
        {
        $ai="additems".''.$a;
        echo $ai;
        if(isset($_POST[$ai]))
        {
            echo $_POST[$ai];
            if($_POST[$ai]=="Additems1")
        $s=1;
        else if($_POST[$ai]=="Additems2")
        $s=3;
        else if($_POST[$ai]=="Additems3")
        $s=5;
        else if($_POST[$ai]=="Additems4")
        $s=7;
        else if($_POST[$ai]=="Additems5")
        $s=9;

            for($i=$s;$i<=($s+1);$i++)
            {
                while($i<=10)
            {
                //echo $i.''."<br>";
                $iname="itemname".''.$i;
            if($_POST[$iname]!=" ")
            {   
            $icat="cat".''.$i;
            $iname="itemname".''.$i;
            $iprice="itemprice".''.$i;
            $idesc="itemdesc".''.$i;
            $idis="itemdis".''.$i;
            $iveg="itemveg".''.$i;
            $img="uploadedimage".''.$i;
            $ict=$_POST[$icat];
            $in=$_POST[$iname];
            $ip=$_POST[$iprice];
            $id=$_POST[$idesc];
            $idi=$_POST[$idis];
            $ivg=$_POST[$iveg];
            //echo $ict.''.$in.''.$img;
            if($ivg!="no")
            $iv=1;
            else
            $iv=0;


             if (!empty($_FILES[$img]["name"])) {
            $file_name=$_FILES[$img]["name"];
            $temp_name=$_FILES[$img]["tmp_name"];
            $imgtype=$_FILES[$img]["type"];

            $imagename=$_FILES[$img]["name"];
            echo $imagename;
            $target_path = "items/".$imagename;
            echo $target_path;
        if(move_uploaded_file($temp_name, $target_path)) {
        $sql="insert into item_list(cat_id,item_name,item_desc,item_price,item_discount,item_veg,item_image)values($ict,'".$in."','".$id."','".$ip."','".$idi."','".$iv."','".$target_path."')";
        echo $sql;}
        $result=mysqli_query($con,$sql) or die(mysql_error()."no data");
        }}}}

        if($result)
        {
            echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
            //header("location:Editmenu.php");

            //echo $sql;
        }
        else
        echo "<script type='text/javascript'>alert('Failed!')</script>";
        }
        }
        ?> 

0 个答案:

没有答案