SicKn3sS Posted September 21, 2007 Share Posted September 21, 2007 Hey, how can I make a loop to input all this POST data into my database? Content-Length: 677 ID=2&Units+in+Canada=10&Instrument=H-7000&Category=TEM&Filament=777-0179&Filament+Cartridge=&LAB-6+or+Consumable+Emitter=747-0466%2F747-0299&Fixed+Condenser+Aperture=533-1101+%281000u%29&EDX+Objective+Movable+Aperture=&Condenser+Movable+Aperture=747-1565+%28300-200-50-20u%29&Objective+Movable+Aperture=747-1732+%28160-80-40-20u%29&Beam+Monitor+Aperture=&DPA+or+OBJ+Fixed+Aperture=&Selected+Area+Movable+Aperture=747-1567+%28400-200-100-50u%29&Viewing+Screen=747-0326&Spot+Viewing+Screen=&PMT=J386012+%28R268%29&STEM+Scintillator=533-0286&BSE+Detector=&Track+Ball=&Outer+Viewing+Screen=747-0246&Wehnelt+Assembly=&Whenelt+Contact+Spring=542-1106&Wehnelt+for+Cartridge= Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/ Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 You don't need to use a loop to insert data into a database, in fact, you sould do it in one SQL statement if you can. INSERT INTO table_name (column1, column2, column3) VALUES ('$var1', '$var2', '$var3') etc... Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352321 Share on other sites More sharing options...
SicKn3sS Posted September 21, 2007 Author Share Posted September 21, 2007 That's right but I have about 30 fields and they are subject to change any time so I need to loop to find each field. Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352334 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 Are you saying that all your fields in the database are allowed to be null, and only certain fields will be inserted each time? You're also relying on the field names in the db matching the field names in the form? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352337 Share on other sites More sharing options...
MmmVomit Posted September 21, 2007 Share Posted September 21, 2007 I suppose you could use a foreach loop on the $_POST array to build the query string. Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352373 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 You could, hence the above post. If you're only submitting certain fields each time then there needs to be an allowance for this in the database, and you'll also either need to make sure the columns in the database are the same as the fields in the form, or alternatively (and more secure) do some mapping between fields. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352387 Share on other sites More sharing options...
hvle Posted September 21, 2007 Share Posted September 21, 2007 You just have NO clue on what to do with the data whatsoever, don't you? Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352397 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 You just have NO clue on what to do with the data whatsoever, don't you? Very helpful! firstly, who are you addressing, secondly, how about offering something constructive? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352402 Share on other sites More sharing options...
hvle Posted September 21, 2007 Share Posted September 21, 2007 Very helpful! firstly, who are you addressing, secondly, how about offering something constructive? Regards Huggie Since I didn't quote you, you know I meant to address topic creator. I would suggest something constructive if the man had a constructive purpose. Why would you want to dump a bunch of un-validate, un-checked, un-used, un-processed, and probably un-wanted data into database? Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352407 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 Why would you want to dump a bunch of un-validate, un-checked, un-used, un-processed, and probably un-wanted data into database? You probably wouldn't but as the OP has only made 6 posts in total, they're probably new to PHP hence the hand holding initially. It's a gradual process. Write the code to put the data into the database. Realise that you get errors and someone's submitted a string in a number field. Write the code to correct the above (validate). Realise the code inserts but that someone's spamming you. Write the code to prevent automated submission. When you're new to PHP you don't automatically think about these things. All I'm saying is that even if you're not going to offer any help yourself, a link to a post that might help would certianly be more appreciated. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352412 Share on other sites More sharing options...
hvle Posted September 21, 2007 Share Posted September 21, 2007 You probably wouldn't but as the OP has only made 6 posts in total, they're probably new to PHP hence the hand holding initially. It's a gradual process. Write the code to put the data into the database. Realise that you get errors and someone's submitted a string in a number field. Write the code to correct the above (validate). Realise the code inserts but that someone's spamming you. Write the code to prevent automated submission. When you're new to PHP you don't automatically think about these things. All I'm saying is that even if you're not going to offer any help yourself, a link to a post that might help would certianly be more appreciated. Regards Huggie word! Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352418 Share on other sites More sharing options...
scarhand Posted September 21, 2007 Share Posted September 21, 2007 You'll notice that people who obviously put no effort into solving their problems themselves will not get much help around here. Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352474 Share on other sites More sharing options...
SicKn3sS Posted September 21, 2007 Author Share Posted September 21, 2007 Very helpful! firstly, who are you addressing, secondly, how about offering something constructive? Regards Huggie Since I didn't quote you, you know I meant to address topic creator. I would suggest something constructive if the man had a constructive purpose. Why would you want to dump a bunch of un-validate, un-checked, un-used, un-processed, and probably un-wanted data into database? Only the admin account is able to access the page and he knows what he is doing. I suppose you could use a foreach loop on the $_POST array to build the query string. That's what I want to do but I can't figure out how. Can someone tell me? Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352563 Share on other sites More sharing options...
SicKn3sS Posted September 22, 2007 Author Share Posted September 22, 2007 Okay I figured it out myself: while($row = mysql_fetch_assoc($result)) { foreach($row as $field => $value) { $updateValue=$_POST[$field]; // replace value in db with update value } } Not exactly how I wanted it, but it works. I'm just posting the solution encase people find this post on google or search Quote Link to comment https://forums.phpfreaks.com/topic/70155-solved-update-database-with-all-post-data-from-previous-page/#findComment-352984 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.