473,746 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

drop down php/mysql

153 New Member
Hi all,

This is my first attempt at anything to do with php/mysql so any help will be greatly appreciated.

I have been set a challenge to come up with a web app to enable staff to log on to the page, select their name in a drop down box and then set their current location and time due to leave that location. Then press submit to update their record on staff database.

So far I have gotten any names in database loaded into a drop down box and have 2 further text fields to enter in location and time details. Finding a time function is for another day!

here's my code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <?php
  3. $connection = mysql_connect("localhost", "root", "");
  4. if(!$connection)
  5. {
  6. die("database failed " . mysql_error());
  7. }
  8. $db_select = mysql_select_db("staff_status", $connection);
  9. if(!$db_select)
  10. {
  11. die("database selection failed " .mysql_error());
  12. }
  13. echo $db_select;
  14. ?>
  15.  
  16. <form>
  17. <select>
  18. <?php 
  19. $sql="SELECT id,staff_name FROM status_staff";
  20. $result =mysql_query($sql);
  21. while ($data=mysql_fetch_assoc($result))
  22. {
  23. echo ("<option value=".$data['id'].">". $data['staff_name']."</option>"); 
  24. ?>
  25. <?php } ?>
  26. </select>
  27. </form> 
  28. <form action="drop.php" method="post">
  29.  
  30.  
  31.     <br>
  32. Location: 
  33. <input type="text" name="location">
  34. <br>
  35. Time Leaving: 
  36. <input type="text" name="time">
  37. <br>
  38. <input type="Submit">
  39.  
  40. </html>
  41.  
So far so good but for the life of me I can't find a tutorial to show me how to handle the selection. What I want this app to do is update the record of the person selected with the inputted location and time overiding the last input they did.

Any pointers please?
Jan 21 '09 #1
20 6504
Markus
6,050 Recognized Expert Expert
Ok, the MySQL will need to be an UPDATE query. You will also need to know how to get the selected value of the dropdown (I will explain that below). However, currently, looking at your form, there is a problem: you <form> has no action or method. Not a huge issue, but it is good practice to tell your form what you want it to do. I expect the form is posting to the same page, but I don't know whether you plan on using GET or POST (or if you even know about those?). I'll assume you will use POST. Make your <form> into this:

Expand|Select|Wrap|Line Numbers
  1. <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
  2.  
Now when the form is submitted, all the values of the form will be in the POST array, which we can access by using the name attribute of the form elements as the array key, like so:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo $_POST['element_name'];
  4.  
  5. ?>
  6.  
Overlooking your code again, I see your <select> has no name attribute - this is necessary. Otherwise, the element's value won't be available in the POST array. So add name="user" to your <select>. Note: you will have to have a name attribute for every form element.

Check this tutorial out, for help with forms & php: PHP Tutorial - Forms
Jan 21 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
@Markus
it's a POST to "drop.php", line 28. there's just one <form> too many.

btw. "action" is a required attribute
Jan 21 '09 #3
brendanmcdonagh
153 New Member
so is the code so bad that it can't be made to work?
Jan 21 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
if you apply Markus' improvements, then certainly not.
Jan 21 '09 #5
brendanmcdonagh
153 New Member
Thanks Dormilich for swift reply

Here is code with suggestions added:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <?php
  3. $connection = mysql_connect("localhost", "root", "");
  4. if(!$connection)
  5. {
  6. die("database failed " . mysql_error());
  7. }
  8. $db_select = mysql_select_db("staff_status", $connection);
  9. if(!$db_select)
  10. {
  11. die("database selection failed " .mysql_error());
  12. }
  13. echo $db_select;
  14. ?>
  15.  
  16. <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
  17. <select name="user">
  18. <?php 
  19. $sql="SELECT id,staff_name FROM status_staff";
  20. $result =mysql_query($sql);
  21. while ($data=mysql_fetch_assoc($result))
  22. {
  23. echo ("<option value=".$data['id'].">". $data['staff_name']."</option>"); 
  24. ?>
  25. <?php } ?>
  26. <?php 
  27.  
  28. echo $_POST['staff_name']; 
  29.  
  30. ?>
  31. </select>
  32. </form> 
  33. <form action="drop.php" method="post">
  34.  
  35.  
  36.     <br>
  37. Location: 
  38. <input type="text" name="location">
  39. <br>
  40. Time Leaving: 
  41. <input type="text" name="time">
  42. <br>
  43. <input type="Submit">
  44.  
  45. </html>
  46.  
  47.  
I know I'm out of my depth here but if I was to get it working I could understand/learn from reading the working code.

Any further instructions please?
Jan 21 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
there's still work to do...
minor details:
- line 23, you should html-quote the attribute values
Expand|Select|Wrap|Line Numbers
  1. echo '... value="' . $var . '" ...';
  2. // or
  3. echo "... value=\"" . $var . "\" ...";
  4. // giving
  5. ... value="content_of_$var" ...
- line 24–26, replace by } (the <?php and ?> remove themselves)

major details:
- the values from the first form are never sent (there's no submit button for that form). you should make only one <form> which will process the results. I guess it's the second one. remember, only the values from inside the <form> are submitted (you may have multiple form elements on a side, but only the one whose submit is pressed actually sends data).
Jan 21 '09 #7
brendanmcdonagh
153 New Member
thanks again but just don't understand what you are saying to do.

I have never asked anyone on the site to show me with my variables, but I just don't think ill get it any other way.

If you can't do that for me, thanks again, I'll just stick to java!
Jan 21 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
in the end, the html code should look like this (more or less):
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <form action="drop.php" method="post">
  4.    <select name="user">
  5.      <option value="ID1">NAME1</option>
  6.      <option value="ID2">NAME2</option>
  7.      <option value="ID3">NAME3</option>
  8.  // ...
  9.   </select>
  10.   <br>
  11. Location: 
  12.    <input type="text" name="location">
  13.    <br>
  14. Time Leaving: 
  15.    <input type="text" name="time">
  16.    <br>
  17.    <input type="Submit" value="send">
  18. </form>
  19.  
  20. </html>
in the final version, drop lines 13 and 28 (this is nothing for the eyes of the user, only for debugging)

the file "drop.php" has to handle the database update.

EDIT: the problem of your code does not come from the PHP side, it's the HTML code that's not right.
Jan 21 '09 #9
brendanmcdonagh
153 New Member
Dormilich, thank you! I now know what the meaning of the word post means!

So after a bit of copying and pasting (and learning) I have 2 files which look like they should do what im trying.

here's insert1.php

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <?php
  3. $connection = mysql_connect("localhost", "root", "");
  4. if(!$connection)
  5. {
  6. die("database failed " . mysql_error());
  7. }
  8. $db_select = mysql_select_db("staff_status", $connection);
  9. if(!$db_select)
  10. {
  11. die("database selection failed " .mysql_error());
  12. }
  13. echo $db_select;
  14. ?>
  15.  
  16. <form action="insert2.php" method="post">
  17. <select name="user">
  18. <?php 
  19. $sql="SELECT id,staff_name FROM status_staff";
  20. $result =mysql_query($sql);
  21. while ($data=mysql_fetch_assoc($result))
  22. {
  23. echo ("<option value=".$data['id'].">". $data['staff_name']."</option>");
  24. ?>
  25. </select>
  26. <br>
  27. Location: <input type="text" name="location">
  28. Time Leaving: <input type="text" name="time">
  29.  
  30. <input type="Submit">
  31.  
  32.  
  33.  
  34. </form> 
  35. </html>
  36.  
I have tried to stay with importing the names from database to show in dropdown box as this was working and has been my only success today:(




Here's insert2.php



Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. $connection = mysql_connect("localhost", "root", "");
  5. if(!$connection)
  6. {
  7. die("database failed " . mysql_error());
  8. }
  9. $db_select = mysql_select_db("staff_status", $connection);
  10. if(!$db_select)
  11. {
  12. die("database selection failed " .mysql_error());
  13. }
  14. echo $db_select;
  15.  
  16. $name = $_POST['staff_name']; 
  17. $location = $_POST['location'];
  18. $time = $_POST['time'];
  19. $query2 = "INSERT INTO status_staff VALUES ('$name','$location','$time')";
  20. mysql_query($query2);
  21. mysql_close();
  22. ?>
  23.  
Am i getting closer to the concept of html/php/mysql?

Edit:
Jan 21 '09 #10

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

Similar topics

1
2504
by: jonnytansey2 | last post by:
Can anyone out there give me a pointer regarding creating a dynamically-generated drop-down list connected to an array? And is that question as clear as chocolate spread? Here's what I've got. I'm using PHP and MySQL database. I'm customizing some calendar software, and I want the user to fill in a form by selecting a title from a drop-down list, generated by my MySQL database. However, the program I'm customizing uses arrays, which is...
1
1986
by: brino | last post by:
hi all ! i'm kind of new to MYSQL so am still learning basics. my question is how do you create a drop down list in a field in an SQL database ? thanks brino
5
18724
by: ashok893 | last post by:
I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database. For example, the first drop down list contains Animals, Birds... If i select the Animal option, the second drop down list should show like Lion, Tiger.... Both lists should...
0
7359
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from Mysql. Anybody can help me please.. I will be very much thankfull. Please help me and give me a proper solution for that ASAP. Thanks Tushar
2
3066
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 variable. I have created a dynamic drop down menu that populates itself with data from a MySQL table. I now want it to populate a second drop-down menu with relevant options from my database, based upon a query using a variable. For example........
2
5254
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu, but I am unable to store a variable for it. Here is a rough copy of what I am making: <html> <head> <title>Add New MySQL User</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head>
5
2906
by: mramsay | last post by:
HI, I'm having alot of problems trying to figure out how to create drop down links from a hyperlink on my homepage. I have a hyperlink called Programs (this is for a community centre) When the Programs hyperlink is selected I would like a drop down of link from my programs mysql table to be displayed. So far, it takes my mysql data and puts it as link but on it's own. I'm not sure how to put it under my Programs hyperlinks. Also...
10
2715
by: darkenroyce | last post by:
Hi Guys I am creating a sports database and one of the features involves create drop down dialogue boxes that retrieve data from MySQL tables and provides them as <option> within <select></select>. I am using PHP for this and I'm fairly new at the programming language. I have got the dialogue to work with respect to retrieving the data from MySQL table but it only displays the data from first row/first column of the table twice. I want the...
1
2964
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button is pressed then a text box appears to enter a new question or answer or both. the newly entered data is then inserted into mysql. The newly entered data is then requested by mysql_fetch_array() to be displayed as options in the drop down list. If...
0
8970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8796
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9280
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9214
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8221
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6057
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.