miligraf Posted August 31, 2007 Share Posted August 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/ Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 You are probably going to want to use an array as the field names, but I'm not sure how you are doing it. It would be easier if you posted some code. Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338720 Share on other sites More sharing options...
miligraf Posted August 31, 2007 Author Share Posted August 31, 2007 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> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338864 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338871 Share on other sites More sharing options...
miligraf Posted August 31, 2007 Author Share Posted August 31, 2007 Bingo! 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. Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338879 Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 For every set that you put on the page, use an array as the name. For example, field1[], field2[], field3[]. Then, when you get it from the POST array, you will have an array for every field. Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338891 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 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()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-338892 Share on other sites More sharing options...
miligraf Posted September 1, 2007 Author Share Posted September 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-339157 Share on other sites More sharing options...
darkfreaks Posted September 1, 2007 Share Posted September 1, 2007 try doing ($title,$j) Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-339158 Share on other sites More sharing options...
Ken2k7 Posted September 1, 2007 Share Posted September 1, 2007 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()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-339166 Share on other sites More sharing options...
jitesh Posted September 1, 2007 Share Posted September 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-339169 Share on other sites More sharing options...
miligraf Posted September 1, 2007 Author Share Posted September 1, 2007 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 <?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()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67468-solved-insert-into-sql-several-items-info/#findComment-339212 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.