0

Code for check value in mysql by php and successful return will redirected to another page. Basically, I want code entered in field will check within MySQL table, if found then check status in next column and if found unused, then make it used and user will redirect to the registration page. Made changes as per suggestions, received errors on webpage.

redeem-code.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

<html>
<head>
  <?php

  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "redeem_code";
  $rcode = "";
  $status = "";
  $errors   = array();

  $db = mysqli_connect($servername, $username, $password, $dbname);
  // call the redeem() function if redeem_btn is clicked
  if (isset($_POST['redeem_btn'])) {
    $err = array();
   redeem();
  }

  // Redeem Code
  function redeem(){
   // call these variables with the global keyword to make them available in function
   global $db, $errors, $rcode, $status;

   // receive all input values from the form. Call the e() function
      // defined below to escape form values
   $rcode    =  e($_POST['rcode']);
   $status       =  e($_POST['status']);

   // form validation: ensure that the form is correctly filled
   if (empty($rcode)) {
    array_push($errors, "Code is required");
   }

   // Redeem Code if there are no errors in the form
   if (count($errors) == 1) {

    $query = "SELECT * FROM redeem_codes WHERE rcode='$rcode' LIMIT 1";
   $results = mysqli_query($db, $query);

    if (mysqli_num_rows($results) == 0) {

     $code_found = mysqli_fetch_assoc($results);
    if ($code_found['status'] == 'unused') {

      header('location: register.php');

    }else{
     echo "Code not found";
      header('location: index.php');
    }
    }
      else {
     echo "error";
     //array_push($errors, "Invalid Code");
    }
   }
  }

  ?>
</head>
<body>
  <?php foreach( $err as $line ) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>
  <div class="widget">
  <h3>Redeem Code</h3>
<form method="post" action="redeem.php" role="form" class="contactForm text-left">


<div class="form-group">
<input type="text" name="rcode" class="form-control" id="rcode" placeholder="Enter code" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>

<div class="text-left"><button type="submit" class="btn btn-primary btn-lg" name="redeem_btn">Redeem</button>
</div>

</form>
</div>
</body>
</html>
  • `if (count($errors) == 1)` will be true if there _is_ an error, not if there's no errors. – M. Eriksson Aug 21 '18 at 05:11
  • What is the problem you are facing? and as suggested by Magnus Eriksson use 'if (count($errors) == 0)' – Javed Sayyed Aug 21 '18 at 05:13
  • You're also missing a `}` before the last `else {` in your code, which should give you a parse error. – M. Eriksson Aug 21 '18 at 05:16
  • @Javed : Currently, all conditions get me to a blank page with URL (on click submit button without enter code/entered wrong code/entered correct code). I want code should check from MySQL table and change status into next column, if found unused. and redirect user to registration.html – Umesh Kashyap Aug 21 '18 at 05:36
  • check if you have any errors by print_r($errors) – Javed Sayyed Aug 21 '18 at 05:46
  • A blank page could be caused by errors. Check your servers error log to see if you get any. It could be the syntax issue I've mentioned before. You can also change how PHP displays errors and tell it to show all errors directly on the screen (this is not something you want in production though, since it can show sensitive data, but during development, you should). Here's how to show all errors and warnings: https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – M. Eriksson Aug 21 '18 at 05:47
  • So you want us to write a conditional `UPDATE` query, right? You haven't added your coding attempt for that portion. – mickmackusa Aug 21 '18 at 05:52
  • I don't see a field in your html form with `name="status"`. That `$_POST['status']` should cause problems. – mickmackusa Aug 21 '18 at 05:57
  • Couple of pointers; `select *` is not useful for debugging (in fact it's not useful ever). And LIMIT without ORDER BY is fairly meaningless – Strawberry Aug 21 '18 at 06:18
  • @mickmackusa "status" is only found and checked in MySQL table next to code value and change from "unused" to "used". If this value shows "used" then return/echo "Code already used". – Umesh Kashyap Aug 21 '18 at 09:29
  • Error received: Notice: Undefined variable: err in /Applications/MAMP/htdocs/connection_check/redeem-code.php on line 68 Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/connection_check/redeem-code.php on line 68 – Umesh Kashyap Aug 21 '18 at 10:37
  • Move `$err = array();` outside of your condition block. – mickmackusa Aug 24 '18 at 11:47

0 Answers0