我在获取将数据插入数据库的正确代码时遇到问题。而且我也不知道如何将图像上传到数据库中。以下是我的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();
}
?>
FashionAdd.php代码如下,
<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>
当我在图像中已有图像时,显示图像未上传。 请帮我解决错误以及如何将图片上传到数据库:) !!谢谢!
答案 0 :(得分:0)
您有SQL错误。
此:
insert into fashion(fashionname,description,imagefile values (?,?,?)where userid=?
应该是:
insert into fashion (fashionname, description, imagefile, userid) values (?,?,?,?)
您还应该绑定4个参数,而不仅仅是3个。
此:
$stmt->bind_param('sss', $fashionname, $description, $imagefile,$userid);
应该是:
$stmt->bind_param('sssi', $fashionname, $description, $imagefile, $userid);
要看到这一点,你应该做的事情如下:
try
{
//TODO 2: Prepare the statement to update subject and message in forummessage
if (!$stmt = $mysqli->prepare("insert into fashion (fashionname,description,imagefile,userid) values (?,?,?,?)"))
{
throw new Exception($mysqli->error);
}
//TODO 3: Bind the values
if (!$stmt->bind_param('sssi', $fashionname, $description, $imagefile,$userid))
{
throw new Exception($stmt->error);
}
//TODO 4: Execute the statement
if (!$result = $stmt->execute())
{
throw new Exception($stmt->error);
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
答案 1 :(得分:0)
您的TODO 2缺少“)”
$stmt = $mysqli->prepare("insert into fashion(fashionname,description,imagefile values (?,?,?)where userid=?");
应该是 - 注意关闭后的imagefile:
$stmt = $mysqli->prepare("insert into fashion(fashionname,description,imagefile) values (?,?,?)where userid=?");