Jump to content

Updating Too Many Rows


neverforget98

Recommended Posts

Hello,

 

On one of the pages of the system I am coding there is a form to update the destination of one of the rows. Every row that has no destination has a form to update the destination. However, when you update the destination it updates it for every row that doesn't have a destination. This is the portion of the code, thanks:

if($destination=='')
		{
			echo "<form method='post'>
					<td width='25%'><center><input type='text' name='destination_save2' placeholder='Destination Location' /><input type='submit' name='submit2' value='Update Call' /></center></td>";
				if(isset($_POST['submit2']))
					{
						$destination_save2=$_POST['destination_save2'];
						
						$result8=mysql_query("UPDATE uc_calls SET destination='$destination_save2' WHERE id=$id");
						if(!$result8)
							{
								die('Failed to update call. Please contact your Technical Analyst with this error:<br />' .mysql_error());
							}
						else
							{
								header("Location: ?");
							}
					}
			echo "</form>";
			}
			else
			{
				echo "<td width='25%'><center>$destination</center></td>";
			}
Link to comment
https://forums.phpfreaks.com/topic/283934-updating-too-many-rows/
Share on other sites

Is this code being ran in a loop?

 

if it is then it'll update every row to the same destination value. You will need to the records id to a hidden form field.

 

You will also want to move the code for updating the record outside of the loop.

 

Code for displaying the destination value or edit form

if($destination=='')
{
    echo "<form method='post'>
            <td width='25%'><center>
                <input type='hidden' name='id' value='$id' />
                <input type='text' name='destination_save2' placeholder='Destination Location' />
                <input type='submit' name='submit2' value='Update Call' /></center>
            </td>";
    echo "</form>";
}
else
{
    echo "<td width='25%'><center>$destination</center></td>";
}

Then outside of the loop you'll have the update code

if(isset($_POST['submit2']))
{
    $id = (int) $_POST['id'];
    $destination_save2 = mysql_real_escape_string($_POST['destination_save2']);
    
    $result8=mysql_query("UPDATE uc_calls SET destination='$destination_save2' WHERE id=$id");
    if(!$result8)
    {
        die('Failed to update call. Please contact your Technical Analyst with this error:<br />' .mysql_error());
    }
    else
    {
        header("Location: ?");
    }
}

 

Is this code being ran in a loop?

 

if it is then it'll update every row to the same destination value. You will need to the records id to a hidden form field.

 

You will also want to move the code for updating the record outside of the loop.

 

Code for displaying the destination value or edit form

if($destination=='')
{
    echo "<form method='post'>
            <td width='25%'><center>
                <input type='hidden' name='id' value='$id' />
                <input type='text' name='destination_save2' placeholder='Destination Location' />
                <input type='submit' name='submit2' value='Update Call' /></center>
            </td>";
    echo "</form>";
}
else
{
    echo "<td width='25%'><center>$destination</center></td>";
}

Then outside of the loop you'll have the update code

if(isset($_POST['submit2']))
{
    $id = (int) $_POST['id'];
    $destination_save2 = mysql_real_escape_string($_POST['destination_save2']);
    
    $result8=mysql_query("UPDATE uc_calls SET destination='$destination_save2' WHERE id=$id");
    if(!$result8)
    {
        die('Failed to update call. Please contact your Technical Analyst with this error:<br />' .mysql_error());
    }
    else
    {
        header("Location: ?");
    }
}

Thanks so much Ch0cu3r! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.