【发布时间】:2016-02-25 12:04:37
【问题描述】:
当我试图通过 php 代码在数据库中插入值时,它没有工作,也没有给出任何错误。这是我尝试执行但未成功的代码。
<?php
$mysqli = new mysqli("localhost", "admin", "password", "project") or die("couldn't connect to the database");
error_reporting(0);
session_start();
if (!isset($_SESSION["sess_user"])) {
header("location:index.php");
} else {
$username = $_SESSION['sess_user'];
if (isset($_GET['submit'])) {
if ($_GET['e1'] == "E-LN3465") {
$productname = $mysqli->real_escape_string($_GET['e1']);
if ($insert = $db->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))")) {
$checkQuery = $mysqli->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))");
}
}
}
}
?>
【问题讨论】:
-
执行两次查询,
$db在您的代码中没有任何内容,只需将其从您的代码中删除即可 -
@Calimero 在 INSERT SELECT 语句中不需要
valuesdev.mysql.com/doc/refman/5.7/en/insert-select.html -
@Saty 你是对的,我的错,谢谢你的评论。
-
@Calimero 不客气!我们都在这里学习!!
-
@Saty 但还是没用。
标签: php mysql mysqli insert mysql-insert-id