wogwan wogwan - 2 months ago 6
MySQL Question

show a row from the same table when pin is entered php

have this code so someone can check the authenticity of their product buy typing their serial number into a box and it will show if the product come from me and that works perfectly fine but my problem is that I'm trying to echo another Column in the same row that the serial number is in so it will also echo a description of the product once the correct serial number is entered, I've tried a few thing but I'm just going around in circles, any help would great, thanks in advance.

<?php
if(isset($_POST['pin']))
{
// include Database connection file
include("db_connection.php");

$pin = mysqli_real_escape_string($con, $_POST['pin']);

$query = "SELECT pin,product FROM auth WHERE pin = '$pin'";


if(!$result = mysqli_query($con, $query))
{
exit(mysqli_error($con));
}

if(mysqli_fetch_array($result) > 0)
{
// authentic code entered
echo '<div style="color: green;"> <b>'.$pin.'</b> This is a genuine product </div>';
}
else
{
// not valid code entered
echo '<div style="color: red;"> <b>'.$pin.'</b> Your product is not authentic! </div>';
}
}
?>

Answer

Here is how you can get the extra column and show it in your output. Just add the extra column name to your query and output it where you want. Note that I used description for the column name. You might want to rename that to match the real name of the column you want.

<?php
if(isset($_POST['pin']))
{
    // include Database connection file 
    include("db_connection.php");

    $pin = mysqli_real_escape_string($con, $_POST['pin']);

    $query = "SELECT pin,description FROM auth WHERE pin = '$pin'";


    if(!$result = mysqli_query($con, $query))
    {
        exit(mysqli_error($con));
    }

    if(mysqli_num_rows($result) > 0)
    {
        // authentic code entered
        echo '<div style="color: green;"> <b>'.$pin.'</b> This is a genuine product </div>';

        // get extra column and display
        $data = mysqli_fetch_array($result);
        echo '<div>' . $data['description'] . '</div>';
    }
    else
    {
        // not valid code entered
        echo '<div style="color: red;"> <b>'.$pin.'</b> Your product is not authentic! </div>';
    }
}
?>
Comments