我遇到的問題是下面的PHP代碼只能將數據插入到一個表格中。我想要的是,如果從POST的目錄類別等於例如藍色INSERT到表藍色,但如果它等於黃色插入到黃色,但如果它等於紅色INSERT到表紅色。MySQLi插入如果
我發現處理插入如果存在,但不是多重插入if語句的唯一答案。任何幫助將不勝感激。我只是學習PHP代碼。
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','some directory','password','some username');
//Output any connection error
if ($mysqli->connect_error) {
die('Connection failed : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//values to be inserted in database table
$firstname = '$_POST[firstname]';
$lastname = '$_POST[lastname]';
$city = '$_POST[city]';
$state = '$_POST[state]';
$zipcode = '$_POST[zipcode]';
$directorycategory = '$_POST[directorycategory]';
$active = '$_POST[active]';
$query = ("INSERT INTO blue(
firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)");
$statement = $mysqli->prepare($query);
//bind parameters
$statement->bind_param('sssssss', $_POST['firstname'], $_POST['lastname'], $_POST['city'], $_POST['state'], $_POST['zipcode'], $_POST['directorycategory'], $_POST['active']);
if($statement->execute()){
header("some location");
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
?>
首先,首先刪除引用''__POST [firstname]'和所有其他引號,然後在數組名稱本身內部使用引號。即'$ _POST ['firstname']' - 你在引用錯誤的引用。然而,你不需要這些,你已經在綁定和格式正確,他們爲什麼在你的代碼? –
您可以連接的'$彥博值[ 'directorycategory']'到您的查詢字符串'$查詢=( 「INSERT INTO」。$ _ POST [ 'directorycategory'。「(名字,姓氏,城市,ST ... 「);' –
@PLAudet更好地清理那個輸入,但是 – kittykittybangbang