2017-07-04 40 views
0

我想顯示此消息「已預訂 - 請選擇其他日期」如果兩個客戶選擇相同的產品(pg_no)和日期(Date) 。唯一約束PHP&MYSQL - 如何顯示「已預訂請選擇其他日期」信息

<?php 
//connecting string 
include("dbconnect.php"); 

//assigning 
$name=$_REQUEST['Name']; 
$tele=$_REQUEST['Tele']; 
$city=$_REQUEST['City']; 

// UNIQUE CONSTRAINT 
$pg_no=$_REQUEST['pg_no']; //product 
$date=$_REQUEST['Date'];  //date 

//checking if pg_no and Date are same 
$check = mysqli_query($db_connect,"SELECT * FROM lstclient WHERE pg_no='{$pg_no}', Date='{$date}'"); 

{ 
    echo "Already booked please select another date<br/>"; 
} 
//if not the insert data 
else 
{ 
    $query = mysqli_query($db_connect,"INSERT INTO lstclient(pg_no,Name,Tele,City,Date) VALUES('$pg_no','$name','$tele','$city','$date')") or die(mysql_error()); 
} 

// link closing 
mysqli_close($db_connect); 

// messaging 
if($query) 
{ 
    header("location:index.php?note=failed"); 
} 
else 
{ 
    header("location:index.php?note=success"); 
} 
?> 
+0

我應該在HTML頁面添加什麼 –

+0

請至少發佈有效的代碼 - 'INSERTINTOlstclient'不正確。 –

+0

INSERT INTO lstclient –

回答

1

要查看HTML網頁上的消息,根據你的代碼已經提供,你只需要設置一個$_GET VAR:

<?php 
if (isset($_GET['note'])) 
{ 
    if($_GET['note'] == 'failed';) 
    { 
     $note = "Those dates have already been booked."; 
    } 
    else if($_GET['note'] == 'success') 
    { 
     $note = "You have been booked!"; 
    } 
} 
else 
{ 
    $note = ""; 
} 
?> 

<div class="message"> 
    <p class="alert"> 
     <?php 
      if ($note != "") 
      { 
       echo $note; 
      } 
     ?> 
    </p> 
</div> 

這將檢查在note參數你的網址,並將其設置爲$note然後將其輸出到您的HTML。

您不需要向數據庫添加任何內容,您可以保存請求並記錄它,以便查看哪些日期最受歡迎。

0

這裏有兩個簡單的選項。

選項1 - 分配的消息給一個變量

而是附和你的錯誤信息,將其存儲在其中有一個默認值(例如,「成功」)的變量。然後

$my_str = "Already booked please select another date<br/>"; 

... 

header("location:index.php?note=$my_str"); 

在HTML的一面,你只需要打印的$_GET['note']值:然後,你做你的代碼的最後header()部分,返回這個變量作爲註釋。

選擇2 - 見塞繆爾·豐塞卡的答案

他打我吧!

相關問題