简体   繁体   中英

Uploading Image with text to database php mysql

I currently have an html form that is supposed to submit text and an image to a database. I am trying to post both text and an image with one submit button. The image is stored as a blob. The code currently contains two mysql_query functions, the first is for the text and the second which does not work is for the image. How can I put the form data, (the image and text) into one mysql_query function were it uploads after I hit the form submit button? I have tried many ways to put into one mysql_query function, but I can't get it to work right. or if anyone knows a tutorial that shows how to submit text with an image that would be great.

Code:

    <?php //edit home.php slideshow headlines


    if(isset($_POST['submita'])){
    $linka = $_POST['linka'];
    $titlea = $_POST['titlea'];
    $contenta = $_POST['contenta'];
    $thumba = $_POST['thumba'];

    //first mysql_query function for text
    mysql_query("INSERT INTO headlines2 (linka, titlea, contenta, thumba)VALUE('$linka','$titlea','$contenta','$thumba')");

    //image blob content here

    if(!isset($_FILES['imagea']))
    {
    echo 'Please select an image.';
    }
    else {
    $imagea = addslashes(file_get_contents($_FILES['imagea']['tmp_name']));
    $image_name = addslashes($_FILES['imagea']['name']);
    $image_size = getimagesize($_FILES['imagea']['tmp_name']);

    if($image_size==FALSE){
    echo "That's not an image.";
    } else {

//second mysql_query function imagea
    if(!$insert = mysql_query("INSERT INTO headlines2 VALUES ('','$image_name','$imagea')"))
         {
     echo "Problem uploading image.";
     }else{
     /*$lastid = mysql_insert_id();
     $lastid = $lastid - 10;
     Your image:<br /><img src=get.php?id=$lastid />
     */
     echo "Image uploaded. <br /> ";



     }
     } 
    }

    //end image blob


    echo "Posted";
    header('location: admin.php?edit1=edit_home');

    }else{
    ?>
    <?php

    ?> 
    <form action="?edit1=edit_home" method="post"enctype="multipart/form-data">

    <legend><h3>Update Headlines</h3></legend>
    <p class="label"><label class="field">Headline Link (.php * File Ending Required)</label></p><input type="text" name="linka" />

    <p class="label"><label class="field">Headline Title</label></p><input name="titlea"/>

    <p class="label"><label class="field">Content</label></p><textarea name="contenta"></textarea>
    <p class="label"><label class="field">Left Thumbnail on Slider</label></p><input name="thumba"/>

    Image (Must be a jpeg*): <input type="file" name="imagea" />

    <input type="submit" name="submita" value="Post!" />

    </form>
    <?php
    }
    ?>

使用MySQL的LOAD_FILE()

mysql_query("INSERT INTO headlines2 VALUES ('','$image_name', LOAD_FILE('$imagea')"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM