【发布时间】:2014-02-12 16:54:13
【问题描述】:
我无法获取将数据插入数据库的正确代码。而且我也不知道如何将图像上传到数据库中。下面是我的 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>
当我已经有一张图片进入表单时,它显示图片没有上传。 请帮助我解决错误以及如何将图像上传到数据库:) !!谢谢!
【问题讨论】:
-
帮助我们帮助您为查询添加错误处理,以便我们可以看到问题出在当前 MySQL 错误的位置,see this link for the MySQLi commands used for that. 此外,您的代码在值之前缺少
)行insert into fashion(fashionname,description,imagefile values (?,?,?)where userid=?。同样在$stmt->bind_param('sss', $fashionname, $description, $imagefile,$userid);,您有 3 个s,而您需要 4 个字母,因为您有 4 个变量。 -
@Prix 在我更改了代码之后,网页本身没有出现错误。但它显示为:添加时尚请输入时尚名称!请输入时尚说明!请输入时尚形象!
-
因为您的 HTML 中有 2 个不同字段的表单,请将第一行替换为
<form enctype="multipart/form-data" action="FashionAddResult.php" method="post">并删除后面的以及中间的</form>否则它只会发送文件 -
@Prix OMG OMG OMG !!!现在可以了!!!!谢谢 :) 但我怎样才能上传图像文件?插入语句是否正确?因为我不确定我在做什么...... T__T
-
阅读我的第一条评论并检查所有内容是否有错误,并使用正确的错误消息发布您没有正确操作的地方