views:

179

answers:

3

I need to get the value of a field; I think I am along the right lines but not quite sure this is the proper code. The "Delete Movie" button is where I am trying to get the value of that row like so:

value="'.$row['id'].'"

Can you help?

<?php
//connect to database
mysql_connect($mysql_hostname,$mysql_user,$mysql_password);
@mysql_select_db($mysql_database) or die("<b>Unable to connect to specified database</b>");
//query databae
$query = "select * from movielist";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
echo "<table border=1>";
echo "<tr>
    <td>ID</td>
    <td>Type</td>
    <td>Title</td>
     <td>Description</td>
    <td>Imdb URL</td>
    <td>Year</td>
    <td>Genre</td>
    <td>Actions</td>
  </tr>";
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$type=mysql_result($result,$row,"type");
$title=mysql_result($result,$row,"title");
$description=mysql_result($result,$row,"description");
$imdburl=mysql_result($result,$row,"imdburl");
$year=mysql_result($result,$row,"year");
$genre=mysql_result($result,$row,"genre"); ?>

  <tr>
    <td><?php echo $id; ?></td>
     <td><?php echo $type; ?></td>
    <td><?php echo $title; ?></td>
    <td><?php echo $description; ?></td>
    <td><?php echo $imdburl; ?></td>
    <td><?php echo $year; ?></td>
    <td><?php echo $genre; ?></td>
    <td>


<!-- Delete Movie Button -->
<form style="display: inline;" action="delete/" method="post" onsubmit="return movie_delete()">

<input type="hidden" name="moviedeleteid" value="'.$row['id'].'">

<button type="submit" class="tooltip table-button ui-state-default ui-corner-all" title="Delete trunk"><span class="ui-icon ui-icon-trash"></span></button>
</form> 


    </td>
    </tr>
<?php

$row++;
}
echo "</table>";
?>
A: 

Try using an inline echo tag:

<?=$id;?>

This shorthand style is switched off by default, so if it doesn't work, use this instead:

<?php echo $id; ?>
Charlie Somerville
A: 

You have already extracted $id, just use it directly.

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

You cannot use PHP code between HTML tags without first specifying the start of a new piece of PHP code with the tag.

$row is just an integer, but you are trying to use it like an array. You did not assign the content of one row to $row, but just the current row number.

Alternatively

while ($row = mysql_fetch_assoc($result)
{
  echo $row['id'];
}
Extrakun
A: 

Uhm... I would change that a little...

<?php
//connect to database
mysql_connect($mysql_hostname,$mysql_user,$mysql_password);
@mysql_select_db($mysql_database) or die("<b>Unable to connect to specified database</b>");
//query databae
$query = "select * from movielist";
$result = mysql_query($query) or die('Error, insert query failed');
$row=0; // ???
$numrows=mysql_num_rows($result);
echo "<table border=1>";
echo "<tr>
    <td>ID</td>
    <td>Type</td>
    <td>Title</td>
    <td>Description</td>
    <td>Imdb URL</td>
    <td>Year</td>
    <td>Genre</td>
    <td>Actions</td>
  </tr>";
while($row = mysql_fetch_assoc($result))
{
    $id = $row["id"];
    //... and so on ?>

  <tr>
    <td><?= $id; ?></td>
     <!-- and so on -->
    <td>


<!-- Delete Movie Button -->
<form style="display: inline;" action="delete/" method="post" onsubmit="return movie_delete()">

<input type="hidden" name="moviedeleteid" value="<?= $id ?>">

<button type="submit" class="tooltip table-button ui-state-default ui-corner-all" title="Delete trunk"><span class="ui-icon ui-icon-trash"></span></button>
</form> 


    </td>
    </tr>
<?php

$row++;
}
echo "</table>";
?>

I changed:

  1. the way you iterate through the rows in while()
  2. the way you get the field values using associative arrays
  3. the way you insert the id in the rendered HTML using the PHP value tag
Manrico Corazzi