I have problem getting the correct code for inserting the data into database. And I don't know how to upload image onto the database as well. Below is my code for FashionAddResult.php,
$userid=$_SESSION['userid'];
//check for blanks
if(!empty($_POST['fashionname'])) {
$fashionname = $_POST['fashionname'];
} else {
$fashionname = null;
echo '<p><font color="red">Please enter the Fashion Name!</font></p>';
}
if(!empty($_POST['description'])) {
$description = $_POST['description'];
} else {
$description = null;
echo '<p><font color="red">Please enter the Fashion Description!</font></p>';
}
if(!empty($_POST['imagefile'])) {
$imagefile = $_POST['imagefile'];
} else {
$imagefile = null;
echo '<p><font color="red">Please enter the Fashion Image!</font></p>';
}
if($fashionname != null && $description != null && $imagefile != null){
//TODO 1: Connect to forumdb database
$stmt = new mysqli("localhost", "root", null, "fashiondb");
//TODO 2: Prepare the statement to update subject and message in forummessage
$stmt = $mysqli->prepare("insert into fashion(fashionname,description,imagefile) values (?,?,?)");
if (!$stmt = $mysqli->prepare($sql))
{
die('Query failed: (' . $mysqli->errno . ') ' . $mysqli->error);
}
//TODO 3: Bind the values
$stmt->bind_param('sss', $fashionname, $description, $imagefile);
if (!$stmt->bind_param('sss', $fashionname, $description, $imagefile))
{
die('Binding parameters failed: (' . $stmt->errno . ') ' . $stmt->error);
}
//TODO 4: Execute the statement
$result = $stmt->execute();
if (!$stmt->execute())
{
die('Execute failed: (' . $stmt->errno . ') ' . $stmt->error);
}
//TODO 5: If execute is successful, display update successful message
//else display error message
if($result == true && $stmt->affected_rows>0){
echo '<p>Your fashion name has been added!</p>';
}
else{
echo '<p>Fashion information is not adeed!</p>';
//echo "result=$result<br/>row=$stmt->row_affected<br/>";
}
//TODO 6: close the statement
$stmt->close();
//TODO 7: close $mysqli
$mysqli->close();
}
?>
The FashionAdd.php code is below,
<form enctype="multipart/form-data" action="FashionAddResult.php" method="post">
<p>Fashion Name: <input type="text" name="fashionname"></p>
<p>Fashion Description:<br/></p>
<textarea name="description" rows="10" cols="75">
</textarea><br>
Please choose a file: <input name="imagefile" type="file" /><br/>
<br/>
<input type="submit" value="Upload Fashion!" />
</form>
It displays that the image is not uploaded when I have already an image into the the form. Please help me with the error and how to upload the image into the database :) !! thanks!