473,396 Members | 1,898 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,396 software developers and data experts.

dropdown populate from db and update db

5
Hi I am very new to this and is stuck trying to make this work... :-(

I want a 2 dropdown box'es which is populated from a mysql db (that I can do, I think :-p )
[PHP]
$sql="SELECT X FROM table WHERE Y=Z";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

$X=$row["X"];
$options.="<option VALUE=\"$X\">".$X.'</option>';
}
?>

<SELECT NAME=something>
<OPTION VALUE=0>choose
<?php echo $options ?>
</SELECT>[/PHP]

I then want a button which runs a mysql update with the selected input in the dropdown boxes

the DB table is:
name
choice1 (should be from dropdownbox1)
choice2 (should be from dropdownbox2)

also (if possible) I would like that the selected value is the one that is the one in the db.

Really hope someone can help me :-)
Jul 5 '07 #1
3 1467
pbmods
5,821 Expert 4TB
Heya, gab. Welcome to TSDN!

For XHTML compliance, consider making your tags all lowercase.

As for your question, Create a form around your select elements, and set its action to the page you want to use to update the form (generally, this should be a separate page):
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(! empty($_GET['message']))
  3.         echo "<div style=\"text-align:center;\">$_GET[message]</div>";
  4. ?>
  5. <form action="dbi/updatePrefsOrWhatever.php" method="get">
  6.     <select name="choice1">
  7.         <?php
  8.             $sql = "SELECT * FROM X ...";
  9.             .
  10.             .
  11.             .
  12.         ?>
  13.     </select>
  14.  
  15.     <select name="choice2">
  16.         <option value="0">Choose:</option>
  17.         .
  18.         .
  19.         .
  20.     </select>
  21. </form>
  22.  
Then in updatePrefsOrWhatever.php:
Expand|Select|Wrap|Line Numbers
  1. If(empty($_GET['choice1']) && empty($_GET['choice2'])) {
  2.     header('Location: theOtherPage.php?message=Please%20make%20a%20choice');
  3.     exit();
  4. }
  5.  
  6. $name = '';  // Not sure what goes here.
  7. $choice1 = intval($_GET['choice1']);
  8. $choice2 = intval($_GET['choice2']);
  9.  
  10. //  connect to mysql db here.
  11.  
  12. $sql = "INSERT INTO `X` (`name`, `choice1`, `choice2`) VALUES('$name', $choice1, $choice2)";
  13. if(mysql_query($sql))
  14.     $message = 'Success';
  15. else
  16.     $message = urlencode(mysql_error());
  17.  
  18. header("Location: theOtherPage.php?message=$message");
  19. exit();
  20.  
Jul 5 '07 #2
gab
5
Thanks alot for taking time to help me :-)

I have been trying to mess a little around with your supplied code, but I'm still nok getting there :-p (yes I am very green at this...)

I think I maybe forgot to mention something before...
I have a table which is supposed to fill the dropdowns (choice_tabel).

Then there is a table (table) which is supposed to be like:
username,choice,choice2
username2,choice,choice2


its should:
-fill the dropdown boxes with different data from DB (can only fill the first dropdown with below code)
-be able to update the DB with the selected choices (or make a new db entry if one's username doesn't have one already)
-when it shows the dropdown boxes, if ones username is already in the table then ones previus choice should be selected in the dropdownbox.

thx again :-)

[PHP]<?php
//index.php

//connecting to DB

$username = 'thisismyname'; //going to be dynamic later

$sql="SELECT choice FROM choice_tabel WHERE something...";

$result=mysql_query($sql);


$options="";
$options2="";

while ($row=mysql_fetch_array($result)) {

$choice=$row["choice"];
$options.="<option VALUE=\"$choice\">".$choice.'</option>';
}

$sql2="SELECT choice FROM choice_tabel WHERE something else...";
$result2=mysql_query($sql2);
while ($row2=mysql_fetch_array($result2)) {

$choice2=$row2["choice"];
$options2.="<option VALUE=\"$choice2\">".$choice2.'</option>';
}
?>


<form action="update.php" method="get">
<SELECT NAME=dropdown1>
<OPTION VALUE=0>choose 1
<?php echo $options ?>
</SELECT>

<SELECT NAME=dropdown2>
<OPTION VALUE=0>choose 2
<?php echo $options2 ?>
</SELECT>
...
<input type="Submit" name="submit" value="submit">
</form>




//update.php

<?php

If(empty($_GET['choice']) && empty($_GET['choice2'])) {
header('Location: index.php?message=Please%20make%20a%20choice');
exit();
}

//$username = ''; // Not sure what goes here.
$choice = intval($_GET['choice']);
$choice2 = intval($_GET['choice2']);

// connecting to DB here.

$sql = "INSERT INTO `tabel` (`username`, `choice`, `choice2`) VALUES('$username', $choice, $choice2)";
if(mysql_query($sql))
$message = 'Success';
else
$message = urlencode(mysql_error());

header("Location: index.php?message=$message");
exit();

?>[/PHP]
Jul 5 '07 #3
pbmods
5,821 Expert 4TB
Heya, gab.

Expand|Select|Wrap|Line Numbers
  1. <SELECT NAME=dropdown1>
  2.  
Expand|Select|Wrap|Line Numbers
  1. $choice = intval($_GET['choice']);
  2.  
You're gonna want these to match:

Expand|Select|Wrap|Line Numbers
  1. <select name="choice">
  2.  
Expand|Select|Wrap|Line Numbers
  1. $choice = intval($_GET['choice']);
  2.  
Jul 5 '07 #4

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

Similar topics

13
by: melih.onvural | last post by:
Group, I'm having a tough time understanding some of the previous posts on this topic so I wanted to write what I've tried and hope that you can help me troubleshoot. I have a dropdown populated...
1
by: Patrick Olurotimi Ige | last post by:
Populating some Data to a DropDown control (after postback)i would like to display the result in a TextBox and if possible be able to EDIT and Update the Data populated.. What i really want to...
2
by: Steve Pierce | last post by:
I am having some issues with a runtime dropdownlist in a datagrid. The issue is that I cannot get ViewState to fill the selected index of a runtime dropdown properly on postback. I do not want to...
2
by: Ian Davies | last post by:
Hello Ive been informed that I need to do the following in javascript. I have two drop down menus in my php script. The items displayed in the second is dependent on which item is choosen from the...
1
by: Mike C | last post by:
I'm new to Web development so I'm doing some bootstrapping and would really appreciate any help. Been grappling with this for days and am utterly confused by all the information on the web. I have...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
0
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
0
by: Bob | last post by:
Hi, Can anyone show me an example of how to populate / update a dropdown ribbon control with new items from VBA. I have created a dropdown on a custom ribbon group with a callback that works...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.