How to accept a PostgreSQL database quantity in a php script? - php

This is the simple snippet of code that I'm trying to edit for a school project. The problem as far as I can understand, is that its fetching the quantity right but it may not be getting stored in the variable $quantity. I've tried debugging it to no success. Also, The simple addition is giving bizarre results.
Please help. The code is as follows
<?php
include("DB.php");
$id = $_POST["flower_id"];
$add_quantity = $_POST["quantity"];
echo "FlowerID";
echo $id;
echo "<br>";
echo "Add Quantity";
echo $add_quantity;
echo "<br>";
$con = pg_connect("host=localhost dbname=kruttika user=kruttika");
$quantity = pg_query("select quantity from inventory where flower_id='$id'");
$new = $quantity;
echo "Fetched Quantity";
echo $new;
echo "<br>";
if(!$con)
echo "Error<br>";
else
{
$new_quantity = $new + $add_quantity;
echo "Total Quantity";
echo $new_quantity;
echo "<br>";
$sql = "update inventory set quantity=$new_quantity where flower_id='$id'";
echo "SQL injected";
echo $sql;
echo "<br>";
$result=pg_query($sql);
}
echo $result;
echo "<br>";
//echo "<script>location.href=\"adminindex.html\"</script>";
?>

I think the problem is:
$quantity = pg_query("select quantity from inventory where flower_id='$id'");
pg_query() doesn't return the actual value, it returns a pg_fetch_result().
You must fetch the result to get the desidered value. See the examples on the documentation linked above.

Related

ELSE condition is not working in the while loop in PHP

<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')

Pasing data/values

I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>

Displaying multiple MySQL tables in one HTML table with PHP, experiencing loop trouble

This is the code I am trying to use to display information from multiple MySQL tables in one table. I'm also trying to create an else clause if the table information doesn't exist. This is my failed attempt at it.
I keep editing this as I peck away at it but at this point I still can't get it to display the final else when the row doesn't exist.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
}
?>
you could re-write the SQL statement in your $info "SELECT * FROM Info" to "SELECT * FROM Info wHERE Item_ItemID=$row['ItemID'] ... and then delete the
if ($info_row['Item_ItemID'] = $row['ItemID'])
Plus you new a while ind your code after $info_results = mysql_query($info);
* EDITED *
then you do like this, instead of
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
try this
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
i try to edit your code but im not able to try it.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
?>
i hope this is the answer.

Added if else argument and lost variable from database

<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
while($row = mysqli_fetch_array($result))
{
if (!empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $row['Price'] * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
}
mysqli_close($con);
?>
This is returning the "Not Applicable" from the if argument every time.
I've tried placing the lines in different order and using both the price and result variable, it still only returns not applicable but without the if else argument, the rest of it works as intended and brings up a price so I know the price variable shouldn't be empty and neither should the result variable.
With it returning not applicable all the time, I assume it's not retrieving the information from the database properly any more but I can't figure out why when it works perfectly fine without the if else.
Any help would be great, Thanks.
Your logic is wrong:
if (!empty($row['Price']))
^ here
{
echo "Not Applicable";
}
should be:
if (empty($row['Price']))
{
echo "Not Applicable";
}
You also have a serious sql injection problem, you should use prepared statements or at the very least use mysqli's escaping function.
if (empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet'
.....
}
}
I think the in the if condition the negation causes the problem
Remove the exclamation mark!
It might be because you are using mysqli_fetch_array instead of mysqli_fetch_assoc. http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
You should also start using real_ escape_ string on your parameters, or do it in the right way with parameterized prepared statement. http://www.php.net/manual/en/mysqli.prepare.php
<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
$row = (mysqli_fetch_array($result));
$a = $row[3];
if (empty($a))
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>";
echo "N/A";
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $a * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
mysqli_close($con);
?>
Well I found this workaround, instead of using while I just got rid of it and assigned the price from the database into its own variable meaning there's no conflict with the if else statement. Works like a charm... now to look at prepared statements :S

how do i display this columns side by side

im having some problem here. basically, i want to compare columns. so i fetched object and the comparing results appeared just as expected. however, it does not return the compare value anymore after i added the fetch_array to view the current table hoping that the compare value would appear beside the compare value. is there any way i could run the compare code and make it appear the table? i tried a query but it would only work in MySQL and not PHP.
$query = "SELECT * FROM system_audit"; $result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'];
echo $row['Type'];
echo $row['Setting'];
echo $row['Value'];
}
while ($row = mysql_fetch_object($result)) {
if($row->Setting != $row->Value) {
echo "X";
} else {
echo "O";
}
}
Your code contains a lot of echo's that have no use. I would suggest learning PHP a bit more.
Your compare is wrong, this should work :
$query = "SELECT * FROM system_audit";
$result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'] . "<br>";
echo $row['Type'] . "<br>";
echo $row['Setting'] . "<br>";
echo $row['Value'] . "<br>";
if($row['Setting'] != $row['Value']) {
echo "X" . "<br>";
}
else {
echo "O" . "<br>";
}
echo "<br>";

Categories