0

Here in this code I am accepting data as an array and inserting it in the database. As I am using while loop I am having multiple input fields with same name as you can see in the HTML code below. What I want to do is, I want to add a check that if two fields are same then echo error saying two fields cannot be the same else insert the data. I could do it if there were two different input fields for the data like if($field1 == $field2){ echo "Error!"; else insert data. But here the field is only one with is giving multiple fields in a while loop. How can I add this check?

HTML form code:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <?php while($pro = $priq->fetch()){ extract($pro); ?>
        <div class="row tbpad">
          <div class="col-sm-3 col-xs-6">
            <p><i class="fa  fa-caret-right"></i> <?php echo $bk_title; ?></p>
          </div>
          <div class="col-sm-1 col-xs-1">
            <div class="form-group">
              <input type="text" class="form-control" name="priority[]" value="<?php echo $pr_priority; ?>">
              <input type="hidden" class="form-control" value="<?php echo $bk_id; ?>" name="bkid[]">
            </div>
          </div>
        </div>
        <?php } ?>
        <input type="submit" class="btn btn-orange" value="Set Priority" name="priority">
      </form>

PHP code:

if(isset($_POST['priority'])){
    $priority = (!empty($_POST['priority']))?$_POST['priority']:null;
    $bkid     = (!empty($_POST['bkid']))?$_POST['bkid']:null;

    $iNumSets = count($priority);

    for($i = 0; $i < $iNumSets; $i++){
        $str = "INSERT INTO priorities(pr_book, pr_by, pr_priority)
                VALUES('$bkid[$i]', '$sess', '$priority[$i]')";
        $res = $PDO->prepare($str);
        $res->execute();

        if($res){
            echo '<script>alert("Priority added successfully!");window.location = "view-order.php";</script>';  
        }else{
            echo '<script>alert("Server Error! Please try back later.");window.location = "add-order.php";</script>';
        }
    }
}
  • inside the loop you used window.location it will redirect to another page on a single insert query. the one thing is that it would not work for multiple insert query – dev Apr 19 '16 at 09:53
  • first understand the question properly... then answer only if you know the answer... of for any suggestion plz be related to the question specifically.. the place where I used window.location will be executed only if the insert is successful as it is inside if statement.. so there is nothing to worry about it.. –  Apr 19 '16 at 10:01
  • Your code is vulnerable to SQL injections, you have to use prepared statements – Your Common Sense Apr 19 '16 at 10:03
  • i m already using PDO... Your Common Sense.. its just an example.. please use Your Common Sense.. just kidding.. :) –  Apr 19 '16 at 10:53

1 Answers1

-1

Since $priority is an array which you get from $_POST['priority'], you could do the following:

$num_original = count($priority);
$num_unique = count(array_unique($priority));

if ($num_original != $num_unique) {
  echo "One or more item were idendical!";
}

Also make sure you use mysql_real_escape_string() or similar methods on your $_POST input before you pass it into the SQL query. Otherwise your code is vulnerable to SQL injections!

radonthetyrant
  • 1,346
  • 1
  • 8
  • 13
  • I am using PDO... so no need to use mysql_real_escape_string... however.. I will try your answer now.. thanks –  Apr 19 '16 at 09:51