新帖子表单不会写入数据库

时间:2018-09-16 11:11:33

标签: php sql database

我得到了用于添加新帖子的表格,但我无法将其写入数据库。我尽可能地调试了它,当我尝试添加时,我没有得到任何错误,但是我也看不到数据库中存储的帖子。我想念什么吗?代码:

-这是表格

<form method = "post" action = "vpost.php" enctype="multipart/form-data">
                    <label>
                        <h5>Наслов</h5>
                        <input type="text" name="title" placeholder="Enter title" value = "<?php if(isset($_POST['title'])) { echo $post_title; } ?>">
                    </label>
                    <label>
                        <h5>Адреса</h5>
                        <input type="text"  name="address" placeholder="Enter address" value = "<?php if(isset($_POST['address'])) { echo $post_address; } ?>">
                    </label>
                    <hr>
                    <label>
                        <h5>Цена</h5>
                        <input type="number" name="price" placeholder="Enter price" value = "<?php if(isset($_POST['price'])) { echo $post_price; } ?>">
                    </label>
                    <hr>
                    <label>
                        <h5>Тип</h5>
                        <input type="text" name="type" placeholder="Enter type" value = "<?php if(isset($_POST['type'])) { echo $post_type; } ?>">
                    </label>
                </div>
                <hr>
                <div class="user-information-second">
                    <label>
                        <h5>Година на градба</h5>
                        <input type="number" name="year_built" placeholder="Year Built" <?php if(isset($_POST['year_built'])) { echo $post_yearbt; } ?>>
                    </label>
                    <hr>
                    <label>
                        <h5>Паркинг</h5>
                        <input type="text" name="parking" placeholder="parking" value = "<?php if(isset($_POST['parking'])) { echo $post_parking; } ?>">
                    </label>
                    <hr>
                    <label>
                        <h5>Квадратура</h5>
                        <input type="number" name="sqmeter" placeholder="sqmeter" value = "<?php if(isset($_POST['sqmeter'])) { echo $post_sqmeter; } ?>">
                    </label>
                    <br>
                   <hr>
                    <label>
                        <h5>Греење/Ладење</h5>
                        <input type="text" name="heat" placeholder="Heating" value = "<?php if(isset($_POST['heat'])) { echo $post_heat; } ?>">
                    </label>
                    <br>
                   <hr>
                    <label>
                        <h5>Галерија</h5>
                        <div class="file">
                            <input type="file" name="image" enctype="multipart/form-data" placeholder="Upload Image">
                        </div>
                    </label>
                    <br>
                    <hr>
                    <label>
                        <button type="submit" id="submit">Внеси Оглас</button>
                    </label>
                    </form>

-这是验证并写入基本文件

<?php
include('includes/general.php');
if (isset($_POST['title']) && isset($_POST['address']) && isset($_POST['price']) &&
isset($_POST['type']) && isset($_POST['year_built']) && isset($_POST['parking']) &&
isset($_POST['sqmeter']) && isset($_POST['heat']) && isset($_POST['image'])) {
require("GUMP-master/gump.class.php");
$gump = new GUMP();
$_POST = $gump->sanitize($_POST); 

$gump->validation_rules(array(
    'title'    => 'required|max_len,120|min_len,15',
    'address'   => 'required|max_len,100|min_len,3',
    'price' => 'required',
    'type' => 'required',
    'year_built' => 'required',
    'parking' => 'required',
    'sqmeter' => 'required',
    'heat' => 'required',
));
$gump->filter_rules(array(
    'title' => 'trim|sanitize_string',
    'address' => 'trim|sanitize_string',
    ));
$validated_data = $gump->run($_POST);

if($validated_data === false) {
    ?>
    <center><font color="red" > <?php echo $gump->get_readable_errors(true); ?> </font></center>
    <?php 
    $post_title = $_POST['title'];
      $post_address = $_POST['address'];
      $post_price = $_POST['price'];
      $post_type = $_POST['type'];
      $post_yearbt = $_POST['year_built'];
      $post_parking = $_POST['parking'];
      $post_sqmeter = $_POST['sqmeter'];
      $post_heat = $_POST['heat'];
}
else {
    $post_title = $validated_data['title'];
      $post_address = $validated_data['address'];
      $post_price = $validated_data['price'];
      $post_type = $validated_data['type'];
      $post_yearbt = $validated_data['year_built'];
      $post_parking = $validated_data['parking'];
      $post_sqmeter = $validated_data['sqmeter'];
      $post_heat = $validated_data['heat'];
      if (isset($_SESSION['firstname'])) {
        $post_author = $_SESSION['firstname'];
    }
    $post_date = date('Y-m-d');


    $image = $_FILES['image']['name'];
    $ext = $_FILES['image']['type'];
    $validExt = array ("image/gif",  "image/jpeg",  "image/pjpeg", "image/png", "image/jpg");
    if (empty($image)) {
echo "<script>alert('Attach an image');</script>";
    }
    else if ($_FILES['image']['size'] <= 0 || $_FILES['image']['size'] > 1024000 )
    {
echo "<script>alert('Image size is not proper');</script>";
    }
    else if (!in_array($ext, $validExt)){
        echo "<script>alert('Not a valid image');</script>";

    }
    else {
        $folder  = 'postpics/';
        $imgext = strtolower(pathinfo($image, PATHINFO_EXTENSION) );
        $picture = rand(1000 , 1000000) .'.'.$imgext;
        if(move_uploaded_file($_FILES['image']['tmp_name'], $folder.$picture)) {
            $query = "INSERT INTO posts (title,address,price,type,year_built,parking,sqmeter,heat,date,image) VALUES ('$post_title' , '$post_address' , '$post_price' , '$post_type' , '$post_yearbt' , '$post_parking', '$post_sqmeter','$post_heat','$post_date','$picture')";
            $result = mysqli_query($conn , $query) or die(mysqli_error($conn));
            if (mysqli_affected_rows($conn) > 0) {
                echo "<script> alert('Posted!');
                window.location.href='index.php';</script>";
            }
            else {
                "<script> alert('Error while posting..try again');</script>";
            }
        }
    }
}
}
?>

首先,我认为我可能在数据库中缺少一个字段,但是我重新检查并再次创建了该表。如果需要,我将发布我的帖子表和列的图片。

1 个答案:

答案 0 :(得分:1)

我怀疑您的问题是,您正在检查是否已使用$_POST['image']上传了图像文件。这不是PHP处理文件上传的方式-它们存储在$_FILES中,因此存储在isset($_POST['image'] == false中。

这应该有效:

<?php
include('includes/general.php');
if (isset($_POST['title']) && isset($_POST['address']) && isset($_POST['price']) &&
isset($_POST['type']) && isset($_POST['year_built']) && isset($_POST['parking']) &&
isset($_POST['sqmeter']) && isset($_POST['heat']) && isset($_POST['image']) && count($_FILES) > 0) 
{ 
  //Properly sanitise and validate your inputs and do what else you need to do
}

提示:如果您发现PHP文件没有按其应有的方式工作,那么用于验证数据的条件语句很有可能是罪魁祸首。通过将die('OK up to here');放在if块中来尝试调试,以查明代码是否正在执行。

例如,在代码中,如果您使用以下命令进行调试:

<?php
include('includes/general.php');
if (isset($_POST['title']) && isset($_POST['address']) && isset($_POST['price']) &&
isset($_POST['type']) && isset($_POST['year_built']) && isset($_POST['parking']) &&
isset($_POST['sqmeter']) && isset($_POST['heat']) && isset($_POST['image'])) {
    die('OK up to here');
    //Your code
}

然后输出中将不会显示OK up to here消息,并且您会知道条件语句存在问题。