查询未插入值

时间:2012-09-08 08:49:52

标签: php mysql phpmyadmin

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

<?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 ”的tabele列为 空白

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

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

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

echo $petName;

宠物名称来自另一种形式。用户1st必须选择宠物,然后他被重定向到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表单有问题,但我无法理解。

3 个答案:

答案 0 :(得分:2)

在你的评论中,你提到$ petName来自表格中的一个选择。

但是,在表单中,select缺失,所以$ petName($ _POST ['picup'])为空。

    <html >
      <head>
        <title>Image Upload</title>
      </head>
      <body>
        <select name="picup">
          <option>Sparky</option>
          <option>Lucky</option>
        </select>
        <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>

答案 1 :(得分:1)

正如您所说,您从第一次提交中传递了$_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>

希望这会有效..

因为当您提交第二个时,您的第一个帖子值不起作用。

答案 2 :(得分:-1)

也许你的数据类型animalName不是phpmyadmin中的varchar检查