我正在制作一个脚本,用户可以在其中上传他的宠物的图像。我有这个代码:
<?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;
宠物名称来自另一种形式。用户首先必须选择宠物,然后将他重定向到 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> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Upload"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
我怀疑php代码下面的HTML表单有问题,但我不明白是什么。