Hi Everyone!
I have 4 arrays, created by form submission, ive imported the variables. The form submission part is submitting the array properly, however my function for the query string, does not.
Can someone help me figure out how to get this working properly?
I know allot of people do all this on 1 page, but im actually using several pages, as im an extreme noob just trying to figure this all out!
What i am trying to do, is cycle through each of the 4array’s 1 time gathering the info and storing it in a variable to be added to the query string being built, then once the string has been built submitting it to the DB, then repeating the process for each value listed in the array until the last value.
The function and arrays in question are about halfway down in the coding sections denoted with:
//Problem area starts here
<?php
//import variables
$dishname = $_POST['recipename'];
$ingcount = $_POST['ingcount'];
$dishid = $_POST['dishid'];
$rcat = $_POST['rcat'];
$cenviro = $_POST['cenviro'];
$ctemp = $_POST['ctemp'];
$ptime = $_POST['ptime'];
$ctime = $_POST['ctime'];
$stime = $_POST['stime'];
// connect to DB
$conn1 = @mysql_connect("localhost", "UID", "UPW") or die("cannot connect to the database!" . mysql_error() );
mysql_select_db("recipes", $conn1);
// add info to Dish DB table "rtable"
$insrinfo = "update rtable set cenviro='$cenviro', ctemp='$ctemp', ptime='$ptime', ctime='$ctime', stime='$stime' where recipename='$dishname'";
if (!mysql_query($insrinfo,$conn1))
{
die('Error: Inserting Recipe fields ' . mysql_error());
}
$idata = mysql_real_escape_string($_POST['instruction']);
$idata = htmlspecialchars($idata);
$idata = nl2br($idata);
$isql = "update rtable set instruction = '$idata' where recipename='$dishname'";
if (!mysql_query($isql,$conn1))
{
die('Error: Inserting Recipe Instructions ' . mysql_error());
}
mysql_close($conn1);
?>
<?php
// PROBLEM AREA STARTS HERE
//import variables again - not sure if i really need to do this or not....
$dishname = $_POST['recipename'];
$dishid = $_POST['dishid'];
$inga = $_POST['inga'];
$ingq = $_POST['ingq'];
$ingn = $_POST['ingn'];
$ingc = $_POST['ingc'];
$ingcount = $_POST['ingcount'];
// connect to DB
$conn2 = @mysql_connect("localhost", "UID", "UPW") or die("cannot connect to the database!" . mysql_error() );
mysql_select_db("recipes", $conn2);
// Create DB string Query from arrays and insert Ingredients into table "rlist"
function ext_ing() {
$dishid = $_POST['dishid'];
$inga = $_POST['inga'];
$ingq = $_POST['ingq'];
$ingn = $_POST['ingn'];
$ingc = $_POST['ingc'];
$ingcount = $_POST['ingcount'];
$i=1;
do
{
$inginfo = "insert into rlist (id, dishid, inga, ingq, ingn, ingc) values ('', '$dishid'";
foreach($inga as $v1)
{
foreach($ingq as $v2)
{
foreach($ingn as $v3)
{
foreach($ingc as $v4)
{
$inginfo .= "'$v1', ";
$inginfo .= "'$v2', ";
$inginfo .= "'$v3', ";
$inginfo .= "'$v4');";
}
}
}
}
// $inginfo = "insert into rlist set (dishid = '$dishid', inga = '$v1', ingq = '$v2', ingn = '$v3', ingc = '$v4')";
echo "$inginfo" . "\n\n<br><br>";
$i++;
}
while ($i<=$ingcount);
}
mysql_close($conn2);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<?php
// display recipe
echo "Welcome: Below is what you submited to the DB" . "<br>\n\n";
echo "$dishname" . "\n<br>";
print_r($ingn);
echo "\n\n<br>";
echo "# of ingredients in recipe: $ingcount \n\n<br><br>";
ext_ing();
echo "\n<br>";
echo "Ingredients added" . "<br>\n\n";
?>
</body>
</html>