Search code examples
phpmysqlajaxbarcode

Insert and display Barcode in PHP / MySql


I have a form where i scan my barcode

form_insert.php

<form action="stock_insert.php" method="get">
        <input type="text" name="barcode" placeholder="Εισαγωγή BarCode"> <br>
        <input type="submit" name="SAVE" 
</form>

Insert it in my database

stock_insert.php

<?php
include_once  'database_connection.php';
$barcode = $_GET['barcode'];
$sql = "INSERT INTO stock(barcode) VALUES ('$barcode');";
mysqli_query($conn, $sql);
header('Location: form_insert.php?signup=success');
?>

When i'm scanning my barcode the number that i show is 008576124045

But in my database the number is saved as 2147483647

After that when i want to scan again and display my barcode like this:

test.php

<script>
        $(document).ready(function(){ 
            $("#submit").click(function(){
                var barcode =  $("#kwdikos").val();
                $.post("database/ajax.php", 
                {
                    barcode: barcode ,
                    value: value
                },function(data){
                    $("#test").html(data);          
                });
           });
        });
    </script> 
        <input type="text"   id="kwdikos" placeholder="Εισαγωγή  BarCode">
        <input type="submit" id="submit" name="add" value="Προσθήκη">

ajax.php

<?php
$barcode = $_POST['barcode'];
$sql_get = "SELECT * FROM stock WHERE barcode ='$barcode' ";
$result = mysqli_query($conn, $sql_get);      
        $resultCheck = mysqli_num_rows($result);
        if ($resultCheck > 0) {
            while  ($row = mysqli_fetch_assoc($result)){
                    $name = $row['name'];
                    $color = $row['color'];
                    $kind = $row ['kind'];
                    $price = $row['price'];
            }}
$sql = "INSERT INTO receipt (barcode, name, color, kind, value , price) VALUES ('{$barcode}','{$name}','{$color}','{$kind}','{$value}' , '{$price}')";
mysqli_query($conn, $sql);
?>
<table class="table table-dark">  
        <thead>
            <tr>
                <td>BarCode</th>
                <td>Ονομασία</th>
                <td>Χρώμα</th>
                <td>Είδος</th>
                <td>Ποσότητα</td>
                <td>Τιμή</td>
            </tr>
        </thead>         
        <tbody>
                 <?php 
                        $sql = "SELECT * FROM receipt ";
                        $result = mysqli_query($conn, $sql);      
                        $resultCheck = mysqli_num_rows($result);
                        if ($resultCheck > 0) {
                            while  ($row = mysqli_fetch_assoc($result)){
                    ?>
            <tr>    
                <td><?php echo $row['barcode']; ?> </td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['color']; ?></td>
                <td><?php echo $row['kind'];?></td> 
                <td><?php echo $row['value'];?> </td>
                <td><?php echo $row['price'];
                echo "€";?></td>      
            </tr>   
                       <?php }} ?>
        </tbody>
    </table>

When i scan ones again the number the input is (008576124045)

My screen displays the RIGHT number that is saved in database (2147483647)

But all the other stuff that are want to display DON'T DISPLAY!

When i dont use barcode , but in my input i write a number with my keyboard (for example: 10500 ) i have no problem with my code. It is running perfectly and all stuffs in database are correct.


Solution

  • This is because

    1. You are saving barcode as an integer in DB and the max range of int is 2147483647. It will not save any number greater than this.
    2. You cant use int because integer will treat barcode 0096234 as 96234.
    3. Try writing number greater than 2147483647 via keyboard, it will not save as you expected.

    Change your datatype maybe varchar