1

I need to connect the form to a database that I already have, but it is not connecting and I can't seem to find the problem. I the error that is showing up mostly is the "mysqli" error but I can't find the reason behind it

--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-- This is the code that is in 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);
  }
?>
Bleach101
  • 21
  • 1
  • 5

3 Answers3

-1

First, run only the connect.php file to check if the connection can be established successfully.

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

In order.php I can't find anywhere that you have called the connect.php file. So, the $conn variable is of no use there.

Call it using include or require_once function.

NewBee
  • 394
  • 3
  • 15
-1

edit code in connect.php to be like this

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

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

and in order.php put this code under <?php

require_once 'connect.php';
-1

I've found the problem in the order.php the code is

  <?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;
                        }

                    ?>
Bleach101
  • 21
  • 1
  • 5