将HTML表单连接到数据库

时间:2018-05-19 14:48:34

标签: php database connection

我需要将表单连接到我已经拥有的数据库,但它没有连接,我似乎无法找到问题。我出现的错误主要是" mysqli"错误,但我无法找到背后的原因

- order.php -

  
<!DOCTYPE html>
<html>
    <head>
        <title>Checkout</title>
        <link rel = "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
        <link rel="stylesheet" href="styles.css"/>
    </head>
    <body>
        <div class="col-sm-5 clearfix">
            <div class="bill-to">
                <h2>Bill To</h2>
                    <div class="form-one">
                    <form action="order.php" method="POST">
                    <input type="text" id="FirstName" name="FirstName" placeholder="First Name" style="width:350px; background-color:pink;"><br>
                    <input type="text" id="Surname" name="Surname" placeholder="Last Name" style="width:350px;background-color:pink;"><br>
                    <input type="text" id="HouseName" name="HouseName" placeholder="House Name" style="width:350px;background-color:pink;"><br>
                    <input type="text" id="StreetName" name="StreetName" placeholder="Street Name" style="width:350px;background-color:pink;"><br>
                    <input type="text" id="Locality" name="Locality" placeholder="Locality" style="width:350px;background-color:pink;"><br>
                    <input type="text" id="MobileNumber" name="MobileNumber" placeholder="Mobile Number" style="width:350px;background-color:pink;"><br>
                    <input type="text" id="Email" name="Email" placeholder="Email Address" style="width:350px;background-color:pink;">
                    <input type="submit" name="submit" value="Register"/>
                    </form> 


                    <?php

                        if(isset($_POST['submit']))
                        {
                        $FirstName=$_POST['FirstName']; 
                        $Surname=$_POST['Surname'];

                        $result="INSERT INTO users (FirstName,Surname) VALUES ('$FirstName','$Surname')";
                         mysqli_query($conn,$result);
                        }
                    ?>
                   </div>
            </div>
        </div>

    </body>
</html>

- connect.php--这是connect.php中的代码

<?php
    $user = 'root';
    $pass ='';
    $host = 'localhost';
    $db = 'webassignment';

    $conn = new mysqli($host, $user, $pass, $db);

  if ($conn -> connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
?>

3 个答案:

答案 0 :(得分:0)

首先,仅运行connect.php文件以检查是否可以成功建立连接。

if ($conn -> connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
else
    echo 'Success';

order.php我找不到您调用connect.php文件的任何地方。因此,$conn变量在那里没用。

使用includerequire_once功能调用它。

答案 1 :(得分:0)

connect.php 中的代码编辑为此类

$conn = mysqli_connect($host, $user, $pass, $db);

if(mysql_connect_errno()){
echo "can't connect" . mysqli_connect_error();
}

并在 order.php 中将此代码放在<?php

require_once 'connect.php';

答案 2 :(得分:0)

我在order.php中发现了代码

的问题
  <?php
                        $conn = new mysqli("localhost","root",'','webassignment',3306);

                       if (!$conn)
                        {
                           die('Could not connect: ' . mysql_error());
                        }

                        if(isset($_POST['submit']))
                        {
                        $FirstName=$_POST['FirstName']; 
                        $Surname=$_POST['Surname']; 
                        $HouseName=$_POST['HouseName']; 
                        $StreetName=$_POST['StreetName']; 
                        $Locality=$_POST['Locality']; 
                        $MobileNumber=$_POST['MobileNumber'];
                        $Email=$_POST['Email'];
                        }

                        $result="INSERT INTO users (FirstName,Surname,HouseName,StreetName,Locality,MobileNumber,Email) VALUES ('$FirstName','$Surname', '$HouseName','$StreetName','$Locality','$MobileNumber','$Email')";

                        if($conn->query($result))
                        {
                            //echo "worked";
                        }else{
                            //echo "Error: " .$result ."<br>". $conn->error;
                        }

                    ?>