By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

foreach loop not inserting in MySQL

P: 2
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky.

I've tried the mysql_query statement without a preceeding variable as well.

System information
Server
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.3 Time: 12/07/2007 01:20:06 PM
PHP
PHP Version: 5.2.3 Safe Mode activated: no PHP Memory Limit: 256M
gzip compression possible: yes Emails sendable: yes FTP transfer possible: yes
MySQL
MySQL Server: 5.0.45-community-nt MySQL Client: 5.0.45

[PHP]

$getshipgm = mysql_query ("SELECT * FROM shipworks_gm WHERE date BETWEEN '$startdate' AND '$enddate' ");

while ($row = mysql_fetch_array($getshipgm)){

$skuid = $row['skuid'];
$tally = $row['tally'];

$shipgm[$skuid]['skuid'] = $skuid;
$shipgm[$skuid]['tally'] = $shipgm[$skuid]['tally'] + $tally;



}




foreach($shipgm as $ship){

$skuid = $ship['skuid'];
$tally = $ship['tally'];

$insertquery = mysql_query(" INSERT INTO gm_storage (skuid) VALUES ('$skuid') ");

echo '<br>';
echo 'Inserting : ' . $skuid . ' with tally : ' . $tally;
echo '<br>';


}


echo '<br>';
echo '<pre>';
print_r($shipgm);
echo '</pre>';
echo '<br>';

[/PHP]

The bolded foreach loop is where i'm having the issue, i inserted the echo statements to ensure that the loop was generating a record for each child in the array, and my echo output to the screen is fine (about 100 or so items), however when i browse the table in MySQL, only the first record in the array is inserted. The others dont make it and I cant figure out why.
Dec 7 '07 #1
Share this Question
Share on Google+
1 Reply


P: 2
sorry, i fixed it.....

When creating the MySQL database, i forgot to auto_increment the key field. Oops!
Dec 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.