Page 1 of 1

Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 9:31 am
by mgwisni
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.

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;
    }
I know I'm doing something really wrong or really stupid, but I would love some help.


Thanks, Matt.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 11:08 am
by mgwisni
I don't know exactly how sessions work, but is that what I should be doing?

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 11:44 am
by Eric!
Is Line 38 the problem? You're not receiving the $_POST data? Is your field 'nameManu' correct or should this really be 'nameMenu' with an E? You didn't post your form so I'm guessing.

You don't need to use a session for this.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 1:12 pm
by mgwisni
Hey, thanks for the reply. NameManu is correct (name, manufacturer). The problem is that I need to be able to access the $mainID values outside those if/elseif statements, but don't know how.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 1:13 pm
by mgwisni
Also, I added "$_SESSION['mainID'] = $_POST['nameManu'];" and was then able to access the variable outside, so I think that'll work, but I'm not sure I'm using good procedure.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 1:19 pm
by Eric!
Outside of what? On another page? Is the problem Line 38 is not returning a value to menuID? Or is the problem that you need to store MenuID and use it on another page (one that you didn't post code for here)?

Ok, I see you posted again while I was writing. You shouldn't need to do that. I can guess from your post is that Line 38 is not returning a value. Can you post the code from the previous page that has $_POST['nameManu']=something?

EDIT: is $_POST['nameManu'] set on page1.php, then you go to Page2.php for more data and then page3.php and you try to access $_POST['nameManu']?

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 1:37 pm
by mgwisni
Eric, thanks for your continued support here.

Actually, all of the work is being done on the same document. I would post all of the code, but it's getting oftly long, so I'll post the important parts.

When they first come to the page they are presented with the following form (at the very end of my PHP document.

Code: Select all

 
else
    {
        //Form 1
        echo <<< FORM1
        <p>Do the name and manufacturer exist in the database already?</p>
        <form action="{$_SERVER['PHP_SELF']}" method="post">
        <label for="yes">Yes:</label><input type="radio" name="exist" value="yes" /><br/>
        <label for="no">No:</label><input type="radio" name="exist" value="no" /><br/><br/>
        <input type="submit" name="submit1" value="Submit" >
        </form>
        
FORM1;
    }
 
From there, they either click yes or no and hit submit. The page processes them through the following code (located at the very top of my page of code).

Code: Select all

 
/////////////////////////////////////
    //if they've finished the first page//
    //////////////////////////////////////
    if(isset($_POST['exist']))
    {
        $exist = $_POST['exist'];
        
        ///////////////////////////////////////     
        //If they click "yes" on the first page//
        ///////////////////////////////////////
        if($exist == 'yes')
        {
            //form 2a
            @ $db = new mysqli($host, $user, $password, $schema);
            $query = "select * from sw_maintest";
            $result = $db->query($query);
            
            echo <<< FORM2A
            <form action="{$_SERVER['PHP_SELF']}" method="post">
            <label for="nameManu">Select Name and Manufacturer</label>
            <select name="nameManu" id="nameManu">
FORM2A;
            $a=0;
            while(($row = $result->fetch_assoc()) == true)
            {
                
                $x=0;
                foreach($row as $value)
                {
                    if($x==0)
                    {
                        echo "<option value=\"$value\">";   
                    }
                    if($x==1)
                    {
                        echo "$value ";
                    }
                    if($x==2)
                    {
                        echo "- $value</option>";   
                    }
                    $x++;   
                }
                $a++;
            
            }
            echo <<< FORM2A
            </select>
            <input type="hidden" value="$mainID" name="mainID" />
            <input type="submit" name="submit1" value="Submit" >
            </form>
FORM2A;
        }
        
        //////////////////////////////////////
        //if they click "no" on the first page//
        //////////////////////////////////////
        elseif($exist == 'no')
        {
            echo <<< FORM2B
            <form action="{$_SERVER['PHP_SELF']}" method="post">
            <label for="softwareName">Software Name:<br/></label><input type="text" name="softwareName" id="softwareName"><br/>
            <label for="manufacturer">Manufacturer:<br/></label><input type="text" name="manufacturer" id="manufacturer"><br/>
            <input type="submit" name="submit" value="Submit" >
            </form>
FORM2B;
        
        }
        
    }
 
What the previous code does is:
If they clicked yes, it'll give a list of the things already in the table, while making the value of each <select> the primary key, which I need to input into my next query as a foreign key.
If they clicked no, it just has another form to enter a new manufacturer and softwarename.

After they submit either of these two pages, it is process through the following code:

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'])))
    {
        echo 'third page<br/>';
        
        ////////////////////////////////////////////////////////////
        //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);
            $_SESSION['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 = ;
            $_SESSION['mainID'] = $_POST['nameManu'];
            echo $mainID;
        }
        
        
        echo <<< HIDDENFORM
        <form action="{$_SERVER['PHP_SELF']}" name="hiddenForm" method="post">
        <input type="hidden" value="1" name="versionPage" />
        </form>
HIDDENFORM;
    }
 
The previous code does the following:
If they had to enter new information on the previous page, the information is queried, and the primary key is stored in the variable $mainID.
If they picked from the list of values, the primary key of the <select> they selected is stored in the variable $mainID.

So the problem is, I need to be able to access the value of this variable in my next section of code because it needs to be input into the database with the information from the next form, but am unsure of how to do that without using $_SESSION.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 3:07 pm
by Eric!
mgwisni wrote: The previous code does the following:
If they had to enter new information on the previous page, the information is queried, and the primary key is stored in the variable $mainID.
If they picked from the list of values, the primary key of the <select> they selected is stored in the variable $mainID.

So the problem is, I need to be able to access the value of this variable in my next section of code because it needs to be input into the database with the information from the next form, but am unsure of how to do that without using $_SESSION.
I am dense. You have to bare with me. What exactly is the problem and where in all this code is it? I'm repeating my self here: Is it the value $mainID on Line 38 (of your original code) not existing or an invalid value or I'm I looking at the wrong thing?

I'm also confused by what the "next section of code" is. Is that something other than the stuff you posted?

Lines 42 and 43 of your new code don't make any sense. $mainID will not be affected by any of those lines, however the next PHP page you call will be able to pull old value of $mainID out of $_SESSION['mainID'].

If you want to pass on the value to a new page you need to post it again via $_POST['mainID']=$mainID; If you have data that you need to span multiple pages then you can use sessions instead of post, just remember to call session_start before trying to read the data. I don't know what you are doing with $mainID and the lable 'nameManu' but use lables that make sense to your code rather than switching them around.

Code: Select all

session_start();
// code gets a value for $mainID
$_SESSION['mainID']=$mainID;
other page(s)

Code: Select all

session_start();
$mainID=$_SESSION['mainID'];
// some code changes $mainID for some reason, need to resubmit
$_SESSION['mainID']=$mainID;  // update with newer value
The same can be done with $_POST, but you need to remember to carry it through each page weather it is used or not.

Re: Newb having trouble with Nested If Statements and Variables

Posted: Tue Jun 23, 2009 3:22 pm
by mgwisni
Hey Eric!, thanks for all of your help, and sorry for being so confusing. I think I've solved my problem, but I will definitely be back here in the future when I stumble into more problems. I will try to be more clear next time, but I really appreciate your help.

Matt