Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am posting an array which will look something like this 4, 6, 7

I am having trouble inserting each of these values into an sql table though.

Here's what I have so far...

$a = array($_POST['newsletterArray']);
$mything = 32;

$values = array();
foreach($a as $key => $value) {
    $values[] = "('{$mything}', '{$value}')";
}
if(sizeof($values)) {
    $query = "INSERT INTO images (news_event_id, newsletter_id) VALUES ".implode(',', $values);
    $result = $mysqli->query($query);
}

This code inserts the first record from the array but none of the subsequent records.

If I substitute $_POST['newsletterArray'] with 4, 6, 7 it works fine so it seems this is where the problem is.

newsletterArray is a text input if that helps at all.

share|improve this question
 
If I am not totally mistaken that first line will not produce an array the way you mention. –  arkascha Nov 19 '12 at 0:43
 
maybe not... that's why I'm asking where i'm going wrong. –  Tom Nov 19 '12 at 0:47
 
Well, Why maybe? Dump the value in $a and have a look! I could imagine $_POST['newsletterArray'] being an array itself, as it might be the result of checkboxes being posted as part of a form submission. In that case use $a=$_POST['newsletterArray']. Otherwise you get an array holding an array inside $a which does not make sense for your foreach loop. So, clearly the fist step: what is the value of $_POST['newsletterArray']? –  arkascha Nov 19 '12 at 0:51
 
The value of post array is exactly what i said it was... from a text input - not the best approach I'll be the first to admit, but it's the one I'm having to work with. –  Tom Nov 19 '12 at 0:58
1  
That means you have a text input field where you type in 4, 6, 7 and want to use that string as an array? That is something different. Your code gives you an array holding a single element of type string, so array("4, 6, 7"), that is completely different from array(4,6,7). Instead you have to 'explode' the string into separate elements and clear those afterwards: $a=explode(',',$_POST['newsletterArray']);for($i=0; $i<count($a);$i++)$a[$i]=(integer)trim($a);. –  arkascha Nov 19 '12 at 9:04
show 1 more comment

1 Answer

You do not have associative array here. Judged by what you have written, your $a is just a list like this

a = array(4,6,7);

So you need to treat it this way in the foreah

foreach($a as $value) {
    $values[] = "('{$mything}', '{$value}')";
}

This will do the work

share|improve this answer
 
Failing to escape when using mysqli is inexcusable. –  tadman Nov 19 '12 at 1:31
 
did not get you –  Salvador Dali Nov 19 '12 at 1:47
 
Escape your SQL values. –  tadman Nov 19 '12 at 16:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.