-2

So, I have everything working, then I try to add the duplicate insertion prevention, and it all breaks. If someone could help me, that would be great! :)

index.php:

<?php
include('dbConfig.php');
?>

<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="style.css">

<title>Database Solution</title>


</head>
<body>

 <?php

     $barcode=$_POST['barcode']
     $check=mysqli_query($conn,"select * from Barcodes where barcode='$barcode'");
     $checkrows=mysqli_num_rows($check);

  if(isset($_POST['save']))
{
    $sql = "INSERT INTO Barcodes (barcode)
    VALUES ('".$_POST["barcode"]."')";


   if($checkrows>0) {
      echo "barcode exists";
   } else {  
    $result = mysqli_query($conn,$sql);
}

?>

<h2 align="center">Barcode Database</h2>

<style type="text/css">
    .fieldset-auto-width {
         display: inline-block;
    }
</style>
<div class="form">
    <form method="post" align="center">
        <fieldset class="fieldset-auto-width">
            <legend align="center">Enter Barcode:</legend>
    <input type="text" name="barcode" size="35"><br/><br />

    <button type="submit" name="save">Input</button>
        </fieldset>
    </form>
</div>

    <p align="center">© 2018 Nathaniel</p>

</body>
</html>

dbConfig.php:

$conn=mysqli_connect("localhost","n****ec_**s","****","n******_***");

if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}

?>

Is there a way I can fix this? I'm probably being dumb, but I hope that someone can help me fix this issue.

Nathaniel
  • 1
  • 1

2 Answers2

0

You have two very basic issues in your code. You really should be drilling down to what these issues are and how to debug them. Anyways here is the code:

    <?php
include('dbConfig.php');
?>

<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="style.css">

<title>Database Solution</title>


</head>
<body>

 <?php

     $barcode=$_POST['barcode'];
     $check=mysqli_query($conn,"select * from Barcodes where barcode='$barcode'");
     $checkrows=mysqli_num_rows($check);

    if(isset($_POST['save']))
    {
        $sql = "INSERT INTO Barcodes (barcode) VALUES ('".$_POST["barcode"]."')";


       if($checkrows>0) {
          echo "barcode exists";
        } else {  
        $result = mysqli_query($conn,$sql);
        }
    }   

?>

<h2 align="center">Barcode Database</h2>

<style type="text/css">
    .fieldset-auto-width {
         display: inline-block;
    }
</style>
<div class="form">
    <form method="post" align="center">
        <fieldset class="fieldset-auto-width">
            <legend align="center">Enter Barcode:</legend>
    <input type="text" name="barcode" size="35"><br/><br />

    <button type="submit" name="save">Input</button>
        </fieldset>
    </form>
</div>

    <p align="center">© 2018 Nathaniel</p>

</body>
</html>

You are missing a semi-colon on Line 23

$barcode=$_POST['barcode'];

And missing a closing paranthesis after your if statement:

if(isset($_POST['save']))
    {
        $sql = "INSERT INTO Barcodes (barcode) VALUES ('".$_POST["barcode"]."')";


       if($checkrows>0) {
          echo "barcode exists";
        } else {  
        $result = mysqli_query($conn,$sql);
        }
    }   

Also like the comments say try implementing parameterized queries, this code is open for sql injections.

Jash Parekh
  • 1,097
  • 2
  • 10
  • 15
  • It is likely this was pseudo code. The errors you mention are PHP syntax errors and wouldn't allow for the code to execute, so dups wouldnt have produced. – user3783243 Jun 09 '18 at 22:59
  • His code was just fine, but just like you said there were only syntactical errors. Something he himself should have found out. This is not a portal to post such trivial issues. – Jash Parekh Jun 09 '18 at 23:05
  • The question is about `Preventing Duplicate Row Insertion Not Working` though, not PHP errors. – user3783243 Jun 09 '18 at 23:06
  • This fixed everything. I also added the SQL-Injection proofing. Thanks! – Nathaniel Jun 09 '18 at 23:12
  • @Nathaniel There is no chance this fixed the question you asked. If you had these errors the code wouldnt have executed so you wouldn't have had dups. – user3783243 Jun 09 '18 at 23:26
  • @user3783243 I mean, apparently it did? Or I possibly made another change, because now it works. I mean, that's what matters. – Nathaniel Jun 10 '18 at 01:17
  • @Nathaniel No, that is not correct. You're now misinforming future users that the changes this user made will resolve their issue. You made some changes and it coincidentally resolved the issue. This answer would help with PHP syntax issues, which numerous threads already deal with. This will be of no help to users looking for issues with `Preventing Duplicate Row`. – user3783243 Jun 10 '18 at 05:38
  • @user3783243 can you please explain why this will not resolve the issue here? – Jash Parekh Jun 12 '18 at 22:58
  • @JashParekh because this has nothing to do with `Preventing Duplicate Row`. This is 100% PHP syntax errors, no relation to MySQL or rows being inserted. – user3783243 Jun 13 '18 at 02:14
  • @user3783243 his solution already takes care of that. His issue was, he could not test it as there were syntactical errors. Had he known these errors are due to syntax issue, he would not have posted the question here. So my solving syntactical issues gave him a clarity that his solution is correct. – Jash Parekh Jun 14 '18 at 20:16
  • @JashParekh Then the answer should be how to get error messages. This doesn't answer the question that was asked. (which the answer is to use constraints on the column) – user3783243 Jun 15 '18 at 01:34
-1
<?php
if(isset($_POST['save']))
{
    $barcode=$_POST['barcode'];
     $check=mysqli_query($conn,"select * from Barcodes where barcode='".$barcode."'");
   $checkrows=mysqli_num_rows($check);
     if($checkrows==0) {
         $sql = "INSERT INTO Barcodes (barcode) VALUES ('".$_POST["barcode"]."')";       
      $result = mysqli_query($conn,$sql);
     }else{  

         echo "barcode exists";
    }
}   

?>