我正在编写一个脚本,用户可以在其中上传他的宠物的图像。我有以下代码:

<?php
    session_start();
    $name = $_SESSION['myusername'];
    $petName = $_POST['picup']; // a pet name picked by a dropdown list

    $con = mysql_connect( "localhost", "lalala", "blabla" );
    mysql_select_db( "lalal_animal", $con );
    if ( @$_POST ['submit'] ) {
      $file = $_FILES ['file'];
      $name1 = $file ['name'];
      $type = $file ['type'];
      $size = $file ['size'];
      $tmppath = $file ['tmp_name'];
      if ( $name1 != "" ) {
        if ( move_uploaded_file( $tmppath, 'upload/' . $name1 ) ) {
          $query = "insert into pics(animalName,username,image) VALUES('$petName','$name','$name1')";
          mysql_query( $query ) or die( 'could not updated:' . mysql_error() );
          echo "Your image upload successfully !!";
        }
      }
    }
    ?>

    <html >
      <head>
        <title>Image Upload</title>
      </head>
      <body>
        <form name="form" action="" method="post" enctype="multipart/form-data">
          Photo <input type="file" name="file" />
          <input type="submit" name="submit" value="submit" />
        </form>
      </body>
    </html>


运行此命令后,名称为“ animalName”的表列为空。

当我在phpmyadmin中运行此SQL命令时:

insert into pics(animalName,username,image) VALUES('Sparky','tester','sparky.jpg')


表格正常,animalName列包含正确的值。
显示宠物名称,如果我这样做:

echo $petName;


宠物的名字来自另一种形式。用户1首先必须选择宠物,然后将其重定向到uload表单。
这是您要求的下拉菜单:

<table width="480" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="upload" method="post" action="upload_form.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Upload image</strong></td>
</tr>
<td>Select a pet</td>
<td>:</td>
<td>

<?php
mysql_connect('localhost', 'blabla', 'lalal');
mysql_select_db('lalala_animal');

$sql = "SELECT name FROM animal where username='$name'";
$result = mysql_query($sql);

echo "<select name='picup'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Upload"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>


我怀疑php代码下面的HTML表单有什么问题,但是我不明白是什么。

最佳答案

正如您说的那样,您是从第一个提交中传递$_POST['picup']的,只需简单地修改第二个表单并将$_POST['picup']值放在隐藏字段中即可。像这样修改您的表单。

<form name="form" action="" method="post" enctype="multipart/form-data">
      Photo <input type="file" name="file" />
      <input type="hidden" name="picup" value="<?php echo $petName; ?>" />
      <input type="submit" name="submit" value="submit" />
    </form>


希望这会工作..

因为当您第二次提交时,那一次您的第一篇文章值不起作用。

08-26 15:16