Reputation: 475
I'm making a script where the user can upload an image of his pet. I have this code:
<?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>
After running this the tabele column named: "animalName" is blank.
When I run this SQL command inside of phpmyadmin:
insert into pics(animalName,username,image) VALUES('Sparky','tester','sparky.jpg')
The tables are ok and the animalName column contains the right value. The pet name is showing, if I do:
echo $petName;
The pet name is comming from another form. The user 1st have to choose the pet and then he's redirected to the uload form. Here is the dropdown you requested:
<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>
I suspect that there is somthing wrong with the HTML form below of the php code, but I can't understand what.
Upvotes: 0
Views: 134
Reputation: 9782
As you said that you pass the $_POST['picup']
from the first submit, just simply modify your second form and put the $_POST['picup']
value in the hidden field. Modify your form like this..
<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>
Hope this will work..
Because when you submit second, that time your first post values not working.
Upvotes: 1
Reputation: 569
Maybe your data type of animalName is not varchar in phpmyadmin check that
Upvotes: -1
Reputation: 21866
In your comment you mention that $petName comes from a select in the form.
However, in the form, the select is missing, so $petName ($_POST['picup']) is empty.
<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>
Upvotes: 2