Problem passing id to another page then update value

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
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Problem passing id to another page then update value

Post by cjkeane »

i'm having difficulty passing the id value from one page to another, then being able to update the value.

PAGE 1 LISTS CATEGORIES. it highlights the category with a link which takes it to page 2.

Code: Select all

<p>Category Name:<br />
<?php
error_reporting (E_ALL ^ E_NOTICE);
$dbcnx = @mysql_connect('localhost', 'root', 'onlyiknow');
if (!$dbcnx) {
  exit('<p>Unable to connect to the database server at this time.</p>');
}
if (!@mysql_select_db('db')) {
  exit('<p>Unable to locate the database at this time.</p>');
}
$result = mysql_query("SELECT * FROM currencies");
$num_rows = mysql_num_rows($result);
echo "There are $num_rows currency types\n";
$cats = @mysql_query('SELECT currencyid, categoryname FROM currencies');
if (!$cats) {
  exit('<p>Error retrieving currency types from the database!<br />'.
      'Error: ' . mysql_error(). '</p>');
}
while ($cat = mysql_fetch_array($cats)) {
  $id = $cat['currencyid'];
  $name = htmlspecialchars($cat['categoryname']);
  echo "<li><a href='editcurrency.php?id=$id'>$name</a>";
}
?>
</ul><form>
<input type="hidden" value="<?php echo $id; ?>" /> 
</form>
==================================================

PAGE 2 displays the correct currency type in a textbox, but then gives me an error that id isnt defined.

Code: Select all

<?php
$dbcnx = @mysql_connect('localhost', 'root', 'onlyiknow');
if (!$dbcnx) {
  exit('<p>Unable to connect to the ' .
      'database server at this time.</p>');
}

if (!@mysql_select_db('db')) {
  exit('<p>Unable to locate the ' .
      'database at this time.</p>');
}

if (isset($_POST['name'])):
  // The details have been updated.

  $name = $_POST['name'];
  $getid = $_POST['id']; 
  $sql = "UPDATE author SET
          CategoryName='$name',
          WHERE categoryid='$id'";
  if (@mysql_query($sql)) {
    echo '<p>Details updated.</p>';
  } else {
    echo '<p>Error updating details: ' .
        mysql_error() . '</p>';
  }

?>

<p><a href="currencies.php">Return to currencies list</a></p>

<?php
else: // Allow the user to edit the currency

  $id = $_GET['id'];
  $category = @mysql_query(
      "SELECT CategoryName FROM currencies WHERE CurrencyID='$id'");
  if (!$category) {
    exit('<p>Error fetching currency details: ' .
        mysql_error() . '</p>');
  }

  $category = mysql_fetch_array($category);
  $name = $category['CategoryName'];
  // Convert special characters for safe use
  // as HTML attributes.
  $name = htmlspecialchars($name);

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Edit the Currency:</p>
<label>Name: <input name="Name" type="text" id="Name" value="<?php echo $name; ?>" /></label><br /><br />
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="SUBMIT" /></p>
</form>

<?php endif; ?>
I'd appreciate any assistance you can provide. i think there's a simple solution, but for the life of me i'm stumped.
Thanks.
Last edited by califdon on Fri Feb 11, 2011 12:11 am, edited 1 time in total.
Reason: Moderator added syntax=php tags to make code readable. Note to poster, please always do this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem passing id to another page then update value

Post by califdon »

You haven't told us what is going wrong, only that you're having difficulty. Do you get an error? Do you see the id in the URL? Have you made any attempt to debug the script? You need to tell us what the symptoms are. In looking at your code, I don't see anything wrong.
Post Reply