-1

我正在为条形码票务系统构建验证器脚本。需要根据数据库检查条形码编号。问题是条形码的前 7 个数字是客户 ID,后 4 个数字是座位 ID。

例如:

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'];

      }
    ?>

感谢你的帮助。

4

4 回答 4

2

你可以试试这个

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

参考这个。PHP 子字符串

于 2013-04-05T16:52:09.473 回答
1
$uuid = substr($_POST['barcode'], 0, 7);  
$hash = substr($_POST['barcode'], 7, 4);  
于 2013-04-05T16:51:29.603 回答
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}'");
于 2013-04-05T16:54:34.053 回答
-1
$uuid = substr($_POST['barcode'],0,7);
$hash = substr($_POST['barcode'],7,4);

我想这就是你要找的……

于 2013-04-05T16:48:35.770 回答