Hey All,
I am stuck with a project I am working on. Can I get it to work sure but is it the easiest/right way, well that’s why I am here.
To start off I am working on a dynamic form. The form is a parameter entry form where the list of parameters can be different for each user. There are 50+ parameters a user can select to display on their form. All Parameters are stored in a ParameterTypes table. When a user selects the paramaters they would like the ParamTypeID is stored in a comma delimited list in the UserPrefs table so that each time the form is launched they see only the ParamTypeIDs in their preferences. This is easy and the form display is working perfectly.
The challenge I am having is is when the form is submitted and how to process each parameter individually but with 1 insert statement. This is going to be a very read heavy table so the less locking the better. The form consists of ParamID (which is dynamic based off the users pref), ParamValue (manually entered), ParamImage (manually entered) all inserted with the same Date, UserID, and 1 other input field.
To start off with I have just been using the POST array and looping through this to create an Insert variable which gets appended to for each Key in the Array that is not Submit, Date, Time, Img, or blank. This creates the insert and works fine.
Now I am getting into the meat and potatoes a bit more as I think about the Image processing and Edit functionality. Not sure if this method is will work for Editing. Either way I am starting tho think I way over thought this and taking the wrong approach. I was just hoping to get some of your thoughts on how you would handle this situation.
[php]
<?php // SET VARIABLES ------------------------------------------ if (isset($_GET['Form'])) {$Form = $_GET['Form'];} else {$Form = "Display";} if (isset($_GET['Action'])) {$Action = $_GET['Action'];} else {$Action = "";} $TankID = $_GET['TankID']; // FORM PROCESSING -------------------------------------------------------------------------------------------------------------------- // if ($Form == "Add" && !isset($_POST['Date'])) { $Date = date_format(date_create(),"Y-m-d"); $Time = date_format(date_create(),"H:i"); } else if (isset($_POST['Date'])) { $Date = $_POST['Date']; $Time = $_POST['Time']; } if (isset($_POST['Submit'])) { $Insert = ""; foreach ($_POST as $Key => $Value) { if ($Key <> "Submit" && $Key <> "Date" && $Key <> "Time" && substr($Key, 0, 3) <> "img" && $Value <> "") { if (isset($_POST['pic_' . $Key])) { $Img = $_POST['pic_' . $Key]; } else { $Img = NULL; } $Insert .= "('". $TankID."', '". $Key ."', '". $Value ."','". $Img ."'),"; } } $Insert = substr_replace($Insert ,"",-1); } // ADD ------------------------------------------ if ($Action == "Add") { // VALIDATE FORM ------------------------------------------ if ($Insert == "") { $Alert = 'Fail'; $Msg = 'Please fill in at least 1 paramater before saving.'; AddAlerts($Alert, $Msg, $LogAlerts); } else { $AddSql=' INSERT INTO Paramaters (TankID, ParamTypeID, ParamValue, ImgName) VALUES '.$Insert; $AddQry = mysql_query($AddSql); if (!$AddQry) { $Alert = 'Fail'; $Msg = 'Invalid query: '. mysql_error() .'\n Query: '. $AddQry; AddAlerts($Alert, $Msg, $LogAlerts); } else { $Alert = 'Success'; $Msg = 'Your paramaters have been saved!'; AddAlerts($Alert, $Msg, $LogAlerts); $Form = "Display"; $Action = "Success"; } } } // DISPLAY QUERIES -------------------------------------------------------------------------------------------------------------------- // if ($Form == "Display") { } else if ($Form == "Edit" && $TankID != "") { } else if ($Form == "Add" && $TankID != "") { $ParamQry = mysql_query(" SELECT ParamTypeID, Name, Description FROM ParamTypes WHERE ParamTypeID IN (". $_SESSION['ParamIDs'] .") "); if (!$ParamQry) { $Alert = 'Fail'; $Msg = 'Invalid query: '. mysql_error() .'\n Query: '. $ParamQry; AddAlerts($Alert, $Msg, $LogAlerts); $Action = "TryAgain"; } else { $Params = array(); while($Param = mysql_fetch_assoc($ParamQry)) { $Params[] = $Param; } } } // CHECK ALERTS -------------------- if (count($Alerts) > 0) { echo ''; foreach ($Alerts as $Message) { echo $Message; } echo '
'; } // DISPLAY PAGE -------------------------------------------------------------------------------------------------------------------- // // ADD FORM -------------------- if ($Form == "Add" && $Action != "Success") { $Action = "Add"; echo '
Param | Value | Image | Suggested |
Date | |||
'. $Param['Name'] .' | '. $Param['Description'] .' | ||
[/php]
Here is an example POST array.
[_POST] => Array
(
[Date] => 2014-01-11
[Time] => 13:48
[1] => 1.025
[pic_1] => DSC_0005.jpg
[3] => 8
[pic_3] => DSC_0006.jpg
[4] => 0
[5] => 0
[6] => 8
[pic_6] => DSC_0016.jpg
[7] =>
[8] =>
[9] =>
[10] =>
[Submit] => Submit
)
Looking forward to your opinions.
Thank you,
Jeremy