2017-10-13 19 views
-1

私はプログラミングが初めてで、援助が必要です。私も持っているHTMLフォームを使用したPHPの値の割り当てと計算

<?php       
    include ('dbconnect.php'); 

// get values from the form 
    function getPosts(){ 
    $posts = array(); 
    $posts[0] = $_POST['bookingid']; 
    $posts[1] = $_POST['bookingname']; 
    $posts[2] = $_POST['lengthofstay']; 
    $posts[3] = $_POST['guests_description'];  
    $posts[4] = $_POST['startdate']; 
    $posts[5] = $_POST['enddate']; 
    $posts[6] = $_POST['other_information']; 
    return $posts; 
    } 

if (isset($_POST['bookingdetails'])) { 

    $data = getPosts(); 


if (empty($bookingname)){ 
     $errors[] = "Please enter the your booking name."; 
    } 

if(empty($errors)){ 
     foreach($errors as $error){ 
      echo $error . "<br/>"; 
    } 
else{ 

    $insert_Query = "insert into bookings 
    (Bookingid,Bookingname,Lengthofstay,Details_about_guests, 
    Booking_start_date,Booking_end_date,Other_relavant_information) 
    values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]', 
    '$data[5]','$data[6]')"; 

    $result = mysqli_query($db, $insert_Query); 

if ($result) 
{ 
echo "<font color = 'green' . <p>Successfully Booked</p> </font>"; 
} 
else{ 
    echo "<p> Something went Wrong </p>" . mysqli_error ($db); 

    } 
    } 
    } 
    ?> 

:私はまた、次のようになります。同じページ上のPHPコードを持って

<form method = "post" action = "" > 
     <table cellspacing="15"> 

<tr> 
    <th>Bookingid </th> 
    <td><input type = "text" name = "bookingid"/> </td> 
</tr> 

<tr> 
    <th>Booking Name </th> 
    <td><input type = "text" name = "bookingname" /> </td> 
</tr> 

<tr> 
    <th> <label for = "Cabin Type">Cabin Type </label> </th> 
    td><select name = "Cabin Type" id = "Cabin Type"> 
    <option select>Select a Cabin Type</option> 
    <option value = "Luxury Cabin">Luxury Cabin </option> 
    <option value = "Contemporary Cabin">Contemporary Cabin </option> 
    <option value = "Original Cabin">Original Cabin </option> 
    </select> 
    </td> 
    </tr> 

<tr> 
    <th>Length of Stay </th> 
    <td><input type = "text" name = "lengthofstay" /> </td> 
</tr> 

<tr> 
    <th>Details About Guests </th> 
    <td><textarea cols = "30" rows = "5" type = "text" name="guests_description" 
    /> </textarea> </td> 
</tr> 

<tr> 
    <th>Booking Start Date </th> 
    <td><input type = "text" name = "startdate" /> </td> 
</tr> 

<tr> 
    <th>Booking End Date </th> 
    <td><input type = "text" name = "enddate" /> </td> 
</tr> 

<tr> 
    <th>Other Relavant Information </th> 
    <td><textarea cols = "30" rows = "5" type = "text" name="other_information" 
    /> </textarea> </td> 
</tr>   

<tr> 
    <th> </th> 
    <td> <input type = "submit" value = "Create Booking!" name = 
     "bookingdetails" /> </td> 
</tr> 
</table>    
</form> 

:私は、次のコードでaddbooking.phpというページにHTMLフォームを持っています私のデータベース内のテーブルのように見えたキャビンと呼ばれる:私はいくつかの支援がどのように私はCABIからの価格のそれぞれを割り当てないで必要なものを、今

Picture of the Cabins table

n_Price列をフォーム上のドロップダウンメニューの各項目に入力し、「予約詳細」ボタンを押すと、「1200」の選択された値/オプションに「Stay of Stay」フィールドに入力された値例えば ​​'php'を使った '4'。オプションの値は、キャビンのIDになるよう

Assigning the Values to Each Cabin Type

+0

は ''

+0

''の代わりに ''を使用できますか?あなたは正確な価値を得ることができます。値は「価格」として表すことができるので、そこに値を入れることができます。 – Jonjie

+2

@ハッシュ価格をフォームに入れることは非常に悪い考えです。ユーザーは開発者ツールでフォームを変更して価格を変更することができます。 – Barmar

答えて

0
があなたのメニューを変更

:以下は、私が何を意味するかの絵があります。その後、フォームを処理するときには、価格を調べ、滞在期間を掛けます。だから、メニューは次のようになります。

<select name = "Cabin Type" id = "Cabin Type"> 
    <option select>Select a Cabin Type</option> 
    <option value = "1">Luxury Cabin </option> 
    <option value = "2">Contemporary Cabin </option> 
    <option value = "3">Original Cabin </option> 
</select> 

とフォームが送信されたときに、次のようなクエリん:

$stmt = $dbh->prepare("SELECT cabin_price FROM cabins WHERE cabin_id = :id"); 
$stmt->bindParam(":id", $_POST["Cabin Type"]); 
$stmt->execute(); 
$row = $stmt->fetch(PDO::FETCH_ASSOC); 
$price = $row['cabin_price']; 
$total_price = $price * $_POST['lengthofstay']; 
+0

ちょっと、私はPHPを取得しているコードを使用するとき致命的なエラー:定義されていない関数の準備を呼び出す –

+0

上記のコードはあなたがPDOを使用していることを前提としています。他のMySQLエクステンションを使用している場合は、それに変換する必要があります。それは一般的なアイデアのデモンストレーションです。 – Barmar

+0

私は答えを投稿した後にあなたのmysqliコードを追加しました。あなたのコードはSQLインジェクションの対象ですので、準備されたステートメントの実行方法を学ぶ必要があります。 – Barmar

関連する問題