简体   繁体   English

PHP查询不会插入数据库

[英]PHP query wont insert to database

The below query wont insert to database, I had tried this query on my database so I am quite sure that the query is working. 下面的查询不会插入数据库,我已经在数据库上尝试了此查询,因此我非常确定查询是否可以正常工作。 I also added the dbcon.php below. 我还在下面添加了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 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());
     }
  ?>

you're using a bad error reporting mode, and thus need to meticulously check for errors everywhere, but you're not. 您正在使用错误的错误报告模式,因此需要仔细检查所有地方的错误,但事实并非如此。

on not-dbcon.php on line 4 you're not checking that $conn->prepare succeeded, do that, it returns bool(false) if there was an error. 在第4行的not-dbcon.php上,您无需检查$ conn-> prepare成功,执行此操作,如果出现错误,它将返回bool(false)。 on line 16 you're not checking that $stmt->bind_param succeeded, do that, it returns bool(false) if there was an error. 在第16行,您没有检查$ stmt-> bind_param是否成功,执行此操作,如果出现错误,它将返回bool(false)。 or better yet, don't do that, just convert return-value-error-reporting into exception-error-reporting, by running $conn->report_mode = MYSQLI_REPORT_ALL; 或者更好的是,不要这样做,只需运行$conn->report_mode = MYSQLI_REPORT_ALL;即可将返回值错误报告转换为异常错误报告$conn->report_mode = MYSQLI_REPORT_ALL; immediately after creating the object. 创建对象后立即进行。

... and most importantly, seems you forgot to run $stmt->execute(), which actually executes the query, which obviously explains why you're not inserting anything. ...而且最重要的是,您似乎忘记了运行$ stmt-> execute(),该命令实际上执行了查询,这显然说明了为什么不插入任何内容。

<?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();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM