Update inside if/else if/else statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sam Fuller
Forum Newbie
Posts: 2
Joined: Sat Feb 06, 2010 5:07 pm

Update inside if/else if/else statement?

Post by Sam Fuller »

Hello,

I'm working on a small homework assignment which requires selecting a radio button and sending a name from a text field in a html form to a database. I need said name to be put inside a specific row and field in my database depending on which radio button was selected.

I have a series of if/else statements(right word? I'm new to PHP) which detect which radio button was pressed, as well as sending the "name" to the database using INSERT TO, but it just queues up in the order put the name(s) down in. I read about the UPDATE statement, but I do not know how to write the syntax correctly inside my If/Else If statement.

Can anyone provide some help? I'm brand new to PHP and have until Monday to get this working. I feel like i'm close but I'm not sure how to tackle writing the syntax.

Here is the code I have so far.

Code: Select all

 
<?php
 
 
 
//Start connecting to database
$con=mysql_connect("localhost","Sam","root");
if (!$con)
    {
    die('could not connect:'. mysql_error());
    }
//End connecting to database
mysql_select_db("week5", $con);
 
//Update field depending on which radio button was checked
 
$photographer = $_POST['photographer'];
$updatefield = $_POST['radiobutton'];
 
 
 
    if ($updatefield == "feb11th"){
        
        echo $_POST["photographer"];
        echo ',<br/>';
        echo 'You have been marked down for the 11th, good luck shooting!';
        update photographerdatabase set photographer='$_POST["photographer"]' where id=1;
        
    }
    
    
    
    else if ($updatefield == "feb16th"){
        
        echo $_POST["photographer"];
        echo ',<br/>';
        echo 'You have been marked down for the 16th, good luck shooting!';
        
    }
    
    else if ($updatefield == "feb23rd"){
        
        echo $_POST["photographer"];
        echo ',<br/>';
        echo 'You have been marked down for the 23rd, good luck shooting!';
        
    }
    
    else if ($updatefield == "feb18th"){
        
        echo $_POST["photographer"];
        echo ',<br/>';
        echo 'You have been marked down for the 18th, good luck shooting!';
        
    }
//End updating field
 
 
 
$sql="INSERT INTO photographerdatabase (photographer)
VALUES
('$_POST[photographer]')";
 
if(!mysql_query($sql,$con))
    {
    die('Error: '.mysql_error());
    }
 
echo "<br>";
echo "<a href='display.php'>Back to Table</a>";
 
 
 
mysql_close($con);
 
?>
 
Thanks for reading.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Update inside if/else if/else statement?

Post by JakeJ »

Please go read a tutorial on proper structure of a mysql statement in PHP.

But for starters:

Code: Select all

mysql_query("UPDATE table SET name = "Fred" WHERE name = "Joe");
Sam Fuller
Forum Newbie
Posts: 2
Joined: Sat Feb 06, 2010 5:07 pm

Re: Update inside if/else if/else statement?

Post by Sam Fuller »

I have been looking for tutorials, but I found nothing about the right syntax, sorry.

Thank you for your help, that was enough for me to figure it out. : )
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Update inside if/else if/else statement?

Post by JakeJ »

I'm glad I could help!
Post Reply