Having trouble with data validation in Php for a online store

I have to create a website for a school project and I am making a guitar shop. The problem is I have having trouble with data validation when a user selects the amount they want to buy. My current code is

    <meta charset="UTF-8">
 <h1>Welcome to the Acoustic Guitar Shop!</h1>
<form action = '<?php $_SERVER['PHP_SELF'] ?>' method = 'post'>


'Guitar Type'=>"Fender FA-100 <img src=FenderFA100.jpg width='128' 
'Guitar Type'=>"Yamaha FG800 <img src=Yamaha800.jpg width='128' 
'Guitar Type'=>"Yamaha FG830 <img src=Yamaha830.jpg width='128' 
'Guitar Type'=>"Big Baby Taylor <img src=Taylor.jpg width='128' 
'Guitar Type'=>"Martin DX1KAE <img src=MartinDX1AE.jpg width='128' 
// Setting Up the Multi Dimentional Array

$Guitars=array($Guitar1, $Guitar2, $Guitar3, $Guitar4, $Guitar5);

// Creating a array for all Guitars.
echo "<center>";
echo "<table border='3' cellpadding='5' cellspacing='5'>";
echo '<th>Item</th><th>Guitar Type</th><th>Price</th><th>Quantity</th>';
//Creating borders and heading for table.

  for($i=0; $i<count($Guitars); $i++)

   $qty = "quantity" . $i;

  foreach($Guitars as $i=>$Value)

   echo "<tr><td>{$Guitars[$i] ['Item']}</td>";
   echo "<td>{$Guitars[$i] ['Guitar Type']}</td>";
   echo "<td>{$Guitars[$i] ['Price']}</td>";
   echo '<td><input type="text" name='. $qty .'></tr>';
//Creating a for loop to display the table.  
//Created html forms to allow the user to choice the amount of guitars they 
wish to purchase.              

if(array_key_exists('submit_button' , $_POST))
// IF statement that tells the program to display a html table if the user 
presses submit
  echo "<table <table border='3' cellpadding='5' cellspacing='5'>
       <th>Item</th><th>Guitar Type</th><th>Price</th><th>Extended 

//The submit_button input was valid, so it would display the invoice.  
echo 'Value entered is invalid';
//Checking data validation.

 //Creating a if statement for the submit_button.
  else {   

<b><center><input type="submit" name = "submit_button" value = "Purchase">


The main problem I have is with my last if statement, it keeps saying the value is invalid even though it is valid and I am not sure how to properly fix it.
I am new to coding so I am not sure how to fix this. I need to also display a invoice after using to display the final price of the items the user selected. How would i go about doing this?

Thanks in advance!

Change your line If(is_numeric("$qty")) to if(is_numeric($qty)) as you aren't actually comparing the variable you are actually checking the string "$qty".


If you want to get the form value for the element with the name = $qty you need to use post variables. try if(is_numeric($_POST[$qty])) This should get the textbox value with the name "quantity5".

Good luck.

You are writing the validation is if there is only one qty variable, but in fact, you have 5, named quantity1 to quantity5. You need a loop to validate each one of them. Remember you can use dynamic variable names in PHP using a syntax like $_POST[${'quatity'.$index}] to access the variables in you _POST array.