Connecting Tech Pros Worldwide Forums | Help | Site Map

php drop down menu help

Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#1: Feb 18 '09
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's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: Feb 18 '09

re: php drop down menu help


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.
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#3: Feb 18 '09

re: php drop down menu help


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.

Expand|Select|Wrap|Line Numbers
  1. <?php require_once('Connections/userstatesTest.php'); ?>
  2. <?php require_once('Connections/enumTest.php'); ?>
  3. <?php
  4. if (!function_exists("GetSQLValueString")) {
  5. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  6. {
  7.   if (PHP_VERSION < 6) {
  8.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  9.   }
  10.  
  11.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  12.  
  13.   switch ($theType) {
  14.     case "text":
  15.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  16.       break;    
  17.     case "long":
  18.     case "int":
  19.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  20.       break;
  21.     case "double":
  22.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  23.       break;
  24.     case "date":
  25.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  26.       break;
  27.     case "defined":
  28.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  29.       break;
  30.   }
  31.   return $theValue;
  32. }
  33. }
  34.  
  35. $editFormAction = $_SERVER['PHP_SELF'];
  36. if (isset($_SERVER['QUERY_STRING'])) {
  37.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  38. }
  39.  
  40. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  41.   $insertSQL = sprintf("INSERT INTO users (users_id, users_name, states_id) VALUES (%s, %s, %s)",
  42.                        GetSQLValueString($_POST['users_id'], "int"),
  43.                        GetSQLValueString($_POST['users_name'], "text"),
  44.                        GetSQLValueString($_POST['states_id'], "int"));
  45.  
  46.   mysql_select_db($database_enumTest, $enumTest);
  47.   $Result1 = mysql_query($insertSQL, $enumTest) or die(mysql_error());
  48. }
  49.  
  50. mysql_select_db($database_userstatesTest, $userstatesTest);
  51. $query_Recordset1 = "SELECT * FROM states";
  52. $Recordset1 = mysql_query($query_Recordset1, $userstatesTest) or die(mysql_error());
  53. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  54. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  55. ?>
  56. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  57. <html xmlns="http://www.w3.org/1999/xhtml">
  58. <head>
  59. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  60. <title>Untitled Document</title>
  61. </head>
  62.  
  63. <body>
  64. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  65.   <table align="center">
  66.     <tr valign="baseline">
  67.       <td nowrap="nowrap" align="right">Users_id:</td>
  68.       <td><input type="text" name="users_id" value="" size="32" /></td>
  69.     </tr>
  70.     <tr valign="baseline">
  71.       <td nowrap="nowrap" align="right">Users_name:</td>
  72.       <td><input type="text" name="users_name" value="" size="32" /></td>
  73.     </tr>
  74.     <tr valign="baseline">
  75.       <td nowrap="nowrap" align="right">States_id:</td>
  76.       <td><select name="states_id">
  77.         <?php 
  78. do {  
  79. ?>
  80.         <option value="<?php echo $row_Recordset1['states_name']?>" ><?php echo $row_Recordset1['states_name']?></option>
  81.         <?php
  82. } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  83. ?>
  84.       </select></td>
  85.     </tr>
  86.     <tr> </tr>
  87.     <tr valign="baseline">
  88.       <td nowrap="nowrap" align="right">&nbsp;</td>
  89.       <td><input type="submit" value="Insert record" /></td>
  90.     </tr>
  91.   </table>
  92.   <input type="hidden" name="MM_insert" value="form1" />
  93. </form>
  94. <p>&nbsp;</p>
  95. </body>
  96. </html>
  97. <?php
  98. mysql_free_result($Recordset1);
  99. ?>
  100.  
  101.  
  102.  
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
Expand|Select|Wrap|Line Numbers
  1. | states | CREATE TABLE `states` (
  2.   `states_id` bigint(20) NOT NULL DEFAULT '0',
  3.   `states_name` varchar(255) DEFAULT NULL,
  4.   PRIMARY KEY (`states_id`)
  5. ) ENGINE=InnoDB
  6.  
here is the users table

Expand|Select|Wrap|Line Numbers
  1. | users | CREATE TABLE `users` (
  2.   `users_id` bigint(20) NOT NULL DEFAULT '0',
  3.   `users_name` varchar(20) DEFAULT NULL,
  4.   `states_id` bigint(20) NOT NULL DEFAULT '0',
  5.   PRIMARY KEY (`users_id`,`states_id`),
  6.   KEY `states_id` (`states_id`),
  7.   CONSTRAINT `users_ibfk_1` FOREIGN KEY (`states_id`) REFERENCES `states` (`states_id`) ON DELETE CASCADE ON UPDATE CASCADE
  8. ) ENGINE=InnoDB 
  9.  

thanks again for your help!
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#4: Feb 18 '09

re: php drop down menu help


also, states is a list table with a 1 => 8 to users.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#5: Feb 18 '09

re: php drop down menu help


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 misunderstanding your problem?
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#6: Feb 18 '09

re: php drop down menu help


the drop down wont display the name of the state if i pass it the state id, unless you know how?
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#7: Feb 18 '09

re: php drop down menu help


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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#8: Feb 18 '09

re: php drop down menu help


Quote:

Originally Posted by wizardry View Post

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.
Reply