Jump to content

[SOLVED] Insert into SQL several items info


miligraf

Recommended Posts

Hello, i am almost done with a code that does this:

 

User selects how many items they want to insert into the database, they fill out every field for every item but when they insert all the information it does inserts as many items as they selected but every item has the last item information...

 

How can i fix this? I guess i need to assing a number at each item field but how can i get this unique number to keep changing everytime the insert query is called with a for?

 

Thanks!

Here it is:

 

<? if($_POST['send']){
$query = "INSERT INTO table (title, description, code) VALUES ( '".mysql_real_escape_string($title)."', '".mysql_real_escape_string($description)."', '".mysql_real_escape_string($code)."')";
mysql_query($query)or die ("Error: $query. " . mysql_error());
}else{
for ($i=1; $i<=$num; $i++){ ?>
<input name="title" type="text" size="100%">
<textarea name="description" cols="100%" rows="5"></textarea>
<input name="code" type="text" size="100%">
<? } ?>
<input name="send" type="Submit" value="Submit">
<input type="reset" value="Clear"></form>
<? } ?>

Wait what!?

 

So let me get this straight, you want to let the user choose how many entries they want to submit into the database, then display that exact number of fields. After they press submit, it inserts all the data into the database. Is that what you mean?

Bingo!  :D

 

The thing is that the user chooses a number of items to insert (3 fields per item) but how do i difference the same field but from different item to be inserted so it doesnt overwrites itself?

 

Example:

 

Item 1

Code: abc

 

Item 2

Code: def

 

Item 3

Code: ghi

 

When i insert them, Item 1, Item 2 and Item 3 have the same Code ("ghi") that belongs to the last item.

Try this:

 

<?php

$num = 5;

for ($i=1; $i<=$num; $i++){
echo "
<input name='title_$i' type='text' size='100%'>
<textarea name='description_$i' cols='100%' rows='5'></textarea>
<input name='code_$i' type='text' size='100%'>";
}
echo "<input name='send' type='Submit' value='Submit'>
<input type='reset' value='Clear'></form>";

if($_POST['send']){
for ($j=0; $j<$num; $j++){
	$query = "INSERT INTO table (title, description, code) VALUES ( '".mysql_real_escape_string($title_$j)."', '".mysql_real_escape_string($description_$j)."', '".mysql_real_escape_string($code_$j)."')";
	mysql_query($query)or die ("Error: $query. " . mysql_error());
}
}
?>

Try this:

 

<?php

$num = 5;

for ($i=1; $i<=$num; $i++){
echo "
<input name='title_$i' type='text' size='100%'>
<textarea name='description_$i' cols='100%' rows='5'></textarea>
<input name='code_$i' type='text' size='100%'>";
}
echo "<input name='send' type='Submit' value='Submit'>
<input type='reset' value='Clear'></form>";

if($_POST['send']){
for ($j=0; $j<$num; $j++){
	$query = "INSERT INTO table (title, description, code) VALUES ( '".mysql_real_escape_string($title_$j)."', '".mysql_real_escape_string($description_$j)."', '".mysql_real_escape_string($code_$j)."')";
	mysql_query($query)or die ("Error: $query. " . mysql_error());
}
}
?>

 

I get this parse error: unexpected T_VARIABLE because of $title_$j

oops, forgot something there.

 

<?php

$num = 5;

for ($i=1; $i<=$num; $i++){
echo "
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<input name='title_$i' type='text' size='100%'>
<textarea name='description_$i' cols='100%' rows='5'></textarea>
<input name='code_$i' type='text' size='100%'>";
}
echo "<input name='submit' type='Submit' value='Submit'>
<input type='reset' value='Clear'></form>";

if($_POST['submit']){
for ($j=0; $j<$num; $j++){
	$title = "title_" . $j;
	$desc = "description_" . $j;
	$code = "code_" . $j;
	$query = "INSERT INTO table (title, description, code) VALUES ( '".mysql_real_escape_string($_POST[$title])."', '".mysql_real_escape_string($_POST[$desc])."', '".mysql_real_escape_string($_POST[$code])."')";
	mysql_query($query)or die ("Error: $query. " . mysql_error());
}
}
?>

You can use structure like this.

 

table : Item Master table  (List of all available items)

fields : item_id(primaty key),item_name,item_weight

 

table : User Item Mapping table ( Selected items from users with quantity)

fields : u_i_map_id(primary_key),user_id (from user table ) ,item_id ( from Item Master table),quantity.

 

 

Thanks for your help guys! I just had to edit a bit the code provided by Ken2k7, here i post it so you can help other newbies with same problem  :D

 

<?php
for ($i=1; $i<=$num; $i++){
echo "
<input name='title_$i' type='text' size='100%'>
<textarea name='description_$i' cols='100%' rows='5'></textarea>
<input name='code_$i' type='text' size='100%'>";
}
echo "<input name='send' type='Submit' value='Submit'>
<input type='reset' value='Clear'></form>";

if($_POST['send']){
for ($i=1; $i<$num; $i++){
	$title = $_POST['title_'.$i];
	$description = $_POST['description_'.$i];
	$code = $_POST['code_'.$i];
	$query = "INSERT INTO table (title, description, code) VALUES ( '".mysql_real_escape_string($title)."', '".mysql_real_escape_string($description)."', '".mysql_real_escape_string($code)."')";
	mysql_query($query)or die ("Error: $query. " . mysql_error());
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.