【问题标题】:Inserting Value using php mysqli connection使用 php mysqli 连接插入值
【发布时间】:2024-01-19 21:51:01
【问题描述】:

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

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

【问题讨论】:

  • 首先从脚本中删除创建表的东西。他们不属于那里。然后尝试打印出插入查询。也尝试打印查询执行中的错误。
  • @e4c5 - 我同意创建内容不应该存在 - 但没有它 - 我不会发现“简介”列和查询“简介”之间的错字差异 - 即:一个是ca;italized,另一个不是

标签: php mysql mysqli insert-into


【解决方案1】:

您将介绍列定义为

`Introduction` VARCHAR(400),

但试图在“介绍”中插入日期 - 即:不大写:

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

因此,您需要更改创建表的脚本或查询。鉴于没有其他列大写,我建议使用“introduction”作为列名。

【讨论】: