Trouble with multiple PHP/SQL queries?

Discussion in 'PHP' started by Junioreality, Aug 23, 2011.

  1. #1
    I'm attempting to build a website with various PHP/SQL functions for a fictional motorhome dealership as part of an assignment. One of the required functions is to update entries in the SQL database, which is currently giving me a single, large problem.
    The update process is made up of 3 pages, the first has a dynamically filled dropdown menu that populates with all the ID numbers of all the database entires. When one is chosen and the 'submit' button is pressed, the next page comes up. Here, with the exception of the ID number, all the information in the appropriate row is displayed in it's own text field. This works no problem, however, when the submit button on this page is hit, I'm given the error code 'Undefined index: motorID' and I can't figure out why.
    This is my current PHP coding:
    Page 1:
    <form name = "UpdateMotorhome" id = "UpdateMotorhome"
    action = "adminUpdateQuery1.php" method = "post">

    <?php
    include 'database_conn.php';
    $sql = "SELECT motorID FROM motorhomes";
    $result = mysql_query($sql)
    or die(mysql_error());

    $options = "";

    while ($row = mysql_fetch_array($result)) {

    $motorID = $row['motorID'];
    $options.= "<OPTION VALUE=\"$motorID\">".$motorID.'</option>…
    }
    ?>

    <SELECT NAME=motorID>
    <OPTION VALUE=0>Choose
    <?=$options?>
    </SELECT>
    <input type = "submit" value = "submit" />
    </form>

    Page 2:
    <?php

    $mID = $_REQUEST['motorID'];

    include 'database_conn.php';
    $sql = "SELECT * FROM motorhomes WHERE motorID = '$mID'";
    $queryresult = mysql_query($sql)
    or die(mysql_error());

    while ($row = mysql_fetch_assoc($queryresult)){
    $motorID = $row['motorID'];
    $manufacturer = $row['manufacturer'];
    $model = $row['model'];
    $year = $row['year'];
    $engine = $row['engine'];
    $berths = $row['berths'];
    $mileage = $row['mileage'];
    $price = $row['price'];
    $layout = $row['layout'];
    $ownership = $row['ownership'];
    $description = $row ['description'];
    ?>

    <form name = "UpdateMotorhomePart2" id = "UpdateMotorhomePart2"
    action = "adminUpdateQuery2.php" method = "post">

    <?php
    echo "<div id=\"result\">";
    echo "Motorhome ID: $motorID
    ";
    echo "Manufacturer: <input type = \"text\" name = \"manufacturer\" value=\"$manufacturer\">
    ";
    echo "Model: <input type = \"text\" name = \"model\" value=\"$model\">
    ";
    echo "Year: <input type = \"text\" name = \"year\" value=\"$year\">
    ";
    echo "Engine: <input type = \"text\" name = \"engine\" value=\"$engine\">
    ";
    echo "Berths: <input type = \"text\" name = \"berths\" value=\"$berths\">
    ";
    echo "Mileage: <input type = \"text\" name = \"mileage\" value=\"$mileage\">
    ";
    echo "Price: <input type = \"text\" name = \"price\" value=\"$price\">
    ";
    echo "Layout: <input type = \"text\" name = \"layout\" value=\"$layout\">
    ";
    echo "Ownership: <input type = \"text\" name = \"ownership\" value=\"$ownership\">
    ";
    echo "Description: <input type = \"text\" name = \"description\" value=\"$description\">
    ";
    echo "<hr />";
    echo "</div>";
    }
    ?>

    <input type = "submit" value = "submit" />

    </form>

    Page 3:
    <?php
    include 'database_conn.php';

    $motorID = $_REQUEST['motorID']; //Line error keeps occuring on
    $manufacturer = $_REQUEST['manufacturer'];
    $model = $_REQUEST['model'];
    $year = $_REQUEST['year'];
    $engine = $_REQUEST['engine'];
    $berths = $_REQUEST['berths'];
    $mileage = $_REQUEST['mileage'];
    $price = $_REQUEST['price'];
    $layout = $_REQUEST['layout'];
    $ownership = $_REQUEST['ownership'];
    $description = $_REQUEST['description'];

    $sql = "UPDATE motorhomes SET manufacturer = '$manufacturer', model = '$model', year = '$year',
    engine = '$engine', berths = '$berths', mileage = '$mileage', price = '$price', layout = '$layout',
    ownership = '$ownership', description = '$description' WHERE motorID = '$motorID'";
    $queryresult = mysql_query($sql)
    or die(mysql_error());

    echo "Record Updated";

    ?>

    Thanks for any advise in advance.

    I tried a similar set up with the Remove from database function (without editable data on 2nd page) that gave me the same problem. I eventually removed the 2nd page and the function started working correctly.
     
    Junioreality, Aug 23, 2011 IP
  2. swiminsoda

    swiminsoda Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    A lot of small logic and syntax errors in this code.
    Let's rewrite it as a single scrpt!

    NEWSCRIPT.PHP
    require_once ("database_conn.php");
    <head>
    <?php
    function select_vehicle()
    {
    $txt = "<form name = \"UpdateMotorhome\"
    action = \"".$_SERVER['PHP_SELF']."\" method = \"POST\">";
    $link = dbconnect(); // whatever function to connect and
    select db in the file database_conn.php
    $sql = "SELECT `motorID` FROM `motorhomes`";
    $result = mysql_query($sql) or die(mysql_error());
    $txt .= "<select name=\"vehicle\">";
    while ($row = mysql_fetch_array($result))
    {
    $motorID = $row['motorID'];
    $txt .= "<option value=\"".$motorID."\">";
    $txt .= $motorID."</option>";
    }
    mysql_free_result($result);
    mysql_close($link);
    $txt .= "</select>";
    $txt .= "<input type = \"submit\" value = \"submit\" />";
    $txt .= "</form>";
    return($txt);
    }
    function edit_vehicle($mID)
    {
    $link = dbconnect();
    $sql = "select * from `motorhomes` where `motorID` = '".$mID."' limit 1";
    // Not syntax! "`" to enclose table and fields name
    // there whould only be ONE motorID
    $queryresult = mysql_query($sql)
    or die(mysql_error());
    $row = mysql_fetch_array($queryresult);
    $txt = "<form name = \"UpdateMotorhomePart2\"
    action = \"".$_SERVER['PHP_SELF']."\" method = \"post\">";
    $txt .= "Motorhome ID: ".$row['motorID']."<br>";
    $txt .= "Manufacturer: <input type = \"text\" name = \"manufacturer\"
    value=\"".$row['manufacturer']."\"><br…
    $txt .= "Model: <input type = \"text\" name = \"model\"
    value=\"".$row['model']."\"><br>";
    $txt .= "Year: <input type = \"text\" name = \"year\"
    value=\"".$row['year']."\">";
    $txt .= "Engine: <input type = \"text\" name = \"engine\"
    value=\"$row['engine']."\"><br>";
    $txt .= "Berths: <input type = \"text\" name = \"berths\"
    value=\"".$row['berths']."\"><br>";
    $txt .= "Mileage: <input type = \"text\" name = \"mileage\"
    value=\"".$row['mileage']."\"><br>";
    $txt .= "Price: <input type = \"text\" name = \"price\"
    value=\"".$row['price']."\"><br>";
    $txt .= "Layout: <input type = \"text\" name = \"layout\"
    value=\"".$row['layout']."\"><br>";
    $txt .= "Ownership: <input type = \"text\" name = \"ownership\"
    value=\"".$row['ownership']."\"><br>";
    $txt .= "Description: <input type = \"text\" name = \"description\"
    value=\"".$row['description']."\"><br>…
    $txt .= "<input type=\"hidden\" name=\"motid\" value=\"".$mID."\" />";
    $txt .= "<input type = \"submit\" value = \"submit\" />";
    $txt .= "</form>";
    return($txt);
    }
    function update_vehicle($_POST)
    {
    $link = dbconnect();
    $sql = "update `motorhomes` SET `manufacturer` = '".$_POST['manufacturer']."',
    `model` = '".$_POST['model']."', `year` = '".$_POST['year']."',
    `engine` = '".$_POST['engine']."', `berths` = '".$_POST['berths']."',
    `mileage` = '".$_POST['mileage']."', `price` = '".$_POST['price']."',
    `layout` = '".$_POST['layout']."', `ownership` = '".$_POST['ownership']."',
    `description` = '".$_POST['description']."' where `motorID` = '".$motid."' limit 1";
    $queryresult = mysql_query($sql)
    or die(mysql_error());
    mysql_close($link);
    return ("Record Updated");
    }
    ?>
    </head>
    <body>
    <?php
    if (isset($_POST['motid']))
    {
    echo (update_vehicle($_POST));
    unset($POST['motid']);
    }
    else if (isset($_POST['vehicle']))
    {
    echo (edit_vehicle($_POST['vehicle']));
    unset($_POST['vehicle']);
    }
    else
    {
    echo (select_vehicle());
    }
    ?>
    </body>

    (Untested)
     
    swiminsoda, Aug 23, 2011 IP
  3. om39a

    om39a Peon

    Messages:
    287
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    motorID had to be part of your form on page 2.
     
    om39a, Aug 23, 2011 IP
  4. Junioreality

    Junioreality Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    At first, I misunderstood what this answer meant, but after a few minutes got the function working correctly. Thanks for your help guys!
     
    Junioreality, Aug 23, 2011 IP