我不明白爲什麼會出現這樣的錯誤,請問您能幫助我嗎? 錯誤:
SQLSTATE [23000]:完整性約束違規:1048列 '名稱' 不能爲空PHP SQLSTATE [23000]:完整性約束衝突
SQLSTATE [23000]:完整性約束違規:1048列 'idT_Inf' 不能爲空
也許我需要改變數據庫中的一些細節?是的,我爲數據庫中的每一列設置了「NOT NULL」條件,但我不會在表單中留下任何空白(來自Administrativni prostredi)爲空。你們有些人遇到過這樣的問題。感謝提前:)
PraceSDB.php:
<?php
class Connection{
public function __construct(){
try{
$this->db = new PDO('mysql:host=localhost; dbname=brichevg','brichevg','wa1');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public function addNote(){
$name= $_POST['tourName'];
$path= $_POST['path'];
$days= $_POST['days'];
$hotel=$_POST['hotel'];
$priceSingleRoom= $_POST['priceSingleRoom'];
$priceDoubleRoom= $_POST['priceDoubleRoom'];
$priceBreakfast= $_POST['priceBreakfast'];
$priceLunch= $_POST['priceLunch'];
$priceDinner= $_POST['priceDinner'];
$cityTourPrice= $_POST['priceTour'];
$totalTourPrice= $_POST['totalTourPrice'];
$date= $_POST['date'];
$sql_1= "INSERT INTO Tour_Info (name, path, Hotel, Price_of_single_room, Price_of_double_room, Price_of_breakfast,
Price_of_lunch, Price_of_dinner, City_tour, Total_tour_price, City_tour_price, Amount_of_days)
VALUES (:name, :path, :hotel, :priceSingleRoom, :priceDoubleRoom, :priceBreakfast, :priceLunch, :priceDinner, :cityTour,
:totalTourPrice, :cityTourPrice, :days) ";
try{
$prep_1=$this->db->prepare($sql_1);
$prep_1->bindParam(":name",$name);
$prep_1->bindParam(":path",$path);
$prep_1->bindParam(":days",$days);
$prep_1->bindParam(":hotel",$hotel);
$prep_1->bindParam(":priceSingleRoom",$priceSingleRoom);
$prep_1->bindParam(":priceDoubleRoom",$priceDoubleRoom);
$prep_1->bindParam(":priceBreakfast",$priceBreakfast);
$prep_1->bindParam(":priceLunch",$priceLunch);
$prep_1->bindParam(":priceDinner",$priceDinner);
$prep_1->bindParam(":cityTour",$cityTourIncluded);
$prep_1->bindParam(":cityTourPrice",$cityTourPrice);
$prep_1->bindParam(":totalTourPrice",$totalTourPrice);
$prep_1->execute();
$res=$this->db->query('SELECT @@IDENTITY');
$row=$res->fetch(PDO::FETCH_ASSOC);
$idecko=$row[0];
}
catch(PDOException $e){
echo $sql_1 . "<br>" . $e->getMessage();
}
$sql_2="INSERT INTO Tour(idT_inf,date) VALUES (:idT_inf, :date)";
try{
$prep_2=$this->db->prepare($sql_2);
$prep_2->bindParam(":date",$date);
$prep_2->bindParam(":idT_inf",$idecko);
$prep_2->execute();
}
catch(PDOException $e){
echo $sql_2 . "<br>" . $e->getMessage();
}
}
}
Administrativni_prostredi.php:
<?php
require('praceSDB.php');
$connection= new Connection();
if(isset($_POST['submitNewTour'])){
$connection->addNote();
}
?>
<html>
<head><Title>Administrative interface</title>
<meto charset="utf-8"/>
</head>
<body>
<h3>Add new tour</h3>
<form method="post" action="">
<p><label>Tour name:</label>
<input type="text",name="tourName"/>
</p>
<p><label>Tour path:</label>
<input type="text",name="path"/>
</p>
<p><label>Amount of days:</label>
<input type="text",name="days"/>
</p>
<p><label>Hotel:</label>
<input type="text",name="hotel"/>
</p>
<p><label>Hotel price(single room):</label>
<input type="text",name="priceSingleRoom"/>
</p>
<p><label>Hotel price(double room):</label>
<input type="text",name="priceDoubleRoom"/>
</p>
<p><label>Breakfast price:</label>
<input type="text",name="priceBreakfast"/>
</p>
<p><label>Lunch price:</label>
<input type="text",name="priceLunch"/>
</p>
<p><label>Dinner price:</label>
<input type="text",name="priceDinner"/>
</p>
<p><label>City tour price:</label>
<input type="text",name="priceTour"/>
</p>
<p><label>City tour included:</label>
Yes: <input type="radio" name="cityTour" value="yes" /> No:<input type="radio",name="cityTour" value="no"/>
</p>
<p><label>Tour price (transport,agency servise): </label>
<input type="text",name="totalTourPrice"/>
</p>
<p><label>Trip's date:</label>
<input type="text",name="date"/>
</p>
<input type="submit" value="submit" name="submitNewTour"/>
</form>
<h3>All actual tours</h3>
</body>
</html>
當你調試idt_inf和名稱爲空? – 2014-11-21 20:31:36
爲什麼你在所有輸入中間都有逗號''''input type =「text」,name =「tourName」/>'? – Sean 2014-11-21 20:44:32
肖恩,謝謝你,我已經把英文逗號給了 – Zhenya 2014-11-21 20:57:08