【问题标题】:Form not sending all fields and query not properly working表单未发送所有字段且查询无法正常工作
【发布时间】: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-&gt;bind_param('sss', $fashionname, $description, $imagefile,$userid);,您有 3 个s,而您需要 4 个字母,因为您有 4 个变量。
  • @Prix 在我更改了代码之后,网页本身没有出现错误。但它显示为:添加时尚请输入时尚名称!请输入时尚说明!请输入时尚形象!
  • 因为您的 HTML 中有 2 个不同字段的表单,请将第一行替换为 &lt;form enctype="multipart/form-data" action="FashionAddResult.php" method="post"&gt; 并删除后面的以及中间的 &lt;/form&gt; 否则它只会发送文件
  • @Prix OMG OMG OMG !!!现在可以了!!!!谢谢 :) 但我怎样才能上传图像文件?插入语句是否正确?因为我不确定我在做什么...... T__T
  • 阅读我的第一条评论并检查所有内容是否有错误,并使用正确的错误消息发布您没有正确操作的地方

标签: php sql database insert


【解决方案1】:

您的 TODO 2 缺少“)”

$stmt = $mysqli->prepare("insert into fashion(fashionname,description,imagefile values (?,?,?)where userid=?");

应该是 - 注意图像文件之后的关闭 ):

$stmt = $mysqli->prepare("insert into fashion(fashionname,description,imagefile) values (?,?,?)where userid=?");

【讨论】:

    【解决方案2】:

    您有一个 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();
    }
    

    【讨论】:

    • 插入代码是否正确使用?我对数据库很陌生:(
    • 如我所说,出现错误。 imagefile values (?,?,?) 应该是 imagefile) values (?,?,?)
    • 我的意思是,userid=?,它应该在那里吗?我的时尚数据库有 5 列,其中包括 1)时尚 ID、2)时尚名称、3)描述、4)图像文件、5)用户 ID。我需要在 insert 语句中添加时尚 ID 吗?主键是时尚ID。谢谢
    • 啊,抱歉,不应该有WHERE userid = ?。应该像我的编辑显示的那样。如果是auto increment,则无需声明fashionid
    猜你喜欢
    • 2021-06-11
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多