Jump to content

[SOLVED] INSERT using for() - need value to increase


Recommended Posts

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 />";
}

 

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]

// 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 />";
}

// 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()

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.