samedi 25 avril 2015

PHP MySQL insert file, validating form with IF-statement with OR operator

I am brand new to PHP and I am not sure if my technique is even good practice, but my code worked until I added the "|| (!is_numeric($variable)) " portions of my if-statements. I tested to code by inserting letters into the number fields but ensuring that the length of the string was correct. When I do, the code runs the else-statement and echos ".$jname."added.....etc"". the mysqli_query is commented out for testing. I have tried different versions with more or less parenthesis.Why won't is_numeric() catch my letters? Am I allowed to do multiple ! arguments in an if-statement? First question, thanks.

<?Php
header('Refresh: 3; url=../job_table.php');
require 'connect.php';  
$jname = mysqli_real_escape_string($db,$_POST['field1']);
$jname_length = 25;
$jwo = mysqli_real_escape_string($db,$_POST['field2']);
$jwo_length = 7;
$jpo = mysqli_real_escape_string($db,$_POST['field3']);
$jpo_length = 4;
$add_job_query = "INSERT INTO jobs (name, wo_no, po_no) VALUES ('$jname','$jwo','$jpo')";
if(($jname || $jwo || $jpo ) == ""){
        echo "<center><h1>Please fill all fields!</h1></center>";
}elseif(!(strlen($jname) <= $jname_length)){
        echo "<center><h1>Name must be less than or equal to ".$jname_length." characters!</h1></center>";
}elseif((strlen($jwo) != $jwo_length) || (!is_numeric($jwo))){
        echo "<center><h1>WO number must be a ".$jwo_length." digit number!</h1></center>";
}elseif((strlen($jpo) != $jpo_length) || (!is_numeric($jwo))){
        echo "<center><h1>PO number must be a ".$jpo_length." digit number!</h1></center>";
}else{
        //echo mysqli_query($db,$add_job_query);
        echo "<center><h1>".$jname." added!</h1><h1>WO number:".$jwo."</h1><h1>PO number:".$jpo."</h1></center>";
}
echo "<center><h1>Please wait.</h1></center>";
?>

Aucun commentaire:

Enregistrer un commentaire