tgavin Posted December 31, 2007 Share Posted December 31, 2007 I'm INSERTing using a for() loop starting at $i=0. However, I need each INSERT to have an id number starting at 1. If I change $i to 1 the loop stops one short. How can I get the INSERT id to start at 1? Note: this is NOT auto incrementing. for($i=0; $i<count($_POST['art_headline']); $i++){ // create vars for newsletter articles $art_headline = $_POST['art_headline']; $art_txt = $_POST['art_txt']; $art_trunc = $_POST['art_trunc']; $art_rm = $_POST['art_rm']; $art_img = $_POST['art_img']; $query = " INSERT INTO {$database}.newsletter_articles ( art_nl_id, art_article_num, art_headline, art_txt, art_trunc, art_rm,art_img ) VALUES ( '{$newid}', '{$i}', // need to put id here (NOT auto incrementing) '{$art_headline[$i]}', '{$art_txt[$i]}', '{$art_trunc[$i]}', '{$art_rm[$i]}', '{$art_img[$i]}' )"; $sql = mysql_query($query) or die(sql_error('could not INSERT INTO {$database}.newsletter_articles table')); echo $query . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/ Share on other sites More sharing options...
kenrbnsn Posted December 31, 2007 Share Posted December 31, 2007 Change the for loop to: <?php for($i=1; $i<=count($_POST['art_headline']); $i++){ ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426875 Share on other sites More sharing options...
tgavin Posted December 31, 2007 Author Share Posted December 31, 2007 Change the for loop to: <?php for($i=1; $i<=count($_POST['art_headline']); $i++){ ?> Ken Thanks Ken. missed the <= part! Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426881 Share on other sites More sharing options...
fataqui Posted December 31, 2007 Share Posted December 31, 2007 instead of... '{$i}', // need to put id here (NOT auto incrementing) do something like... '" . ( $i + 1 ) . "', // need to put id here (NOT auto incrementing) Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426891 Share on other sites More sharing options...
tgavin Posted December 31, 2007 Author Share Posted December 31, 2007 I actually jumped the gun on the topic solved button. Now I have a new problem. The art_article_num field is being populated with the correct numbers (1,2,3,etc,), however, the fields are not in the correct order now. meaning, that all of the POSTed values for record 2 are being inserted into record 1. and all values for record 3 are being inserted into record 2. and all of 3 is empty. When i filled out the form, i entered nothing but 1s in the 1 form, 2s in the 2 form and 3s in the 3 form so that it would be easy to see what was going on. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426915 Share on other sites More sharing options...
hitman6003 Posted December 31, 2007 Share Posted December 31, 2007 // these don't need to be reassigned each time since they are arrays. $art_headline = $_POST['art_headline']; $art_txt = $_POST['art_txt']; $art_trunc = $_POST['art_trunc']; $art_rm = $_POST['art_rm']; $art_img = $_POST['art_img']; for($i = 1; $i <= count($_POST['art_headline']); $i++) { $query = " INSERT INTO {$database}.newsletter_articles ( art_nl_id, art_article_num, art_headline, art_txt, art_trunc, art_rm,art_img ) VALUES ( '{$newid}', // Where is this coming from? '{$i}', '{$art_headline[$i - 1]}', '{$art_txt[$i - 1]}', '{$art_trunc[$i - 1]}', '{$art_rm[$i - 1]}', '{$art_img[$i - 1]}' )"; $sql = mysql_query($query) or die(sql_error('could not INSERT INTO {$database}.newsletter_articles table')); echo $query . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426928 Share on other sites More sharing options...
tgavin Posted December 31, 2007 Author Share Posted December 31, 2007 // these don't need to be reassigned each time since they are arrays. $art_headline = $_POST['art_headline']; $art_txt = $_POST['art_txt']; $art_trunc = $_POST['art_trunc']; $art_rm = $_POST['art_rm']; $art_img = $_POST['art_img']; for($i = 1; $i <= count($_POST['art_headline']); $i++) { $query = " INSERT INTO {$database}.newsletter_articles ( art_nl_id, art_article_num, art_headline, art_txt, art_trunc, art_rm,art_img ) VALUES ( '{$newid}', // Where is this coming from? '{$i}', '{$art_headline[$i - 1]}', '{$art_txt[$i - 1]}', '{$art_trunc[$i - 1]}', '{$art_rm[$i - 1]}', '{$art_img[$i - 1]}' )"; $sql = mysql_query($query) or die(sql_error('could not INSERT INTO {$database}.newsletter_articles table')); echo $query . "<br />"; } Thank you! // Where is this coming from SELECT LAST_INSERT_ID() Quote Link to comment https://forums.phpfreaks.com/topic/83882-solved-insert-using-for-need-value-to-increase/#findComment-426948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.