我正在尝试检查部件中的条形码,因为混合的风险。条形码由24个字符组成,前14个字符是部件号,因为最后10个字符是计数器,是不是检查前14个字符的解决方案?
以下是条形码示例:308739420 D52 AD1702600131
混合部件是因为来自另一部分的下一个条形码: 308739420的 B0L AD1702600131
因此,我的问题是:我可以在输入数据库中的前14个字符时检查吗?
我可以这么说,告诉我今天用于在数据库中引入条形码的脚本:
<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<h1>Barcode checker</h1>
<div id="login"><hr/>
<form action="" method="post">
<label>Barcode :</label>
<input type="text" name="TAK0010barcode" id="name" required="required" placeholder="Please enter barcode"/><br /><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
}
?>
</body>
</html>
谢谢。
答案 0 :(得分:1)
您可以使用PHP函数substr()
:
$control = '308739420D52AD';
$test = substr($_POST["TAK0010barcode"], 0, strlen($control));
if($control == $test){
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
// etc...
文档:http://php.net/manual/en/function.substr.php
UPDATE:扩展示例,用您的php代码替换所有内容:
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$control = '308739420D52AD'; // YOUR BAR CODE IDENTIFIER
$test = substr($_POST["TAK0010barcode"], 0, strlen($control)); // BEGINNING OF THE POSTED BAR CODE.. SET TO SAME LENGTH AS BAR CODE IDENTIFIER
if($control == $test){ // CHECK IF STRINGS ARE ALIKE
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
} else { // STRINGS DO NOT MATCH!
echo "<span style='color: red; font-size: 40px; font-weight: bold'>The barcode don't match the part number. Please use a barcode starting with ".$control."</span>";
}
}
?>