Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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: 115
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
 
 
Register now for full access!
Old 10-23-2009, 12:27 AM Re: Php Dropdown value updates Database Row Value
lizciz's Avatar
Webmaster Talker

Posts: 744
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());

__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
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

Posts: 2,781
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
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

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

Posts: 115
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: 115
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
Webmaster Talker

Posts: 744
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>
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366

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: 115
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
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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.27051 seconds with 12 queries