使用php-mysqli连接插入值


Inserting Value using php mysqli connection

创建了数据库和表,但表单值没有进入数据库表表单字段值是绝对正确的,我通过echo 检查了它们

我的insert into sql命令中有错误吗。代码如下

<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>

您将介绍列定义为

`Introduction` VARCHAR(400),

但试图在"介绍"中插入日期,即:未大写:

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

因此,您需要更改创建表的脚本或查询。考虑到没有其他列是大写的,我建议使用"introduction"作为列名。