Newb having trouble with Nested If Statements and Variables
Posted: Tue Jun 23, 2009 9:31 am
Hey. I just started learning PHP around two weeks ago, and I'm having some trouble that I'm not sure how to fix. I may just be trying to do something that isn't possible and don't know.
Basically I'm trying to create a page that will allow a user to add information to three different databases in succession (I'm picking up the project from someone else, and the database was designed terribly). A user will fill in two fields, hit submit, and be taken to the next area, where they have to fill in like 12 fields, and finally to a third area where they fill out another 10 or so fields. I know how I'm going to approach most of this, but I'm having problems with one portion
The first thing the user does is pick if a certain field exists in the database or not. If it does, they select from a list of values already in the DB, if not, they have a form to put in a new one. This is where I'm going screwed up:
The following code is for a page that is meant supposed to run through without the user seeing anything. The body tag looks like (<body onload="document.hiddenForm.submit()">) so that the form at the end of this elseif() statement should submit automatically. The problem is, I can't get the variables inside the nested statements ($mainID) to pass. The code is just so that I can grab this variable and use it as a hidden input in the next form.
I know I'm doing something really wrong or really stupid, but I would love some help.
Thanks, Matt.
Basically I'm trying to create a page that will allow a user to add information to three different databases in succession (I'm picking up the project from someone else, and the database was designed terribly). A user will fill in two fields, hit submit, and be taken to the next area, where they have to fill in like 12 fields, and finally to a third area where they fill out another 10 or so fields. I know how I'm going to approach most of this, but I'm having problems with one portion
The first thing the user does is pick if a certain field exists in the database or not. If it does, they select from a list of values already in the DB, if not, they have a form to put in a new one. This is where I'm going screwed up:
The following code is for a page that is meant supposed to run through without the user seeing anything. The body tag looks like (<body onload="document.hiddenForm.submit()">) so that the form at the end of this elseif() statement should submit automatically. The problem is, I can't get the variables inside the nested statements ($mainID) to pass. The code is just so that I can grab this variable and use it as a hidden input in the next form.
Code: Select all
//////////////////////////////////////////////////////////////////////////////////////////////
//If they get through the first page and have either entered a new one, or picked an old one//
//////////////////////////////////////////////////////////////////////////////////////////////
elseif((isset($_POST['softwareName']) && isset($_POST['manufacturer'])) || (isset($_POST['nameManu'])))
{
////////////////////////////////////////////////////////////
//if they had to enter a new softwareName and manufacturer//
////////////////////////////////////////////////////////////
if(isset($_POST['softwareName']) && isset($_POST['manufacturer']))
{
$mainID=0;
$softwareName = $_POST['softwareName'];
$manufacturer = $_POST['manufacturer'];
echo "<br/>$softwareName $manufacturer<br/>";
@ $db = new mysqli($host, $user, $password, $schema);
$query = "insert into sw_maintest (Name, Manufacturer) values (\"$softwareName\", \"$manufacturer\")";
$result = $db->query($query);
echo $query;
$query2 = "select ID from sw_maintest where Name=\"$softwareName\" and Manufacturer=\"$manufacturer\"";
$result2 = $db->query($query2);
echo "<br/>";
echo $query2."<br/>";
$result2 = mysqli_fetch_assoc($result2);
$mainID = $result2["ID"];
echo $mainID."<br/>";
}
///////////////////////////////////////////////////////////////////////////////////
//if the name and manu already existed and they picked one from the dropdown menu//
///////////////////////////////////////////////////////////////////////////////////
elseif(isset($_POST['nameManu']))
{
$mainID=0;
$mainID = $_POST['nameManu'];
echo $mainID;
}
echo <<< HIDDENFORM
<form action="{$_SERVER['PHP_SELF']}" name="hiddenForm" method="post">
<input type="hidden" value="$mainID" name="mainID" />
</form>
HIDDENFORM;
}Thanks, Matt.