简体   繁体   English

没有插入数据库。 PHP和MySQL

[英]Not inserting into database. PHP and mysql

I have to create a form where answers get sent to the database but when I fill in the form the database is not updating and I have getting the self made error :"something went wrong". 我必须创建一个表单,将答案发送到数据库,但是当我填写表单时,数据库没有更新,并且出现了自制错误:“出了点问题”。 Can anyone see anything wrong? 谁能看到任何错误? Thanks. 谢谢。

Form: 形成:

<form id="contact-form" method="post" action="sentEnquiries.php" name="enquiries">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="name">
                            Name</label>
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span>
                            </span>
                            <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required="required" /></div>
                    </div>

                    <div class="form-group">
                        <label for="email">
                            Email Address</label>
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
                            </span>
                            <input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required="required" /></div>
                    </div>

                    <div class="form-group">
                        <label for="phoneNumber">
                            Phone Number</label>
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span>
                            </span>
                            <input type="tel" class="form-control" id="phoneNumber" name="phone" placeholder="Enter phone number" required="required" /></div>
                    </div>

                    <div class="form-group">
                        <label for="partySize">
                            Party Size</label>

                        <input type="number" min="1" max="6" class="form-control" id="partySize" name="partySize" required="required" />
                    </div>




                </div>

                <div class="col-md-6">
                     <div class="form-group">
                        <label for="arrivalDate">
                            Arrival Date</label>

                        <input type="date" class="form-control" id="arrivalDate" name="arrivalDate" />
                    </div>

                    <div class="form-group">
                        <label for="departureDate">
                            Departure Date</label>

                        <input type="date" class="form-control" id="departureDate" name="departureDate"/>

                    </div>
                    <div class="form-group">
                        <label for="name">
                            Message</label>
                        <textarea name="message" id="message" class="form-control" rows="9" cols="25" required="required"
                            placeholder="Message"></textarea>
                    </div>
                </div>
                <div class="col-md-12">
                    <button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
                        Send enquiry</button>
                </div>
            </div>
            </form>

Answers: 答案:

   <?php

include("conn.php");


 $sentName = $_POST['name'];
$sentEmail = $_POST['email'];
$sentPhone = $_POST['phone'];
$sentPartySize = $_POST['partySize'];
$sentArrivalDate = $_POST['arrivalDate'];
$sentDepartureDate = $_POST['departureDate'];
$sentMessage = $_POST['message'];


$insertQuery = "INSERT INTO Enquiries(enquiryID, name, email, phone,   partySize, arrivalDate, departureDate, message) VALUES(NULL, '$sentName', '$sentEmail', '$sentPhone, '$sentPartySize', $sentArrivalDate, '$sentDepartureDate', '$sentMessage')";


?>

Further down answers doc: 进一步的答案文档:

<div class="descriptions">

        <?php
        if(mysqli_query($conn, $insertQuery)) {
            echo "<p>Thank you for your enquiry.</p>";
                mysqli_close($conn);
        } else {
            echo "<p>Something went wrong.</p>";
                mysqli_close($conn);
        }


        ?>




        </div>

could you make sure as below : 你能确定如下:

  1. enquiryID column, is this column is auto increment if not better you set the id become auto increment primary key enquiryID列,如果不是更好,则此列是自动递增的,将id设置为自动递增主键
  2. For debug purposes, can you echo first in $_POST['name'] then exit; 为了调试目的,您可以先在$ _POST ['name']中回显然后退出吗? to make sure that the post data from your html is work correctly. 确保来自html的发布数据正常工作。
  3. if that not work, you should try to select first from database with simple query, so you will know the connection to database table is work correctly 如果那行不通,则应尝试通过简单查询从数据库中首先选择,以便您知道与数据库表的连接正常

Here is how i'd do it. 这是我会做的。

Change yours as required but here's my example: 根据需要更改您的名称,但这是我的示例:

PDO class: PDO类别:

class form
{
    public function sendForm($name, $email, $phone, $party, $date, $depart, $message)
    {
        $stmt = $this->conn->prepare("INSERT INTO `enquiries` (`name`,`email`,`phone`,`partySize`,`arrivalDate`,`departureDate`,`message`) VALUES (:fname, :email, :phone, :party, :arrive, :depart, :msg)");
        $stmt->bindParam(array(':fname' => $name, ':email' => $email, ':phone' => $phone, ':party' => $party, ':arrive' => $date, ':depart' => $depart, ':msg' => $message));
        $stmt->execute();
    }
}

Then within your form page: 然后在您的表单页面中:

$form = new form();

if (isset($_POST['sendIt']))
{
    $form->sendForm($_POST['Fname'],$_POST['email'],$_POST['phone'],$_POST['size'],$_POST['date'],$_POST['depart'],$_POST['message']);
}

Then my basic form without the div tagging so you'll need to tweak slightly: 然后是没有div标签的基本表单,因此您需要进行一些微调:

<form action="" method="post">
    <input type="text" name="Fname">
    <input type="email" name="email">
    <input type="text" name="phone">
    <input type="text" name="size">
    <input type="text" name="date">
    <input type="text" name="depart">
    <textarea name="message" id="" cols="30" rows="10"></textarea>

    <input type="submit" name="sendIt">
</form>

This is a very basic one with no thank you message or conditions but you can easily add conditions before running the query by doing 这是一个非常基本的示例,没有任何感谢信息或条件,但是您可以通过运行以下命令轻松添加条件

if (!empty($var)
{
 // do something
} else {
echo "you didnt fill this in...";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM