2013-04-05 51 views
-1

我正在爲條形碼票務系統構建一個驗證器腳本。需要根據數據庫檢查條形碼號碼。事情是條形碼的前7個數字是客戶ID,最後4個數字是座位ID。來自1個輸入的2個值,MySQL MySQL

例如:

12345671234 
^^^^^^^--Customer ID 
     ^^^^-- Seat ID 

正如我將使用條碼掃描器掃描條形碼和一個輸入框,我需要在 1輸入到單獨的這兩個值將 寫完整的條碼爲了從數據庫中獲得正確的數據。

它現在想:

<form action="validator.php" method="post"> 
Customer ID: <input id="uuid" name="uuid" maxlength="7"><br> 
Seat:  <input id="hash" name="hash" maxlength="4"> 

需要像:

Barcode: <input id="barcode" name="barcode" maxlength="11"><input type="submit"> 

<input type="submit"> 
</form> 

而且PHP端

<?php 

$uuid = $_POST['uuid']; 
$hash = $_POST['hash']; 




    $con=mysqli_connect("localhost","admin","321","db_name"); 
    // Check connection 
    if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

    $result = mysqli_query($con,"SELECT * FROM seat_booking_bookings 
    WHERE uuid ='$uuid'"); 

    while($row = mysqli_fetch_array($result)) 
     { 
     echo $row['customer_name'] . " " . $row['customer_phone']; 
     echo "<br>"; 
     } 

     $result = mysqli_query($con,"SELECT * FROM seat_booking_bookings_seats 
    WHERE hash ='$hash'"); 

     while($row = mysqli_fetch_array($result)) 
     { 
     echo "Seat:"; 
     echo $row['seat_id'] . " " . $row['price']; 

     } 
    ?> 

感謝您的幫助。

+0

你的問題是什麼?另外,$ hash是如何計算的? – cernunnos 2013-04-05 16:44:21

+0

您的代碼仍然容易受到SQL注入的影響。瞭解準備好的陳述。 – Kermit 2013-04-05 16:45:07

+0

@cernunnos顯然的問題是如何? – mplungjan 2013-04-05 16:46:51

回答

2

你可以試試這個,

$uuid = substr($_POST['barcode'], 0,7); 
$hash = substr($_POST['barcode'], 7,4); 

請參閱此。 PHP Substring

+0

非常感謝。正是我需要的。 – user2249907 2013-04-05 16:55:09

+0

太好了。不用謝 – 2013-04-05 16:55:51

-1
$uuid = substr($_POST['barcode'],0,7); 
$hash = substr($_POST['barcode'],7,4); 

我認爲這是你在找什麼...

+0

請在發佈答案之前檢查語法 – Amir 2013-04-05 16:52:43

+1

該字符串首先是...然後開始,然後是長度。 – bestprogrammerintheworld 2013-04-05 16:53:36

+0

正是我在找的東西,但它需要修復。 – user2249907 2013-04-05 16:54:49

1
$uuid = substr($_POST['barcode'], 0, 7); 
$hash = substr($_POST['barcode'], 7, 4); 
0

您只需要在查詢中添加一個AND語句。你正在嘗試基於兩個值進行驗證,對吧?

$result = mysqli_query($con,"SELECT * FROM seat_booking_bookings 
WHERE uuid ='{$uuid}' AND hash ='{$hash}'"); 

但是,爲了防止sql注入,你應該首先轉義你的值。所以這會更好:

$uuid = mysql_real_escape_string($uuid); 
$hash = mysql_real_escape_string($hash); 

$result = mysqli_query($con,"SELECT * FROM seat_booking_bookings 
WHERE uuid ='{$uuid}' AND hash ='{$hash}'");