PHP Question

Discussion in 'PHP' started by dean5000v, Jul 17, 2008.

  1. #1
    Ok well i've created a php script that basically echo's all of the tables within a database into a select box so that a user can select the table to use it then sets the $table variable to the selection that they have made once they click submit.

    here is the code i used.

    $sql = "SHOW TABLES FROM $dbname";
    $result = mysql_query($sql);
    
    if (!$result) {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] ."\">"; 
    echo "<span class=\"style\">Please Select a Category:</span> &nbsp;&nbsp;&nbsp;<select name=\"dbtables[]\" id=\"dbtables\">\n";
    echo "<option value=\"\"></option>";
    $accept = array();
    while ($row = mysql_fetch_row($result)) {
    
        echo "<option value=\"".$row[0]."\">".$row[0]."</option>\n";
        $accept[$row[0]] = true;
    }
    
    if(!isset($accept[$_POST['dbtables']])) { 
    echo "<input type=\"submit\" name=\"select_table\" value=\"Submit\"></select></form>";
    mysql_free_result($result);
    $table = $dbtables[0]; 
    $table = trim($table); 
    } 
    ?>
    Code (markup):
    this works great however further down i have used another form which basically inserts a stock code, description and price into the table that they have selected so the sql i have used is this.

    $sql="INSERT INTO `$table` (item_code, item_desc, item_price)VALUES('$item_code', '$item_desc', '$item_price')";
    Code (markup):
    which works fine when i don't use the $table variable and type in the table automatically. so the problem is once i use the next form it resets the $table variable to nothing because they are in different forms.

    Does anyone know a way around this !

    so would javascript do the job for me, ot would there be a way i can insert it into the same form as my insert one
     
    dean5000v, Jul 17, 2008 IP
  2. wdillsmith

    wdillsmith Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you have to do it in one form.
     
    wdillsmith, Jul 17, 2008 IP