【问题标题】:My posts do not appear in my database我的帖子没有出现在我的数据库中
【发布时间】:2014-05-09 10:28:00
【问题描述】:

我有下面的表单和 php 代码,但我的帖子没有出现在我的数据库中,这让我很困惑,因为我不知道问题出在哪里。请帮忙。提前致谢

表单代码是

<html>
<head>
    <title>Insert New Product</title>
<link rel ="stylesheet" href="admin_style.css" />
</head>
<body>
    <form method="post" action="" enctype="multipart/form-data">
        <table width="600" align="center" border="10">

            <tr>
                <td align="center" bgcolor="yellow" colspan="6">    <h1>Insert New Product Here</h1></td>
            </tr>

            <tr>
                <td align="right">Product Name</td>
                <td><input type="text" name="name" size="30"></td>
            </tr>
            <tr>
                <td align="right">Product Price</td>
                <td><input type="text" name="price" size="30"></td>
            </tr>
            <tr>
                <td align="right">Product Main Image</td>
                <td><input type="file" name="mainimage"></td>
            </tr>
            <tr>
                <td align="right">Product Sub Image 1</td>
                <td><input type="file" name="subimage1"></td>
            </tr>
            <tr>
                <td align="right">Product Sub Image 2</td>
                <td><input type="file" name="subimage2"></td>
            </tr>
            <tr>
                <td align="right">Product Sub Image 3</td>
                <td><input type="file" name="subimage3"></td>
            </tr>
            <tr>
                <td align="center" colspan="6"><input type="submit"     name="submit" value="Post Product Now!"></td>
            </tr>
        </table>
    </form>
</body>
</html>
<?php
include("includes/connect.php");

if(isset($_POST['submit'])) {

$post_name = $_POST['name'];
$post_price = $_POST['price'];
$post_mainimage = $_FILES['mainimage']['name'];
$post_subimage1 = $_FILES['subimage1']['name'];
$post_subimage2 = $_FILES['subimage2']['name'];
$post_subimage3 = $_FILES['subimage3']['name'];
$mainimage_tmp = $_FILES['mainimage']['tmp_name'];
$subimage1_tmp = $_FILES['subimage1']['tmp_name'];
$subimage2_tmp = $_FILES['subimage2']['tmp_name'];
$subimage3_tmp = $_FILES['subimage3']['tmp_name'];


if($post_name=='' or $post_price=='' or $post_mainimage=='' or
$post_subimage1=='' or $post_subimage2=='' or $post_subimage3==''){


echo "<script>alert('any of the fields is empty')</script>";

exit();

}
else {

move_uploaded_file($mainimage_tmp,"images/$post_mainimage");
move_uploaded_file($subimage1_tmp,"images/$post_subimage1");
move_uploaded_file($subimage2_tmp,"images/$post_subimage2");
move_uploaded_file($subimage3_tmp,"images/$post_subimage3");

$insert_query = "insert into posts 
(post_name,post_price,post_mainimage,post_suimage1,
post_subimage2,post_image3) values ('$post_name','$post_price','
$post_mainimage','$post_subimage1','$post_subimage2','$post_subimage3')";

if(mysql_query($insert_query)){

echo "<center><h1>POST SUCCESSFUL!</h1></center>";

}


}



}

?>

提前致谢

【问题讨论】:

  • connect.php 中有什么?
  • 你能描述得更详细一点吗?您是否收到“发布成功”消息?是不是在这之前就失败了?你有任何错误吗?
  • 检查这个 mysql_query($insert_query) 或 mysql_error() 你会发现查询是否正在运行
  • @user3619935 将error_reporting(E_ALL);ini_set('display_errors', 1);放在PHP文件开头打印错误。
  • "connect.php" 是用来连接数据库的,我每次发帖都没有收到任何错误信息,也没有收到"POST SUCCESSFUL"

标签: php mysql forms


【解决方案1】:

解决了最初的问题,包括表中的PRIMARY KEYAUTO_INCREMENT 字段id

下一个问题: 不插入新记录且字段为post_nameUNIQUE,尝试使用ON DUPLICATE KEY UPDATE

$insert_query = "insert into posts 
(post_name,post_price,post_mainimage,post_suimage1,
post_subimage2,post_image3) values ('$post_name','$post_price','
$post_mainimage','$post_subimage1','$post_subimage2','$post_subimage3') 
ON DUPLICATE KEY UPDATE post_price = VALUES(post_price), post_mainimage = VALUES(post_mainimage), 
post_suimage1 = VALUES(post_suimage1), post_suimage2 = VALUES(post_suimage2), post_suimage3 = VALUES(post_suimage3)";

【讨论】:

  • 对不起各位,来晚了,问题已经解决了,非常感谢@LucasHenrique
猜你喜欢
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多