PHP错误将表单数据发布到数据库

时间:2017-06-03 09:19:01

标签: php html mysql database

我正在尝试制作广告后表单,将数据添加到数据库中。该页面不断重新加载并要求填写所有详细信息。我似乎无法找到错误,我已经在谷歌和youtube上做了很多搜索,都无济于事。请帮忙!!!

<?php
    session_start();
    include'db.php';
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phoneNumber = $_POST['mobile-num'];
    $photos = $_POST['fileselect'];
    $town = $_POST['location'];
    $category = $_POST['category'];
    $adTitle = $_POST['title'];
    $adDescription = $_POST['description'];

    if(isset($_SESSION['email']))
    {
        if($email != "" && $name != "" && $phoneNumber != "" && $photos != "" && $town != "" && $category != "" && $adTitle !="" && $adDescription != "")
        {
            $name = stripslashes($name);
            $email = stripslashes($email);
            $phoneNumber = stripslashes($phoneNumber);
            $photos = stripslashes($photos);
            $town = stripslashes($town);
            $adTitle = stripslashes($adTitle);
            $category = stripslashes($category);
            $adDescription = stripslashes($adDescription);

            $name = mysqli_real_escape_string($connection,$name);
            $email = mysqli_real_escape_string($connection,$email);
            $phoneNumber = mysqli_real_escape_string($connection,$phoneNumber);
            $photos = mysqli_real_escape_string($connection,$photos);
            $town = mysqli_real_escape_string($connection,$town);
            $adTitle = mysqli_real_escape_string($connection,$adTitle);
            $category = mysqli_real_escape_string($connection,$category);
            $adDescription = mysqli_real_escape_string($connection,$adDescription);
            $imagePath = "images/".basename($_FILES['fileselect']['MAX_FILE_SIZE']);
            $photo = $_FILES['fileselect']['MAX_FILE_SIZE'];
            $date = date("j F Y");



            if(filter_var($email,FILTER_VALIDATE_EMAIL))
            {
                mysqli_query($connection, "SELECT email,ad-title,ad-category,ad-description,Photos,Name,Mobile-Num,Town,date from ads");
                $insertQuery = mysqli_query($connection, "INSERT INTO ads(email,ad-title,ad-category,ad-description,Photos,Name,Mobile-Num,Town,date) 
                                                            VALUES('$email','$adTitle','$category','$adDescription','$photo','$name','$phoneNumber','$town','$date')");
                header("Location: /profile.php");
            }
            else
                $_SESSION['errorMessage'] = "Please check email pattern";
                header("Location: /post-ad.php");
        }
        else
            $_SESSION['errorMessage'] = "Please input all the required details";
            header("Location: /post-ad.php");
    }
    else
        header("Location: /login.php");

?>  

那是PHP代码。 由于我对Stackoverflow不是很好,我在格式化我希望在这里发布的html表单代码时出现问题。我会附上一张图片。 Html form code for the post-ad form

2 个答案:

答案 0 :(得分:1)

不确定为什么要运行SELECT,因为你似乎对它没有任何作用,也没有参数。但INSERT应该是......

$insertQuery = mysqli_query($connection, "INSERT INTO ads(email,`ad-title`,`ad-category`,`ad-description`,`Photos`,`Name`,`Mobile-Num`,`Town`,`date`) 
                                                            VALUES('$email','$adTitle','$category','$adDescription','$photo','$name','$phoneNumber','$town','$date')");

当你的列名用连字符时,它应该包含在后面的标记中,或者我建议(如果不是太晚)删除连字符并改为使用下划线。

您还应该在运行任何SQL时检查错误,并使用它们进行某种处理。

答案 1 :(得分:0)

感谢各位帮忙。很抱歉给你带来压力。我浏览了我的数据库结构,发现一个类型错误的列阻止了sql insert查询。我道歉......