Log in

View Full Version : Multiple Row Insert



jgifford
01-12-2009, 08:18 PM
I've created a Database schema that will allow the admin to easily update thier form. To do this I am using the data from the DB to dynamically create the form. For example, if the admin wants to add a new input to the form, they can do that by giving it a name and a type, such as text or radio.

I've written code that displays the form based on the data. I'm stuck on getting the results from the form inserted back to the DB. Hopefully I can explain this well enough without having to post all the details.

I have one table that is the responses from the form. Each row contains 5 columns that represent the request, the question, the option chosen, any details from the option chosen (text from a text field) and a customer id.

I need to do an insert that will create a new row for the user's response to each question. I'm having some difficulty creating this mutiple row insert. Here is what I have so far. I'm hoping it's just a syntax issue...but it could be more. Thanks in advance.


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
{
foreach ($_POST as $var => $value)
{
if ($value != NULL)
{
$insertSQL = sprintf("INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['rid'], "int"),
GetSQLValueString($var, "int"),
GetSQLValueString($value, "int"),
GetSQLValueString($value, "text"),
GetSQLValueString($_POST['cid'], "int"));

mysql_select_db($database_connDBName, $connDB);
$Result1 = mysql_query($insertSQL, $connDB) or die(mysql_error());
}
}
}

jgifford
01-12-2009, 09:06 PM
I didn't really explain the result. Nothing is inserted into the DB and everything that appeared on the page before adding the insert now no longer appears.

gpigate
01-13-2009, 02:10 AM
nothing is showing up period, white screen? ie php syntax error? or you just are not getting the results you are looking for

shouldnt this
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))

be this

if ((isset($_POST["MM_insert"]) && ($_POST["MM_insert"] == "form1"))

jgifford
01-15-2009, 06:07 PM
Correct. Nothing is showing up, blank screen.

I'm pretty sure the original is correct:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))

Still haven't found the error.

gpigate
01-15-2009, 06:17 PM
you are correct on the if, the structure is right... I may have been looking out of only 1 eye before ;)

I placed the code you have above in a small test.php file and I am able to echo out test statements so I think your code is syntactically ok.

any chance you can put the whole script up? url to the page?

gpigate
01-15-2009, 06:19 PM
also is that your entire post or submit section once the form is submitted? no header redirect or output of anything?

jgifford
01-15-2009, 08:40 PM
you are correct on the if, the structure is right... I may have been looking out of only 1 eye before ;)

I placed the code you have above in a small test.php file and I am able to echo out test statements so I think your code is syntactically ok.

any chance you can put the whole script up? url to the page?

Thanks for the help.

The form is very dynamic and so I think the whole script wouldn't be very helpful. I found an error I had and have made some changes to the original code. If a solution isn't apparent after this, I will do my best to add some details to the code and post the rest of the script.


also is that your entire post or submit section once the form is submitted? no header redirect or output of anything?

After insert it should redirect. Here is the new code along with the redirect.



if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
{
foreach ($_POST as $var => $value)
{
if ($value != NULL)
{

$combinedID = explode("-",$var));
// $var is the input name, for text inputs the name is questions_ID followed by a dash followed by options_ID so we must split them apart to get the values. Example $var is 10-101 for text inputs
if ($combinedID[1] != "" && $combinedID[1] != NULL)
{
$insertSQL = "INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES ('".mysql_real_escape_string($_POST['rid'])."', '".(int)$combinedID[0]."', '".(int)$combinedID[1]."','".$value."','".mysql_real_escape_string($_POST['cid'])."')";
}
else
// $var is the input name, $value is the input value, for radio inputs the name is the questions_ID and the value is the options_ID, radio inputs have no details. Example $var is 10, $value is 101 for radio inputs
{
$insertSQL = "INSERT INTO response (request_ID, questions_ID, options_ID, details, customer_ID) VALUES ('".mysql_real_escape_string($_POST['rid'])."', '".(int)$var."', '".(int)$value."','".NULL."','".mysql_real_escape_string($_POST['cid'])."')";
}

mysql_select_db($database_connDBName, $connDB);
$Result1 = mysql_query($insertSQL, $connDB) or die(mysql_error());
}
}
}

$insertGoTo = "thanks.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
header(sprintf("Location: %s", $insertGoTo));