开发者

How to get the value of a field in PHP?

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 t开发者_如何转开发o 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>";
?>


You are using it like

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

but the question here is: how can PHP know that you are referring to a variable instead of pure HTML? PHP provide you with a very handy tool to make it explicit that you want to execute PHP code within HTML (actually pure PHP code don't exists as any .php file can contain also HTML): the <?php and ?> tags.

I'm pretty sure you know what I'm talking about because you have already used them correctly. Therefore just change the above line with:

<input type="hidden" name="moviedeleteid" value="<?php echo $row['id']; ?>">
//                                               ^^^^^ ^^^^           ^ ^^

What you were using was string concatenation which works exactly like that (with . to concatenate strings) but in your case was put in a HTML context.


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'];
}


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; ?>


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜