我的帖子没有出现在我的数据库中

时间:2014-05-09 10:28:00

标签: php mysql forms

我有下面的表单和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>";

}


}



}

?>

提前致谢

1 个答案:

答案 0 :(得分:0)

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

下一个问题: 不要插入新记录,其字段为post_name UNIQUE,请尝试使用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)";