Reply
Php Dropdown value updates Database Row Value
Old 10-22-2009, 08:15 PM Php Dropdown value updates Database Row Value
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
So I edited to fit my style.

PHP Code:
<?php 
include("inc/config.php");
$result mysql_query("SELECT style FROM website"
or die(
mysql_error());  

$row mysql_fetch_row($result);
$col $row[0];

switch(
$col)
{
case 
1: echo "value1 selected";
case 
2: echo "value2 selected"
case 
3: echo "value3 selected"

?>
Now If someone can make a dropdown for me that edits the value on it.

Like this:
PHP Code:
<select>
<
option value="1">First</option>
<
option value="2">second</option>
<
option value="3">third</option>
</
select
and the value that you pic will be updated in the database. So if you pick third and click submit, the value that will be in the database would be 3.

Can someone help me out with that?
sith717 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-23-2009, 12:27 AM Re: Php Dropdown value updates Database Row Value
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Your form
HTML Code:
<form action="...">
<select name="value">
<option value="1">First</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
<input type="submit" name="submit">
</form>
And some php code to handle it
PHP Code:
$allowed = array(123);
$default 1;
if (isset(
$_POST['submit'])) {
   
$value in_array($_POST['value'], $allowed) ? $_POST['value'] : $default;
   
$query "UPDATE your_table SET style='$value' LIMIT 1";
   
mysql_query($query) or die(mysql_error());

__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-23-2009, 01:05 AM Re: Php Dropdown value updates Database Row Value
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Easy PHP Search with Opera
Posts: 1,069
Name: Matt
Location: Irvine, CA
Trades: 0
Also, the syntax for your switch statement is incorrect:
PHP Code:
switch($col)
{
case 
1: echo "value1 selected";
case 
2: echo "value2 selected"
case 
3: echo "value3 selected"

should be

PHP Code:
switch($col)
{
     case 
1
          echo 
"value1 selected";
          break;
     case 
2
          echo 
"value2 selected"
          break;
     case 
3
          echo 
"value3 selected";
          break;

If you leave out the break, for any given case, all of the cases below it will be executed as well. For example, with your code if col were 2 both "value2 selected" and "value3 selected" would be echo'd
__________________
Tinsology | How to Post Code | EverythingDev

Last edited by NullPointer; 10-23-2009 at 01:07 AM..
NullPointer is online now
Reply With Quote
View Public Profile
 
Old 10-23-2009, 11:55 AM Re: Php Dropdown value updates Database Row Value
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Thanks for showing me my errors and will get back to you if whatyou have given me works.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 10-24-2009, 11:58 AM Re: Php Dropdown value updates Database Row Value
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Thanks, it works perfectly!

Thanks so much, also, how would I do it so the drop down automatically selects the value once the page is loaded?

Do you understand what I mean?
sith717 is offline
Reply With Quote
View Public Profile
 
Old 10-24-2009, 12:24 PM Re: Php Dropdown value updates Database Row Value
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Something like

PHP Code:
<?php
// select the value from the database
// $value = ...

?>
<form action="...">
<option value="1"<?php if ($value == 1) { echo ' selected="selected"'; }  ?>>First</option>
<option value="2"<?php if ($value == 2) { echo ' selected="selected"'; }  ?>>Seccond</option>
<option value="3"<?php if ($value == 3) { echo ' selected="selected"'; }  ?>>Third</option>
<input type="submit">
</form>
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f

Last edited by lizciz; 10-24-2009 at 12:27 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-24-2009, 02:44 PM Re: Php Dropdown value updates Database Row Value
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
I get these errors:

Notice: Undefined variable: value in /home/sith717/public_html/option.php on line 37

What does that mean?
sith717 is offline
Reply With Quote
View Public Profile
 
Old 10-24-2009, 03:06 PM Re: Php Dropdown value updates Database Row Value
chrishirst's Avatar
Super Moderator

Posts: 22,260
Location: Blackpool. UK
Trades: 0
it means you haven't done this bit

select the value from the database
$value = ...

to define the variable and give it a value
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Php Dropdown value updates Database Row Value
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.13699 seconds with 13 queries