I am having some issues figuring out how to print the actual values from a row that contains the desired column value. I have two php files, quantity.php and quantity-inquiry.php, where quantity.php has a post submit form that redirects through quantity-inquiry.php to perform the actual sql.
quantity-inquiry opening php:
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'db-connect-99k.php';
$db = new mysqli($host, $user, $pass, $databaseName);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$searchName = $_POST['name'];
$res = $db->query("SELECT * FROM inventory WHERE name=$searchName");
$searchName is set appropriately from the $_POST (I can echo it out and it prints the correct input from the quantity form field):
<p> Product Name or PPI required (both not required) </p>
<p>--------------------------------------------------</p>
<form action="quantity_inquiry.php" method="post">
Product Name: <input type="text" name="name"><br />
PPI: <input type="text" name="ppi"><br />
<input type="submit" name="submit" id="submit">
</form>
In other words if I type "car" in the form field of quantity.php, $name in quantity-inquiry.php will echo out "car" as the $name variable. The problem I am having is that using $name as my query:
$searchName = $_POST['name'];
$sql = "SELECT * FROM inventory WHERE name='".$searchName."'";
$res = $db->query($sql);
I can never seem to get the actual values. I have tried tons of stuff, for example:
while($row = $res->fetch_assoc()){
$rowName = $row['name'];
$rowPPI = $row['ppi'];
$rowQuantity = $row['quantity'];
$rowPrice = $row['price'];
}
To set variables followed by
<tr>
<td> $rowName </td>
<td> $rowPPI </td>
<td> $rowQuantity </td>
<td> $rowPrice </td}
</tr>
Doesn't actually select a the field. For example, if I KNOW that I have 'product1' in my inventory table's 'name' column, and it is set as my '$searchName' it doesn't actually select it from the table and print it out as my $rowName, $rowPPI, $rowQuantity, and $rowPrice.
I have tried a number of other things that all end up in some sort of error or incorrect value being printed out. Is my query wrong or am I setting/using my variables incorrectly?
WHERE name=$nameshould probably beWHERE name="$name"(you need a better quoting solution, but that will get you started). And then you probably also want to get the data out of your result, so something likewhile($obj = $res->fetch_object()){ echo $obj->name; }.