Jump to content

Updating Too Many Rows


neverforget98
Go to solution Solved by Ch0cu3r,

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
Share on other sites

  • Solution

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: ?");
    }
}
Edited by Ch0cu3r
Link to comment
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: ?");
    }
}

Thanks so much Ch0cu3r! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.