1

i have a problem with the selection of the images for insert into the mysql database, when i select the image from upload (max 3), i get into mysql only the last image selected, i would want get all images selected

Code:

<?php include('../../include/conn.php'); ?>
<html>
<head></head>
<body>
<style type="text/css">
.thumbimage {
    float:left;
    width:100px;
    position:relative;
    padding:10px;
}
</style>

<form method="POST" action="imageuploadpro.php" enctype="multipart/form-data">
<tr><input id="imageupload" name="imageupload" type="file" class="imageupload" enctype="multipart/form-data" multiple accept=".gif,.jpg,.jpeg,.png"/>
<div id="wrapper">
    <td width="200" height="100" bgcolor="#DFF0FF">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <br />
   <tr><div id="preview-image"></div></tr></table></td>
</div>
 <input type="submit" name="submit_image" value="Upload">
</form>
<?php
if (!isset($_FILES['imageupload']['name']))
{
 echo "insert image";
}
else
{
$imagename=$_FILES["imageupload"]["name"]; 

//Get the content of the image and then add slashes to it 
$imagetmp=addslashes (file_get_contents($_FILES['imageupload']['tmp_name']));


//Insert the image name and image content in image_table
$insert_image="INSERT INTO `Annunci` (`image`,`name`) VALUES('$imagetmp','$imagename')";

mysql_query($insert_image);
}
?>

<div id="textDiv"></div>

<script type='text/javascript' src='./jquery-1.9.1.js'></script>

<script type="text/javascript">
var countImages = 0;

$("#imageupload").on('change', function () {
    var countFiles = $(this)[0].files.length;

    var imgPath = $(this)[0].value;
    var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
    var image_holder = $("#preview-image");

    //image_holder.empty();

    if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
        if (typeof (FileReader) != "undefined") {
            for (var i = 0; i < countFiles; i++) {
                if(countImages >=3 )
                {
var div = document.getElementById("textDiv");
    div.textContent = "Non puoi inserire più di 3 immagini";
    var text = div.textContent;
                {
                break;
                }
                }
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("<img />", {
                        "src": e.target.result,
              "class": "thumbimage" //questo imposta la dimensione delle immagini dichiarate nello stile(inizio)
                    }).appendTo(image_holder);
                }
                image_holder.show();
                reader.readAsDataURL($(this)[0].files[i]);
                countImages++;
            }
        }
        else {
            alert("It doesn't supports");
        }
    }
    else {
        alert('Formati consentiti gif - png - jpg');
    }
});
</script>
</body>
</html>

Someone can help me?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 31 '17 at 18:14
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jan 31 '17 at 18:14
  • `$_FILES['imageupload']['tmp_name']` is a file and filename created by PHP It does not need fiddling with – RiggsFolly Jan 31 '17 at 18:16
  • How can you expect to process more than one uploaded file if you are not processing the information in any sort of loop – RiggsFolly Jan 31 '17 at 18:16
  • Start by reading the PHP Manual [Uploading multiple files](http://php.net/manual/en/features.file-upload.multiple.php) – RiggsFolly Jan 31 '17 at 18:17
  • @RiggsFolly the kitten seems happy though.... – davejal Jan 31 '17 at 18:25
  • @davejal But inside it is very angry that people are still using the `mysql_` extension and teachers are still teaching it – RiggsFolly Jan 31 '17 at 18:30
  • @RiggsFolly , just pulling your leg – davejal Jan 31 '17 at 18:33
  • @davejal No problem. I know, I could feel you tickleing my toes – RiggsFolly Jan 31 '17 at 18:34
  • You can also read http://stackoverflow.com/q/12859942/3664960 on top of what @RiggsFolly already suggested – davejal Jan 31 '17 at 18:36

0 Answers0