Page 1 of 1

Update inside if/else if/else statement?

Posted: Sat Feb 06, 2010 5:18 pm
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.

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

Posted: Sat Feb 06, 2010 9:29 pm
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");

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

Posted: Sun Feb 07, 2010 12:02 pm
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. : )

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

Posted: Mon Feb 08, 2010 10:42 am
by JakeJ
I'm glad I could help!