Jump to content

put a value from mysql into select

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
26 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi all, i'm using this code in a registration form to get the values into mysql..Suppose the user want to edit their profile,how do i display the values from the mysql into select..Thanks a lot...This code is for selecting province..


<select name="propinsi">

        <option value="Pilih" selected="selected">Pilih salah satu....</option>

	 <option>Nanggroe Aceh Darussalam</option>

	<option>Sumatera Utara</option>

	<option>Sumatera Barat</option>

	<option>Bengkulu</option>

	<option>Riau</option>

	<option>Kepulauan Riau</option>

	<option>Jambi</option>

	<option>Sumatera Selatan</option>

	<option>Lampung</option>

	<option>Kepulauan Bangka Belitung</option>

	<option>DKI Jakarta</option>

	<option>Jawa Barat</option>

	<option>Banten</option>

	<option>Jawa Tengah</option>

	<option>DI Yogyakarta</option>

	<option>Jawa Timur</option>

	<option>Kalimantan Barat</option>

	<option>Kalimantan Tengah</option>

	<option>Kalimantan Selatan</option>

	<option>Kalimantan Timur</option>

	<option>Bali</option>

	<option>Nusa Tenggara Barat</option>

	<option>Nusa Tenggara Timur</option>

	<option>Sulawesi Barat</option>

	<option>Sulawesi Utara</option>

	<option>Sulawesi Tengah</option>

	<option>Sulawesi Selatan</option>

	<option>Sulawesi Tenggara</option>

	<option>Gorontalo</option>

	<option>Maluku</option>

	<option>Maluku Utara</option>

	<option>Papua Barat</option>

	<option>Papua</option>

        </select>



#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
So you wish to select what the user has chose and then input into a MySQL table?

If so, have you set up a MySQL databse yet? If so, you first need to make a MySQL connection. Also you have to connect to the databse.
$connect = mysql_connect('hostname', 'username', 'password');
$db = mysql_select_db('database_name', $connect);

Once the connection is made you must first retrieve the users input. You must put the above input selection tags into a form with the action of a PHP file. The method must also be post.
<form action="new.php" method="POST">
//Input tags go in here
</form>

Then in the new PHP file, you must add the mysql_connect function and also the following code which will retrieve the users input.
$user_input = $_POST['propinsi'];

Now you have that, you must insert the user information into a table.
$input = "INSERT INTO `table_name` ('column1', 'column2') VALUES ($user_input, 'name');

Any further help just ask.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
For such a list, it's much easier to create an sql table and put your values there, and assign each name with an id number, as number matching is better than string matching. Also, it is safer for eventual spelling errors and stuff.

$userprop = 15;  // in your way, take the users value from the database
// or if it is at registration, set at wished default value
$res = mysql_query("select id, name from propinsi");
echo '<select name="propinsi">';
while ($row = mysql_fetch_array($res)) {
    if ($row['id'] == $userprop) {
        echo '<option value="'.$$row['id'].'" selected="selected">'.$row['name'].'</option>';
    } else {
        echo '<option value="'.$$row['id'].'">'.$row['name'].'</option>';
    }
}
echo '</select>';

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Thanks orjan..I will try the way you taught me..

#5
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Hold on, with what Orjan has said. I am confused on what you want to do?
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#6
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
I'm intending to get the values that user had choosed before and put it into select option..

#7
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Yes sorry, follow Orjan's code. I read your question wrong.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#8
qnstner

qnstner

    Newbie

  • Members
  • PipPip
  • 15 posts

Brandon W said:

So you wish to select what the user has chose and then input into a MySQL table?

If so, have you set up a MySQL databse yet? If so, you first need to make a MySQL connection. Also you have to connect to the databse.
$connect = mysql_connect('hostname', 'username', 'password');
$db = mysql_select_db('database_name', $connect);

Once the connection is made you must first retrieve the users input. You must put the above input selection tags into a form with the action of a PHP file. The method must also be post.
<form action="new.php" method="POST">
//Input tags go in here
</form>

Then in the new PHP file, you must add the mysql_connect function and also the following code which will retrieve the users input.
$user_input = $_POST['propinsi'];

Now you have that, you must insert the user information into a table.
$input = "INSERT INTO `table_name` ('column1', 'column2') VALUES ($user_input, 'name');

Any further help just ask.

Hi Brandon!

Im trying to do something like you described. What i have is a select form where the options comes from a MySQL database. My SQL command is:

SELECT test_num, test_text, test_type FROM test WHERE al='1'

This is working fine.

What i want to do is when i select an option and press a button i would like to send 0 back to al from the selected option to MySQL. The Values of each option is test_num and they are unique.

Do you know if it's possible to accomplish this?

Best regards.

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts

qnstner said:

What i want to do is when i select an option and press a button i would like to send 0 back to al from the selected option to MySQL. The Values of each option is test_num and they are unique.

What do you mean by sending 0 back to all? I don't understand what you want to achieve?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
qnstner

qnstner

    Newbie

  • Members
  • PipPip
  • 15 posts
i want to send the value "0" to the record "al" in MySQL when i have selected an option and pushed a submit button.

I reality it means that i want to acknowledge one alarm.

#11
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
is this depending on what the user chooses in the select, or what?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#12
qnstner

qnstner

    Newbie

  • Members
  • PipPip
  • 15 posts
yes, because every option has it's own "al" record.