HELP: I don't know where I did wrong...

Discussion in 'PHP' started by JoeyWong, Nov 10, 2010.

  1. #1
    Hi all, I have 2 selection in my code. After select first selection, user need to click Go button then will show the second selection menu which depend on the first selection. But, what problem I faced is when I click Go button nothing display at second selection menu. Is it the sql wrong or what?? Can someone help me?? thanks~

    <body>
    <h2>insert course</h2>
    <form method="post" action="" name="form">
    Institution Name:
    <?php $sql = "SELECT DISTINCT i_name FROM school ORDER BY i_name";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["InstitutionName"]))
    $InstitutionName = $_POST["InstitutionName"];
    echo"<select name='InstitutionName'>";
    while ($row = mysql_fetch_array($result))
    { $i_name=$row["i_name"];
    echo"<option value='$i_name'";
    if (isset($InstitutionName) && $InstitutionName == $i_name)
    echo " selected> $i_name";
    else echo "> $i_name"; }
    echo "</select>"; ?>
    <input type="submit" name="submit" value="Go" />
    </form>
    School Name:
    <?php
    if(isset($_POST["submit"])){
    $submit = $_POST["submit"];
    if($submit=="Go")
    {
    $InstitutionName=$_POST['InstitutionName'];
    $sql = "SELECT DISTINCT s_name from school WHERE i_name=$InstituionName ORDER BY s_name";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["SchoolName"]))
    $SchoolName = $_POST["SchoolName"];
    echo"<select name='SchoolName'>";
    while ($row = mysql_fetch_array($result))
    { $s_name=$row["s_name"];
    echo"<option value='$s_name'";
    if (isset($SchoolName) && $SchoolName ==$s_name)
    echo " selected> $s_name";
    else echo "> $s_name"; }
    echo "</select><br>";}}
    ?>
    <body>
     
    JoeyWong, Nov 10, 2010 IP
  2. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    You're using this variable "$InstituionName" in the SQL, and there's no variable named like that because you have a typo. Fix Instituion, it should be Institution.
     
    ASTURIAS, Nov 10, 2010 IP
  3. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But it is still the same... Second selection menu display nothing...
     
    JoeyWong, Nov 11, 2010 IP
  4. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #4
    From where are you getting SchoolName?, is that an input name?, I don't see it. I mean, in order to be able to POST the value of SchoolName and have it return the other form, you must display the form, and have SchoolName as one of the input's name, so that you can submit it. Here, you're testing to see if SchoolName returns true:

    if(isset($_POST["SchoolName"]))

    else

    it wont display the form.

    Also, add the opening brackets to (isset($_POST["SchoolName"])) { { , and end the body tag <body> with </body>
     
    Last edited: Nov 11, 2010
    ASTURIAS, Nov 11, 2010 IP
  5. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    SchoolName is a selection menu which get from database.
     
    JoeyWong, Nov 11, 2010 IP
  6. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #6
    But before you display the second form, you must submit it, by clicking a button, that way X value is sent to the PHP program for processing. In your current code, you test SchoolName first and after it returns true, it displays the form, but since you never submitted a form containing the name "SchoolName" in one of the inputs, it takes it as false or not-set (!isset).
     
    ASTURIAS, Nov 11, 2010 IP
  7. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Can you help me to modify? I know what you mean but i don't know how to do it. I am just a beginner of PHP. Thanks~
    Database: test1
    table: course
    s_id, i_name, s_name, c_name, mode, duration, fee, requirement, description

    <?php
    $connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() );
    $selection = mysql_select_db("test1") or die("Unable to select database. " .mysql_error());
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Insert Course</title>
    <script language="javascript">
    var counter = 0;
    function moreFields(){
    counter++;
    var newFields = document.getElementById('readroot').cloneNode(true);
    newFields.id = '';
    newFields.style.display = 'block';
    var newField = newFields.childNodes;
    for (var i=0;i<newField.length;i++)
    {
    var theName = newField.name
    if (theName)
    newField.name = theName + counter;
    }
    var insertHere = document.getElementById('writeroot');
    insertHere.parentNode.insertBefore(newFields,insertHere);
    }
    window.onload = moreFields;
    </script>
    </head>

    <body>
    <h2>insert course</h2>
    <form method="post" action="" name="form">
    Institution Name:
    <?php $sql = "SELECT DISTINCT i_name FROM school ORDER BY i_name";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["InstitutionName"]))
    $InstitutionName = $_POST["InstitutionName"];
    echo"<select name='InstitutionName'>";
    while ($row = mysql_fetch_array($result))
    { $i_name=$row["i_name"];
    echo"<option value='$i_name'";
    if (isset($InstitutionName) && $InstitutionName == $i_name)
    echo " selected> $i_name";
    else echo "> $i_name"; }
    echo "</select>"; ?>
    <input type="submit" name="submit" value="Go" />
    </form>
    School Name:
    <?php
    if(isset($_POST["submit"])){
    $submit = $_POST["submit"];
    if($submit=="Go")
    {
    $InstitutionName=$_POST['InstitutionName'];
    $sql = "SELECT DISTINCT s_name from school WHERE i_name=$InstitutionName ORDER BY s_name";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["SchoolName"]))
    $SchoolName = $_POST["SchoolName"];
    echo"<select name='SchoolName'>";
    while ($row = mysql_fetch_array($result))
    { $s_name=$row["s_name"];
    echo"<option value='$s_name'";
    if (isset($SchoolName) && $SchoolName ==$s_name)
    echo " selected> $s_name";
    else echo "> $s_name"; }
    echo "</select><br>";}}
    ?>

    <div id="readroot" style="display: none">
    <br /><input type="button" value="Remove" onClick="this.parentNode.parentNode.removeChild(this.parentNode);" />
    <table>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Course Name</td>
    <td>:</td>
    <td>
    <input type="text" name="CourseName" size="50" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Mode of Study</td>
    <td>:</td>
    <td>
    <input type="text" name="ModeOfStudy" size="50" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Duration</td>
    <td>:</td>
    <td>
    <input type="text" name="Duration" size="50" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Total Fees (Local)</td>
    <td>:</td>
    <td>
    RM&nbsp;<input type="text" name="TotalFees" size="45" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Entry Requirement</td>
    <td>:</td>
    <td>
    <input type="text" name="EntryRequirement" size="50" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="left">Description of Course</td>
    <td>:</td>
    <td>
    <input type="text" name="Description" size="50" />
    </td>
    </tr>
    </table>
    </div>

    <form method="post" action="view_insert_course_test.php">

    <span id="writeroot"></span>

    <input type="button" value="Add" onClick="moreFields()"/>
    <input type="submit" value="Insert Record" />
    </form>


    </form>
    </body>
    </html>
     
    JoeyWong, Nov 11, 2010 IP
  8. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #8
    Try this code on the first example you had, and see if it works:

     
    ASTURIAS, Nov 11, 2010 IP
  9. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Warning: mysql_num_rows() expects parameter 1 to be resource.
     
    JoeyWong, Nov 11, 2010 IP
  10. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #10
    Sorry, you had the sql query separate. Replace if(mysql_num_rows($sql) > 0) { with if(mysql_num_rows($result) > 0) {
     
    ASTURIAS, Nov 11, 2010 IP
  11. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry for disturb!! Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\innosphere\insert_course.php on line 60
     
    JoeyWong, Nov 11, 2010 IP
  12. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #12
    Well, I need to be in the actual server so that I can debug the program, reading only the code only allows me to give assumptions to what the problem might be.
     
    ASTURIAS, Nov 11, 2010 IP
  13. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    anyway... thanks for your help!!
     
    JoeyWong, Nov 11, 2010 IP