Michael
Michael

Reputation: 49

Displaying a MySQL Record

I have a form on a page that posts a record id to a page I want to display that record on. The form is:

<form method="post" action="update.php">
<input type="hidden" name="sel_record" value="$id">
<input type="submit" name="update" value="Update this Order">    
</form>

I have tested to see if $id is getting the correct value and it does. When it post to update.php it does not return any values. Any ideas? here is the update page code:

$sel_record = $_POST['sel_record'];

$result = mysql_query("SELECT * FROM `order` WHERE `id` = '$sel_record'") or die     (mysql_error());
    if (!$result) {
    print "Something has gone wrong!";
    } else {
    while ($record = mysql_fetch_array($result)) {
        $id = $record['id'];
        $firstName = $record['firstName'];
        $lastName = $record['lastName'];
        $division = $record['division'];
        $phone = $record['phone'];
        $email = $record['email'];
        $itemType = $record['itemType'];
        $job = $record['jobDescription'];
        $uploads = $record['file'];
        $dateNeeded = $record['dateNeeded'];
        $quantity = $record['quantity'];
        $orderNumber = $record['orderNumber'];
    }
    }

Upvotes: 0

Views: 82

Answers (3)

calcrisk33
calcrisk33

Reputation: 587

You have to escape the string... and you can drop the single quotes around order and id.

Try:

$result = mysql_query("SELECT * FROM order WHERE id = '" . $sel_record . "'")

if $sel_record is a String, otherwise remove the single quotes:

...WHERE id = " . $sel_record)

You can also use functions sprintf and mysql_real_escape_string to format:

$query = sprintf("SELECT * FROM order WHERE id = '%s'", mysql_real_escape_string($sel_record));

Upvotes: 0

mpriscella
mpriscella

Reputation: 381

You should also try to define those variables outside of the while loop.

$id = '';

$result = mysql_query("SELECT * FROM `order` WHERE `id` = '$sel_record'") or die     (mysql_error());
if (!$result) {
  print "Something has gone wrong!";
} else {
  while ($record = mysql_fetch_array($result)) {
    $id = $record['id'];
  }
}

Not a full example, but you get the idea.

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

you have not put the php tags <?php ?> inside the html

<input type="hidden" name="sel_record" value="<?php echo $id; ?>">

Upvotes: 0

Related Questions