Update mysql row from radio script

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
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Update mysql row from radio script

Post by blade_922 »

Hi there,

Just a brief intro

My Aim: It is to read data from mysql table, from 2 fields in particular called 'esrb' and 'genre'. I have managed to output on an edit page the values of 'esrb' and 'genre'. The 2 fields output to a set of radio buttons.

So ESRB values could be:

Not Rated
Everyone
Teen
Mature

and Genre

Fighting
War
Action
Adventure

These are radio buttons and whatver is in the field in the mysql row, by default the correct radio button is checked. Now if i change this radio button to something else like from Everyone to Teen or from War to Acrtion, i CANT seem to get it to update the values in the mysql table.

Here is the code editgame.php

Code: Select all

<?php

include('db.php');

$id = $_GET['id']; 


$result = mysql_query("SELECT * FROM ccms_gameindex WHERE id = $id");
$row = mysql_fetch_array($result);

$genre=$row['genre'];
$esrb=$row['esrb'];


switch($genre) 
{ 
   case "Shooter": $checkshooter = "checked"; break; 
   case "Racer": $checkracer = "checked"; break; 
   case "Pinball": $checkpinball = "checked"; break; 
   case "Puzzle": $checkpuzzle = "checked"; break; 
   case "Strategy": $checkstrategy = "checked"; break; 
   case "Action": $checkaction = "checked"; break; 
   case "Boardgame": $checkboardgame = "checked"; break; 
   case "Fighting": $checkfighting = "checked"; break; 
   case "Sim": $checksim = "checked"; break; 
   case "RPG": $checkrpg = "checked"; break; 
   case "Sports": $checksports = "checked"; break; 
   case "Adventure": $checkadventure = "checked"; break; 
   case "War": $checkwar = "checked"; break; 
 
} 

switch($esrb) 
{ 
   case "Not Yet Rated": $checknotyetrated = "checked"; break; 
   case "Early Childhood": $checkearlychildhood = "checked"; break; 
   case "Everyone": $checkeveryone = "checked"; break; 
   case "Everyone 10+": $checkeveryone10 = "checked"; break; 
   case "Teen": $checkteen = "checked"; break; 
   case "Mature 17+": $checkmature17 = "checked"; break; 
   case "Adults Only 18+": $checkadultsonly = "checked"; break; 

} 


?>


Game Name: <?php echo $row['title']; ?></br>

<form action="subgame.php" method="post">
<table><tr><td>
Genre: <input type="text" name="genre" value="<?php echo $row['genre']?>" /></br>
</td><td>
ESRB: <input type="text" name="esrb" value="<?php echo $row['esrb']?>"  /></br>
</td></tr>
<tr><td>
<input type="radio" name="genre" Value="Shooter" <?=$checkshooter?> > Shooter </br>
<input type="radio" name="genre" Value="Racer" <?=$checkracer?>>  Racer</br>
<input type="radio" name="genre" Value="Pinball" <?=$checkpinball?>>  Pinball</br>
<input type="radio" name="genre" Value="Puzzle" <?=$checkpuzzle?> > Puzzle</br>
<input type="radio" name="genre" Value="Strategy" <?=$checkstrategy?> > Strategy</br>
<input type="radio" name="genre" Value="Action" <?=$checkaction?> > Action</br>
<input type="radio" name="genre" Value="Boardgame" <?=$checkboardgame?>>  Boardgame</br>
<input type="radio" name="genre" Value="Fighting" <?=$checkfighting?>>  Fighting</br>
<input type="radio" name="genre" Value="Sim" <?=$checksim?> > Sim</br>
<input type="radio" name="genre" Value="RPG" <?=$checkrpg?> > RPG</br>
<input type="radio" name="genre" Value="Sports" <?=$checksports?> > Sports</br>
<input type="radio" name="genre" Value="Adventure" <?=$checkadventure?>>  Adventure</br>
<input type="radio" name="genre" Value="War" <?=$checkwar?>>  War</br>
</td>
<td>

<input type="radio" name="esrb" Value="Not Yet Rated" <?=$checknotyetrated?> > Not Yet Rated</br>
<input type="radio" name="esrb" Value="Early Childhood" <?=$checkearlychildhood?>>  Early Childhood</br>
<input type="radio" name="esrb" Value="Everyone" <?=$checkeveryone?>>  Everyone</br>
<input type="radio" name="esrb" Value="Everyone 10+" <?=$checkeveryone10?>>  Everyone 10+</br>
<input type="radio" name="esrb" Value="Teen" <?=$checkteen?> > Teen</br>
<input type="radio" name="esrb" Value="Mature 17+" <?=$checkmature17?> > Mature 17+</br>
<input type="radio" name="esrb" Value="Adults Only 18+" <?=$checkadultsonly?> > Adults Only 18+</br>

</td></tr></table>
<input type="submit" />
</form>

and subgame.php

Code: Select all

<?php include('db.php'); ?>

<?php
$id=$_POST['id'];
$esrb=$_POST['esrb'];
$genre=$_POST['genre'];

?>

<?php

$sql="UPDATE ccms_gameindex SET esrb='$esrb', genre='$genre', WHERE id ='$id'"
mysql_query($sql);
echo "Record Updated";
mysql_close();




?>
Your help is much appreaciated
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Update mysql row from radio script

Post by blade_922 »

Hey,

Need help with this asap.

Anyone's help is much appreciated. Im pretty sure ive missed something out in the subgames.php page so that it saves to the DB.

Can someone have a quick glance see if im doing something wrong or missed something out?

Kind Regards
Omar Parvaiz
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Update mysql row from radio script

Post by danwguy »

Might be a stupid question but where are you getting the $id variable from? You might want to just echo $id before you run the update part of your command to make sure you are getting the id properly. That's the only thing I can see that would be causing it to not update the sql right. Also are you getting the "record updated" echo after you submit the form?
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Update mysql row from radio script

Post by blade_922 »

Hi,

Thanks for the reply. Yes i am getting the Record Updated echo. But when i check the mysql table it does not change.

In regards to the echo $id do you mean on the subgames.php?

Kind Regards
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Update mysql row from radio script

Post by blade_922 »

Okay, so

on the subgames.php page it echo's out the esrb and the genre but DOES NOT echo out the i.d


Kind Regards
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Update mysql row from radio script

Post by blade_922 »

And to update, i managed to echo the id as well now, so now when i update it, it works. A simple thing as someone telling you to output the form data actually helped me fix my problem on my own. Life is one hella learning curve.

Thanks bud
Kind Regards
Omar Parvaiz
Post Reply