0

So if the Material number doesn't match the Input with the data stored in database it should redirect to requestfailure.php page and it doesn't store it in database. If it exists it should redirect to requestsuccess.php and store the value in database.

The problem here is, It works fine but the redirection is not working as im always getting redirected to requestsuccess.php no matter it gets stored or not.

<?php

if(isset($_POST['submit'])) {

include_once 'dbh.inc.php';

$mnum = mysqli_real_escape_string($conn, $_POST['mnum']);
$mcat = mysqli_real_escape_string($conn, $_POST['mcat']);
$mmess = mysqli_real_escape_string($conn, $_POST['mmess']);
$mcode = mysqli_real_escape_string($conn, $_POST['mcode']);
$mdev = mysqli_real_escape_string($conn, $_POST['mdev']);

//error handlers
//check for empty fields

if(empty($mnum) || empty($mcat)){

    header("Location: ../index.php?index=empty");
    exit(); 

} else {

    //check if the request already exists

    $sql = "SELECT * FROM pending WHERE Material = '$mnum'";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

    if($resultCheck > 0) {

    header("Location: ../index.php?index=requestalreadyexists");
    exit();                 

    } else { 

        $sql = "INSERT INTO pending (Material, ShopFloor, Message, Code, Device, Description, Location) SELECT '$mnum','$mcat','$mmess','$mcode','$mdev', Description, Location FROM list WHERE Material = '$mnum';";

        mysqli_query($conn, $sql);

        header("Location: ../requestsuccess.php");

    }

}
} else {

    header("Location: ../requestfailure.php");
    exit();

}

Also if anyone can tell me how to debug this and where my if / else is going wrong.

Ajithesh N
  • 66
  • 9
  • i think problem is here - $sql = "INSERT INTO pending (Material, ShopFloor, Message, Code, Device, Description, Location) SELECT '$mnum','$mcat','$mmess','$mcode','$mdev', Description, Location FROM list WHERE Material = '$mnum';"; – Sanjay Kumar Jun 15 '18 at 11:49
  • Description, Location is should be $Description & $Location and also define them – Sanjay Kumar Jun 15 '18 at 11:50
  • @SanjayKumar clearly $Description and $Location don't exist. Those are the columns they are copying from the select statement. – Devon Bessemer Jun 15 '18 at 11:52
  • The problem is not the database as the logic is properly working (for db). eg. if material number exists it inserts in db, if it doesn't it wont and if it doesn't exist, it should redirect to requestfailure.php but in both cases its redirecting to requestsuccess.php – Ajithesh N Jun 15 '18 at 11:55
  • SO it required debugging can you try to put echo before $sql = "INSERT INTO and comment the redirection. run the code and get the string try to execute in SQL – Bikesh M Jun 15 '18 at 12:04
  • https://www.w3schools.com/code/tryit.asp?filename=FSC5DG0QWQWZ – Sanjay Kumar Jun 15 '18 at 12:08

0 Answers0