【问题标题】:2 values from 1 input, PHP MySQL来自 1 个输入的 2 个值,PHP MySQL
【发布时间】:2013-03-28 04:18:07
【问题描述】:

我正在为条形码票务系统构建验证器脚本。需要根据数据库检查条形码编号。问题是条形码的前 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'];

      }
    ?>

感谢您的所有帮助。

【问题讨论】:

  • 您的问题是什么?还有,$hash 是怎么计算的?
  • 您的代码仍然容易受到 SQL 注入的攻击。了解准备好的语句。
  • @cernunnos 显然问题是如何?
  • 我只是想知道如何将输入分成 2 个值并发布到 php 文件中。
  • 使用substring

标签: php jquery mysql post input


【解决方案1】:

你可以试试这个,

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

参考这个。 PHP Substring

【讨论】:

  • 非常感谢。正是我需要的。
【解决方案2】:
$uuid = substr($_POST['barcode'], 0, 7);  
$hash = substr($_POST['barcode'], 7, 4);  

【讨论】:

    【解决方案3】:

    您只需在查询中添加一个 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}'");
    

    【讨论】:

      【解决方案4】:
      $uuid = substr($_POST['barcode'],0,7);
      $hash = substr($_POST['barcode'],7,4);
      

      我想这就是你要找的……

      【讨论】:

      • 请在发布答案前检查语法
      • 字符串先出现...然后开始,然后是长度。
      • 正是我想要的,但它需要修复。
      猜你喜欢
      • 2020-08-12
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      相关资源
      最近更新 更多