...

View Full Version : saving fields in database using php



Jodarecode
03-12-2009, 07:39 PM
The code below saves top portion of the order form. What I need is to save 240 other fields, they are named and shown as:

each row is textbox_r1_c1, textbox_r1_c2, textbox_r1_c3, textbox_r1_c4, textbox_r1_c5, textbox_r1_c6, textbox_r1_c7, textbox_r1_c8, textbox_r1_c9
next row is textbox_r2_c1 thru textbox_r2_c9
3rd row is textbox_r3_c1 thru textbox_r3_c9
4th row is textbox_r4_c1 thru textbox_r4_c9
5th...
6th...
and so on to a total of 240 fields....

How would I go about saving those specific fields into the database as well as the fields noted below? Or maybe I can save all fields and not the hidden ones or the ones I dont want saved?


$fieldstoinsert=array('purchaser', 'orderno', 'contact', 'shipto', 'street', 'city', 'state', 'zip', 'orderdate', 'reqshipdate', 'shipviaUPS', 'shipviaTC', 'shiptotel', 'dealertel', 'mobiletel', 'specinstr', 'blumotion', 'casetape', 'caseconst', 'drwbox', 'state', 'RosBox', 't2', 't3', 't4', 't5', 't6', 't7', 't8');

$query = "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES";

foreach($_POST as $fieldid => $value){
if(in_array($fieldid,$fieldstoinsert)){
if(trim($value)!=""){
$query.= "('$userid', '$fieldid', '$value', '$saveas'),";

}
}
}

$query=substr($query,0,-1);

Jodarecode
03-12-2009, 08:58 PM
ok I figured it would be a nested loop.
now, I just need to figure out how to implement it to save

1st concern) Currently I have and is still not saving the 1 thru 240 fields, what am I doing wrong?

2nd concern) Is there a way to check to see if value is already there for a particular user and save over it instead of adding to the database?


for ($i=0; $i<=240; $i++) {
for ($j=0; $j<=9; $j++) {

$fieldstoinsert=array('purchaser', 'orderno', 'contact', 'shipto', 'street', 'city', 'state', 'zip', 'orderdate', 'reqshipdate', 'shipviaUPS', 'shipviaTC', 'shiptotel', 'dealertel', 'mobiletel', 'specinstr', 'blumotion', 'casetape', 'caseconst', 'drwbox', 'state', 'RosBox', 't2', 't3', 't4', 't5', 't6', 't7', 't8', '"textbox_r".$i."_c".$j');


$query = "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES";

foreach($_POST as $fieldid => $value){
if(in_array($fieldid,$fieldstoinsert)){
if(trim($value)!=""){
$query.= "('$userid', '$fieldid', '$value', '$saveas'),";

}
}
}

}
}

abduraooft
03-12-2009, 09:07 PM
1st concern) Currently I have and is still not saving the 1 thru 240 fields, what am I doing wrong? Have you added proper error checks to your query statement?

2nd concern) Is there a way to check to see if value is already there for a particular user and save over it instead of adding to the database?
You might need to look into "INSERT IGNORE (http://dev.mysql.com/doc/mysql/en/insert.html)" or "ON DUPLICATE KEY UPDATE (http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html)"

Jodarecode
03-13-2009, 12:35 AM
1st concern is solved!

2nd concern: Its still not updating, basically I want it to save if there is a new saveas and update if there is already an existing saveas


example

$fieldstoinsert=array('purchaser', 'orderno', 'contact', 'shipto', 'street', 'city', 'state', 'zip', 'orderdate', 'reqshipdate', 'shipviaUPS', 'shipviaTC', 'shiptotel', 'dealertel', 'mobiletel', 'specinstr', 'blumotion', 'casetape', 'caseconst', 'drwbox', 'state', 'RosBox', 't2', 't3', 't4', 't5', 't6', 't7', 't8');


$query = "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES ON DUPLICATE KEY UPDATE WHERE saveas=saveas";

foreach($_POST as $fieldid => $value){
if(in_array($fieldid,$fieldstoinsert)){
if(trim($value)!=""){
$query.= "('$userid', '$fieldid', '$value', '$saveas'),";

}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum