0

I am very new to PHP and I am having some issues with validating the data against what is already stored in another table. The idea is pretty simple in that someone will enter some data against a works order and write that to the database. The data will only be written to the database if the works order number exists in a separate table.

$sqlv = "Select count (worksorderv) from validationtest where worksorderv = ('".$_POST["worksorder"]."')";
$checkwo = sqlsrv_query($conn, $sqlv, array(), array('Scrollable' => 'buffered'));

 if (sqlsrv_fetch_array($checkwo) < 1) {
  echo "That is not a valid works order.<br />";
  }else{
$sql = "INSERT INTO main_table (worksorder, part, quantity, date)
      VALUES ('".$_POST["worksorder"]."','".$_POST["part"]."','".$_POST["quantity"]."',GETDATE())";
$res = sqlsrv_query($conn, $sql);

I have tried to create a count, so if the works order does not exist within the table it will output a 0 else it exists and can be committed to the database.

if I hardcode a single number in I can get it to work, but I can not get it working using the $_POST["worksorder"] which is the form entry.

it seems to ignore and writes all entries regardless of if the count is 0 or 1

Any help is greatly appreciated!

1 Answer 1

1

sqlsrv_fetch_array() returns an array, and you are using this result in your comparison:

if (sqlsrv_fetch_array($checkwo) < 1) {

You probably want to check the actual column that holds the result of the count:

$sqlv = "Select count(worksorderv) as CNT from validationtest where worksorderv = ('".$_POST["worksorder"]."')";
// ...
$result = sqlsrv_fetch_array($checkwo);
if ($checkwo['CNT'] < 1) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.