2014-11-21 162 views
0

我不明白爲什麼會出現這樣的錯誤,請問您能幫助我嗎? 錯誤:
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> 
+0

當你調試idt_inf和名稱爲空? – 2014-11-21 20:31:36

+0

爲什麼你在所有輸入中間都有逗號''''input type =「text」,name =「tourName」/>'? – Sean 2014-11-21 20:44:32

+0

肖恩,謝謝你,我已經把英文逗號給了 – Zhenya 2014-11-21 20:57:08

回答

0
$prep_1->bindParam(":cityTour",$cityTourIncluded);` 

哪裏$cityTourIncluded定義?

編輯:這似乎不是你的問題,但它顯示了一個問題。如果您有通知顯示或記錄,您可能會弄清楚這一點。所以我猜你會有其他的通知和錯誤顯示你的問題。

+0

德文,謝謝你的建議。我錯過了cityTourIncluded,我添加了它,第一個錯誤消失了。第二個(關於'idT_inf')仍然存在。我猶豫,如果我以正確的方式做到這一點:$ res = $ this-> db-> query('SELECT @@ IDENTITY'); $ row = $ res-> fetch(PDO :: FETCH_ASSOC); $ idecko = $ row [0]; – Zhenya 2014-11-21 20:58:55

+0

我需要cityTour Informtion的ID,這是第一個插入。它是一個auto_incremented ID – Zhenya 2014-11-21 21:02:17

+0

第一次插入是否成功?你爲什麼使用'SELECT @@ IDENTITY'而不是'lastInsertId()'方法? MySQL是否支持'@ IDENTITY'? – Devon 2014-11-21 23:29:22

相關問題