0

I have a code below where it looks for an ImageId by seeing what is entered in 'ImageFile[]' and then I use INSERT VALUES to insert the values (at the moment I am using echo $questionsql). The problem though is that the value is left blank when I echo it. Also I am getting undefined index notice for imageFile[]. $insertquestion = array();

    $imagequery = "SELECT ImageId FROM Image WHERE (ImageFile = '". mysql_real_escape_string($_POST['imageFile'])."')";
    $imagers = mysql_query($imagequery);
    $imagerecord = mysql_fetch_array($imagers);
    $imageid = $imagerecord['ImageId']; 

        $insertquestion[] = "'".
                    mysql_real_escape_string( $imageid ) ."'";

 $questionsql = "INSERT INTO Question (ImageId) 
    VALUES (" . implode('), (', $insertquestion) . ")";

    echo($questionsql);

The reason I believe it echos a blank value for 'imageFile[]' and why it is stating 'imageFile[]' is undefined is because in the <form> below, there is no imageFile[]. The imageFile[] is in the javascript code and it is appended into the form table.

So what I want to know is how can I get it so I can get a hidden input which matches the appended ImageFile'[]' into the form code so that when I post imageFile[] in the next page where the php code is, I don't get an undefined index and I won't get a blank value for 'imageFile[]?

Below is the javascript and form code:

<script>

    function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $image = $("<td class='image'></td>");

    var $imagefile = $('<input />')
        .attr({
            type: 'file',
            name: 'imageFile[]',
            class: 'imageFile'
    });

    $tr.append($qid); 
    $tr.append($image);   
    $tbody.append($tr); 

    }

</script>


<form id="QandA" action="insertQuestion.php" method="post" >

<table id="qandatbl" align="center">
<thead>
<tr>
    <th class="image">Image</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

</form>

1 Answers1

0

When you use the PHP array syntax for post fields, the [] do NOT show up in the $_POST array. The syntax would be $_POST['imageFile'][0], $_POST['imageFile'][1], etc... for every field you've named imageFile[] in your form.

As well, I suspect your implode() later on will produce invalid SQL. It's going to make the query:

INSERT INTO Question (ImageId)
VALUES ('value1''),(''value2''),etc...

You've already surrounded the values with quotes in the previous line, then add more quotes as part of the implode, so you'll end up with some unbalanced quotes in the query string.

brettkelly
  • 27,655
  • 8
  • 56
  • 72
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • so I am guessing the implode should be like this: `(" . implode('), ( $insertquestion) . ")";` – user1309180 Apr 05 '12 at 04:01
  • I have removed [] from the php code and the undefined index is gone, but I am still getting blanks values while trying to insert the VALUES – user1309180 Apr 05 '12 at 04:28
  • echo out the generated sql statement and try executing it yourself. look for syntax errors, and add some error handling to your query calls, e.g. `$result = mysql_query(...) or die(mysql_error());`. – Marc B Apr 05 '12 at 04:29