-2

DB and Table is created but the form values are not going into the database table Form field values are absolutely right i checked them by echo

Is there any mistake in my insert into sql command. Code is as following

<html>
<head>
    <title></title>
</head>
<body>

<?php 
    $servername="localhost";
    $username="root";
    $password="";
    $conn = new mysqli($servername, $username, $password);
    if ($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    } 
    $conn->query("CREATE DATABASE IF NOT EXISTS `B2B`");
    $sql1="CREATE TABLE IF NOT EXISTS `B2B`.`company_details`(
                `email` VARCHAR(30) NOT NULL,
                `password` VARCHAR(20) NOT NULL,
                `company_name` VARCHAR(70) NOT NULL,
                `address` VARCHAR(150) NOT NULL,
                `website` VARCHAR(70),
                `phone` VARCHAR(20),
                `mobile` VARCHAR(20) NOT NULL,
                `fax` VARCHAR(20),
                `contact_person` VARCHAR(30) NOT NULL,
                `deals_in` VARCHAR(300),
                `Introduction` VARCHAR(400),
                PRIMARY KEY (email));";
        if($conn->query($sql1))
        {
            echo 'table is created succssfully';
        }
        function test_data($data)
        {
            $data=trim($data);
            $data=stripslashes($data);
            $data=htmlspecialchars($data);
            return $data;
        }
        $errors = array();
        if ( $_SERVER["REQUEST_METHOD"] =="POST" )
        {
            $email=test_data($_POST["email"]);
            $password=test_data($_POST["password"]);
            $companyName=test_data($_POST["companyName"]);
            $introduction=test_data($_POST["introduction"]);
            $deals_in=test_data($_POST["deals_in"]);
            $address=test_data($_POST["address"]);
            $website=test_data($_POST["website"]);
            $phone=test_data($_POST["phone"]);
            $mobile=test_data($_POST["mobile"]);
            $fax=test_data($_POST["fax"]);
            $contact_person=test_data($_POST["contact_person"]);
            $sql="INSERT INTO `company_details` (`company_name`, `address`, `email`, `mobile`, `contact_person`, `password`, `website`, `phone`, `fax`, `introduction`, `deals_in` ) VALUES ( '".$companyName."', '".$address."', '".$email."', '".$mobile."', '".$contact_person."', '".$password."', '".$website."', '".$phone."', '".$fax."', '".$introduction."', '".$deals_in."')";
            $conn->select_db('B2B');
            $conn->query($sql);
        }
        else
        {
            echo '<h2>Access is Denied</h2>';
        }
        $conn->close();
?>
</body>

  • 1
    First of all remove that create table stuff from the script. They don't belong there. Then try to print out insert query. Also try to print the error from the query execution. – e4c5 May 03 '16 at 10:11
  • @e4c5 - I agree that the create content should not be there - but without it - I would not have spotted the typo difference between the column "Introduction" and the query "introduction" - ie: one is ca;italised and the other isn't – gavgrif May 03 '16 at 10:13

1 Answers1

0

you are defining the introduction column as

`Introduction` VARCHAR(400),

but attempting to insert date in 'introduction' - ie :not capitalised:

..., `introduction`, `deals_in` ).... 

So you need to alter either the script creating the table or the query. Given that no other columns are capitalised, I would recommend going with "introduction" for the column name.

gavgrif
  • 15,194
  • 2
  • 25
  • 27