PHP查询不会插入数据库

时间:2018-11-11 10:22:18

标签: php mysqli

以下查询不会插入数据库,我已经在数据库上尝试了此查询,因此我很确定查询可以正常工作。我还在下面添加了dbcon.php。

<?php
require '../api/dbcon.php';

$stmt=$conn->prepare("INSERT INTO joborder (AirCondition,
                                           CarpentryMasonry,
                                           ElectricalWorks,
                                           Plumbing,
                                           Welding,
                                           Campus,
                                           priorityId, 
                                           RequestorName,
                                           UserJobDescription,
                                           SerialCode
                                           ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );

 $stmt->bind_param('ssssssssss',
                $airConditioning,
                $masonryCarpentry,
                $electrical,
                $plumbing,
                $welding,
                $campus,
                $priority,
                $requester,
                $userJobDescription,
                $serialCode);

$airConditioning = "check";
$masonryCarpentry = "check";
$electrical = "check";
$plumbing = "check";
$welding = "check";
$campus =  'NA';
$priority =  '1';
$requester = "m";
$userJobDescription ="test";
//create serial code
$serialCode= "na12321";

?>

dbcon.php

      <?php
     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = '';
     $dbtable = "table";
     $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbtable);

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

2 个答案:

答案 0 :(得分:0)

您使用了错误的错误报告模式,因此需要仔细检查所有地方的错误,但实际上并非如此。

在第4行的not-dbcon.php上,您没有检查$ conn-> prepare成功,执行此操作,如果发生错误,它将返回bool(false)。在第16行,您没有检查$ stmt-> bind_param是否成功,执行此操作,如果出现错误,它将返回bool(false)。或更妙的是,不要这样做,只需在创建对象后立即运行$conn->report_mode = MYSQLI_REPORT_ALL;,即可将返回值错误报告转换为异常错误报告。

...而且最重要的是,似乎您忘了运行$ stmt-> execute()了,该命令实际上执行了查询,显然可以解释为什么不插入任何内容。

答案 1 :(得分:0)

<?php

$servername = "localhost";
$username = "root";
$password = "123456";
$database = "inventory";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

$stat = $conn->prepare("INSERT INTO salary (name, salary, job) values (?, ?, ?)");

$name = 'test';
$salary = '21123';
$job = 'demo';

$stat->bind_param($name,$salary, $job );

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

// prepare and bind
$stmt = $conn->prepare("INSERT INTO salary (name, salary, job) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute
$firstname = "Johnqqq";
$lastname = "123123";
$email = "sdadsad";
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();

?>