php drop down menu help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wizardry
    New Member
    • Jan 2009
    • 201

    php drop down menu help

    hello -

    i've created a drop down menu in dreamweaver, it selects my states from a list table that i have for users table.

    I went from enum to list table and created a fk to the parent table.

    i'm use to doing in access a lookup filed to a list this is what i'm trying to pretty much accomplish.

    i'm able to pull the data no problem. just when it goes to insert into the child table to states it errors. cannot add or update a child row. because it's trying to insert the wrong data type.

    how can i query both items from the parent and insert only the selected states primary_id not name into the child table?

    thanks in advance for your help
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Can you provide the code you have used to try this. Also, any error messages and any info that could be useful. Also, maybe an example (in concise english) of what the outcome should be.

    Comment

    • wizardry
      New Member
      • Jan 2009
      • 201

      #3
      sorry i've been up late programming and kind!

      1. create a drop down list.
      2. when the item is selected, insert it's id, not name into the table

      I'm able to query the database for the drop down menu items, but i can't think of a way to insert the id and not the item name.

      Code:
      <?php require_once('Connections/userstatesTest.php'); ?>
      <?php require_once('Connections/enumTest.php'); ?>
      <?php
      if (!function_exists("GetSQLValueString")) {
      function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
      {
        if (PHP_VERSION < 6) {
          $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }
      
        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
      
        switch ($theType) {
          case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;    
          case "long":
          case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;
          case "double":
            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
            break;
          case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
          case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
        }
        return $theValue;
      }
      }
      
      $editFormAction = $_SERVER['PHP_SELF'];
      if (isset($_SERVER['QUERY_STRING'])) {
        $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
      }
      
      if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
        $insertSQL = sprintf("INSERT INTO users (users_id, users_name, states_id) VALUES (%s, %s, %s)",
                             GetSQLValueString($_POST['users_id'], "int"),
                             GetSQLValueString($_POST['users_name'], "text"),
                             GetSQLValueString($_POST['states_id'], "int"));
      
        mysql_select_db($database_enumTest, $enumTest);
        $Result1 = mysql_query($insertSQL, $enumTest) or die(mysql_error());
      }
      
      mysql_select_db($database_userstatesTest, $userstatesTest);
      $query_Recordset1 = "SELECT * FROM states";
      $Recordset1 = mysql_query($query_Recordset1, $userstatesTest) or die(mysql_error());
      $row_Recordset1 = mysql_fetch_assoc($Recordset1);
      $totalRows_Recordset1 = mysql_num_rows($Recordset1);
      ?>
      <!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>Untitled Document</title>
      </head>
      
      <body>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Users_id:</td>
            <td><input type="text" name="users_id" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Users_name:</td>
            <td><input type="text" name="users_name" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">States_id:</td>
            <td><select name="states_id">
              <?php 
      do {  
      ?>
              <option value="<?php echo $row_Recordset1['states_name']?>" ><?php echo $row_Recordset1['states_name']?></option>
              <?php
      } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
      ?>
            </select></td>
          </tr>
          <tr> </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="Insert record" /></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
      <p>&nbsp;</p>
      </body>
      </html>
      <?php
      mysql_free_result($Recordset1);
      ?>
      error message was child constraint, that because it's trying to insert the name, not id into the child table.

      i have a states table
      Code:
      | states | CREATE TABLE `states` (
        `states_id` bigint(20) NOT NULL DEFAULT '0',
        `states_name` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`states_id`)
      ) ENGINE=InnoDB
      here is the users table

      Code:
      | users | CREATE TABLE `users` (
        `users_id` bigint(20) NOT NULL DEFAULT '0',
        `users_name` varchar(20) DEFAULT NULL,
        `states_id` bigint(20) NOT NULL DEFAULT '0',
        PRIMARY KEY (`users_id`,`states_id`),
        KEY `states_id` (`states_id`),
        CONSTRAINT `users_ibfk_1` FOREIGN KEY (`states_id`) REFERENCES `states` (`states_id`) ON DELETE CASCADE ON UPDATE CASCADE
      ) ENGINE=InnoDB

      thanks again for your help!

      Comment

      • wizardry
        New Member
        • Jan 2009
        • 201

        #4
        also, states is a list table with a 1 => 8 to users.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          My eyes hurt. Anyway, why not pass the states ID instead of it's name to the value attribute of the drop down. Or am I misunderstandin g your problem?

          Comment

          • wizardry
            New Member
            • Jan 2009
            • 201

            #6
            the drop down wont display the name of the state if i pass it the state id, unless you know how?

            Comment

            • wizardry
              New Member
              • Jan 2009
              • 201

              #7
              ok i've got it accomplished thanks for your help.

              it was a setting on the app side that i need to call entity states_name, but get the states_id value for insert

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by wizardry
                ok i've got it accomplished thanks for your help.

                it was a setting on the app side that i need to call entity states_name, but get the states_id value for insert
                Glad you got it working.

                - Markus.

                Comment

                Working...