Updating a records in a mysql database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Wardy7
Forum Commoner
Posts: 38
Joined: Wed Aug 24, 2005 4:45 am
Location: UK

Updating a records in a mysql database

Post by Wardy7 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys, I have a bti of a problem and was hoping for a bit of help if possible (well and you can understand my problem).  
I have a page called "friends.php" that lists the name of users who have tried to add you to their friends list.
Before the people are added the user has to either accept or decline them by selecting the relevant radio button (radio buttom to accept has a value of 2 and button to decline has a value of 3). The radio button names are the "frienduserid".
A user could have a number of frieds to add or decline all in 1 go but I am for some reason having trouble getting this to work despite knowing it should be relatively simple 

friends.php

Code: Select all

echo("<form method=\"post\" action=\"manage-friends.php\">");
    echo("<table>"); 
      
    while($row = mysql_fetch_array($result)){ 
        if ($bgcolor == "#FFFFFF"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#FFFFFF"; 
     } 
    echo("<tr bgcolor=".$bgcolor.">\n<td>");
    echo("<td width=".$tdwidth." bgcolor=".$bgcolor2.">");
    echo("<font face=".$font.">");
    echo('<a href="../poker/user.php?username=' . $row["friendusername"] . '">' . $row["friendusername"] . '</a>');
    echo("<td width=".$tdwidth." bgcolor=".$bgcolor3.">");
    echo('<input type="radio" name="' . $row["frienduserid"] . '" value="2">');
    echo("<td width=".$tdwidth." bgcolor=".$bgcolor4.">");
    echo('<input type="radio" name="' . $row["frienduserid"] . '" value="3">');
    } 
	echo "<br>";
	echo "<input type=\"submit\" name=\"Submit\" value=\"SAVE\"/>";
    echo("</table>");
	echo ("</form>");
manage-friends.php

Code: Select all

$userid = intval($_SESSION['userid']);  // get it from the session and make sure its an integer

// now let's save the information

$result = mysql_query("UPDATE friends SET verified='$frienduserid' WHERE userid='$userid' AND frienduserid='$frienduserid2'");
if($result == 0) die("Error Updating Friends. MySQL said: " . mysql_error());

echo("New information saved");
What happens is that if I have more than 1 "friend" showing to be reviewed only 1 of them get's changed even when a radio button for each has been selected and also it does not update them with the value of the redio button, it just makes the field empty.

Is anyone able to help, I hope I've made it so you can understand but appreciate that it can be a bit confusing when I try to explain some things :roll:

Cheers
Wardy


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to change your radio buttons to arrays instead of string names so that when they are passed, they are passed as an array. Otherwise you will only get the last field value out of however many you have.

You are looking for something along the lines of...

Code: Select all

<?php
foreach ($array as $key => $value)
{
    echo '<input type="radio" name="fieldname[]" value="' . $key . '" />' . $value . '&nbsp;';
}
?>
Then, when you reference it in the $_POST array, you would need to treat it as an array...

Code: Select all

<?php
$radio_array = $_POST['fieldname'];

foreach ($radio_array as $val)
{
    // Blah blah blah
}
?>
Wardy7
Forum Commoner
Posts: 38
Joined: Wed Aug 24, 2005 4:45 am
Location: UK

Post by Wardy7 »

Thanks for the reply Everah. :)
Can anyone help putting this together as I'm still struggling despite Everah giving me the answer :oops:

Cheers
Wardy
Post Reply