Jump to content

Unable to UPDATE row part 2


9three

Recommended Posts

I'm back again with a similar problem *sigh*  :'(

 

I just tried out a different way of updating a row and it's not working as intended:

 

<?php

$nav_results = mysql_query("SELECT * FROM navigation");

 

while($field = mysql_fetch_array($nav_results))

{

$id = $field['ID']; //For sake of simplicity

echo "<tr><td align='center'><input type='text' name='". $field['Name'] ."' value='". $field['Name'] ."' class='field2' /></td>\n";

echo "<td align='center'><input type='text' name='URL' value='". $field['URL'] ."' class='field2' /></td>\n";

echo "<td align='center'><a href='$id'>Up</a> | <a href='$id'>Down</a></td>\n";

echo "<td align='center'><a href='nav_settings.php?delete='$id'>Delete</a></td></tr>\n";

}

if(!empty($_POST['URL'])) //Update existing link

{

$id = $field['ID']; //For sake of simplicity

$old_link = mysql_real_escape_string($_POST['URL']);

$update = mysql_query("UPDATE navigation SET URL='$old_link' WHERE ID='$id'");

}

else

{

echo 'Unable to update right: ' .mysql_error(). ' ';

}

 

?>

[/php

 

The loop works perfect. When the script gets to the UPDATE part its where the trouble starts. It doesn't update at all(once again). I tried using mysql_error but its not throwing an error out. Although it is throwing "Unable to update:"

 

I need the wisdom again  ???

Link to comment
https://forums.phpfreaks.com/topic/136565-unable-to-update-row-part-2/
Share on other sites

I tried changing it

 

$update = "UPDATE navigation SET URL='$old_link' WHERE ID='$id'";
mysql_query($update) or die(mysql_error());

 

And it doesn't throw any error out. I also checked for my Post variables if they were empty but my POST variables always contain a string. If you don't update the field, the field shows the current data in that row.

Do me a favor and do this:

 

   <?php
   print_r($_POST);
   die();

   $nav_results = mysql_query("SELECT * FROM navigation");

   while($field = mysql_fetch_array($nav_results))
   {   
      $id = $field['ID']; //For sake of simplicity
      echo "<tr><td align='center'><input type='text' name='". $field['Name'] ."' value='". $field['Name'] ."' class='field2' /></td>\n";
      echo "<td align='center'><input type='text' name='URL' value='". $field['URL'] ."' class='field2' /></td>\n";
      echo "<td align='center'><a href='$id'>Up</a> | <a href='$id'>Down</a></td>\n";
      echo "<td align='center'><a href='nav_settings.php?delete='$id'>Delete</a></td></tr>\n";
   }
   if(!empty($_POST['URL'])) //Update existing link
   {
      $id = $field['ID']; //For sake of simplicity
      $old_link = mysql_real_escape_string($_POST['URL']);
      $update = mysql_query("UPDATE navigation SET URL='$old_link' WHERE ID='$id'");
   }
   else
   {
      echo 'Unable to update right: ' .mysql_error(). ' ';
   }

   ?>

 

And paste what that prints out.

Well I actually have 2 forms in one HTML page. Maybe they are in conflict with each other. My 2nd form works perfect, which is to INSERT a new row. But updating it which is my 1st form, does not work at all. Ill try assigning names to them now.

 

Well that didn't help  :-\

 

Form1

 

<form name="edit" method="post" action="nav_settings.php">
<table width="95%" border="0" cellpadding="1" cellspacing="0" align="center">
<tr align="center">
<td>Name</td>
<td>URL</td>
<td>Sort</td>
<td>Delete</td>
</tr>
<tr align="center">
   <?php
   $nav_results = mysql_query("SELECT * FROM navigation");

   while($field = mysql_fetch_array($nav_results))
   {   
      $id = $field['ID']; //For sake of simplicity
      echo "<tr><td align='center'><input type='text' name='". $field['Name'] ."' value='". $field['Name'] ."' class='field2' /></td>\n";
      echo "<td align='center'><input type='text' name='URL' value='". $field['URL'] ."' class='field2' /></td>\n";
      echo "<td align='center'><a href='$id'>Up</a> | <a href='$id'>Down</a></td>\n";
      echo "<td align='center'><a href='nav_settings.php?delete='$id'>Delete</a></td></tr>\n";
   }
   if(!empty($_POST['URL'])) //Update existing link
   {
      $id = $field['ID']; //For sake of simplicity
      $old_link = mysql_real_escape_string($_POST['URL']);
      $update = mysql_query("UPDATE navigation SET URL='$old_link' WHERE ID='$id'");
   }
   ?>
</tr>
</table>
</p>
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr align="right">
<td><p><input type="submit" name="edit" value="Save Settings" class="button" /></p></td>
</tr>
</table></td>
</tr>
</table>
</form>

 

Form2

    <form name="add" method="post" action="nav_settings.php">
<table width="95%" border="0" cellpadding="1" cellspacing="0" align="center">
<tr>
<td>                    
<p class="attention">New Link</p>
</td>
<td align="right">                    
<input type="text" name="link" value="" class="field" />
</td>
</tr>
</table>
</p>
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr align="right">
<td><p><input type="submit" name="edit" value="Add Link" class="button" /></p></td>
</tr>
</table></td>
</tr>
</table>
</form>

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.