473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

php drop down menu help

201 100+
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
Feb 18 '09 #1
7 2122
Markus
6,050 Expert 4TB
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.
Feb 18 '09 #2
wizardry
201 100+
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!
Feb 18 '09 #3
wizardry
201 100+
also, states is a list table with a 1 => 8 to users.
Feb 18 '09 #4
Markus
6,050 Expert 4TB
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?
Feb 18 '09 #5
wizardry
201 100+
the drop down wont display the name of the state if i pass it the state id, unless you know how?
Feb 18 '09 #6
wizardry
201 100+
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
Feb 18 '09 #7
Markus
6,050 Expert 4TB
@wizardry
Glad you got it working.

- Markus.
Feb 18 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: vikram.cvk | last post by:
Hello Experts, Im trying to design a CSS vertical drop down menu which should have the following functionality. Home About Us | -->Overview
4
by: rajat | last post by:
Hi, I have adapted a drop down menu from USC webpage (www.usc.edu). The link to my page is http://www-scf.usc.edu/~swarup/test/test.html The links to the CSS ans JS files are:...
2
by: hemanth.singamsetty | last post by:
Hello there, I've a drop down menu (created using CSS & Javascript -- see code below). My problem is, whenever I click a link on the menu the new page replaces the current page (and the menu...
4
by: simon.cigoj | last post by:
I have an javascript made menu and some forms with the dropdown element. When the menu opens and scrolls down the drop down is displeyed over the menu and obscures the menu choices. I have this...
3
by: Andy Munnis | last post by:
Need help with creating a drop down menu. I am trying to replicate the GUI of a large db application that runs on large Sun servers using Oracle/Solaris, etc. My purpose is to create a stand-alone...
3
by: Reb | last post by:
Hi, I am using drop down menu in my pages. In one of my page,there is a listbox control below the menu. This listbox control is hiding the drop down menu items. How can i make the drop down...
1
by: StevePBurgess | last post by:
Hi I am using a drop down menu adapted from the one in Stylin with CSS by Charles Wyke Smith. The CSS is below. It works perfectly in Internet Explorer (using the csshover behaviour file) but...
3
by: rsteph | last post by:
I have a javascript drop down menu that I borrowed from a website. It utilizes a little .css to help with formatting. The menu works great, and on all 3 of the browsers I'm concerned about; but I am...
2
by: ponyeyes | last post by:
Hi There, I am a bit of a newbie to PHP programming and I would like to know how I can place a selected drop down option into a PHP variable and then produce an sql query which incorporates this...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.