Im very new to php, but im trying to create a bit of code that you type in the part number and quantity you want and it returns the part number and the word "limited" if its the same "not enough" if it's less than and "in stock" if its more than, im having trouble getting it to say the word in the end column where i want it.

Here is the code, but please remember im very new to it so it might be a complete mess

<form id="search" name="form" action="stock.php" method="get">
<input type="text" name="partnum" width="50"/>
<input type="text" name="quantity" width="50"/>
<input type="submit" name="Submit" value="Search" />
</form>
    
<?php
include("namefile");

  $pt = @$_GET['partnum'] ;
  $qty = @$_GET['quantity'] ;
  
// check for an empty string and display a message.
if ($qty == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
  
// check for an empty string and display a message.
if ($pt == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

//connect to mysql
$connection = mysql_connect($host,$account,$password)
	or die ("Couldn't connect to server");
	
//select which database you want to edit
$db = mysql_select_db("dbname",$connection)
	or die ("Couln't connect to database");
	
$out = "Out of stock";
$in = "In Stock";
$exact = "Exact";	
	
// SQL Query  
$string = ("SELECT * FROM table WHERE stock >= $qty AND part = \"$pt\"")
	or die("Please enter a part number");

$qresult = mysql_query("SELECT part,stock FROM table WHERE stock >= $qty AND part = \"$pt\"");
$query = mysql_query($string) or die (mysql_error());
    $row = mysql_fetch_array($qresult);
    {
	do 
	{
	echo "<table style='font-size:10px; font-family: arial;' width=1000px>";
    echo "<tr>";
	echo "<td width='20%'>".$result[part]."</td>";
	echo "<td width='40%'>".$result[desc]."</td>";
	echo "<td width='20%'>".$result[xref]."</td>";
	echo "<td width='20%'>".$result[zref_model]."</td>";
	echo "<td width='20%'>".$result[stock]."</td>";
    echo "</tr>";
	echo "<tr><td colspan='5'><hr></td></tr>";
	echo "</table>";
	}
	while($result = mysql_fetch_array($query));
	}
	if(!$qresult || $row['stock'] < $qty) {
        echo "$out";
    }
    elseif(!$qresult || $row['stock'] > $qty) {
        echo "$in";
    }
	elseif(!$qresult || $row['stock'] = $qty) {
        echo "$exact";
	}
    

?>

Recommended Answers

All 16 Replies

Im very new to php, but im trying to create a bit of code that you type in the part number and quantity you want and it returns the part number and the word "limited" if its the same "not enough" if it's less than and "in stock" if its more than, im having trouble getting it to say the word in the end column where i want it.

Here is the code, but please remember im very new to it so it might be a complete mess

<form id="search" name="form" action="stock.php" method="get">
<input type="text" name="partnum" width="50"/>
<input type="text" name="quantity" width="50"/>
<input type="submit" name="Submit" value="Search" />
</form>

<?php
include("namefile");

  $pt = @$_GET['partnum'] ;
  $qty = @$_GET['quantity'] ;

// check for an empty string and display a message.
if ($qty == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for an empty string and display a message.
if ($pt == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

//connect to mysql
$connection = mysql_connect($host,$account,$password)
    or die ("Couldn't connect to server");

//select which database you want to edit
$db = mysql_select_db("dbname",$connection)
    or die ("Couln't connect to database");

$out = "Out of stock";
$in = "In Stock";
$exact = "Exact";   

// SQL Query  
$string = ("SELECT * FROM table WHERE stock >= $qty AND part = \"$pt\"")
    or die("Please enter a part number");

$qresult = mysql_query("SELECT part,stock FROM table WHERE stock >= $qty AND part = \"$pt\"");
$query = mysql_query($string) or die (mysql_error());
    $row = mysql_fetch_array($qresult);
    {
    do 
    {
    echo "<table style='font-size:10px; font-family: arial;' width=1000px>";
    echo "<tr>";
    echo "<td width='20%'>".$result[part]."</td>";
    echo "<td width='40%'>".$result[desc]."</td>";
    echo "<td width='20%'>".$result[xref]."</td>";
    echo "<td width='20%'>".$result[zref_model]."</td>";
    echo "<td width='20%'>".$result[stock]."</td>";
    echo "</tr>";
    echo "<tr><td colspan='5'><hr></td></tr>";
    echo "</table>";
    }
    while($result = mysql_fetch_array($query));
    }
    if(!$qresult || $row['stock'] < $qty) {
        echo "$out";
    }
    elseif(!$qresult || $row['stock'] > $qty) {
        echo "$in";
    }
    elseif(!$qresult || $row['stock'] = $qty) {
        echo "$exact";
    }


?>

end quote.

hi mate,
can u post the exact error message u r gettin.

http://www.dandesign.co.uk/test/stock.php?partnum=a9190&quantity=3&Submit=Search

where the number three is i want it to say exact or not in stock etc and not where it is at the min

ok, let's try to go step by step. i need u to stay here with for a while :D im sure i'll be able to help ya out.

first of all, tell me, what are you trying to achieve here

$string = ("SELECT * FROM table WHERE stock >= $qty AND part = \"$pt\"")
or die("Please enter a part number");

m particularly trying to understand the "or die(..)" part

also u might wanna check the following in ur code

while($result = mysql_fetch_array($query));
}
if(!$qresult || $row['stock'] < $qty) {
//I DONT THINK U NEED "" , try echo $out;
echo "$out";
}
elseif(!$qresult || $row['stock'] > $qty) {
echo "$in";
//change to echo $in;  remove quote
}
elseif(!$qresult || $row['stock'] = $qty) {
echo "$exact";
//remove quote echo $exact;
}

i want to select from database where part number is same as what is entered and return both values

ive removed the quotes and thats starting to look a lot better, i just need it to put the result ("exact") in place of the stock("3").

ive removed the quotes and thats starting to look a lot better, i just need it to put the result ("exact") in place of the stock("3").

just to clarify, u dont want to display the quantity in the table, instead, you want it to be either exact, out of stock or In stock

for example,
A9190 SENSOR ASSY 90DEG 450MM 0233170500 TTC Exact

if this is the case, u might wanna concentrate on this part of ur code.

echo "<td width='20%'>".$result[stock]."</td>";

hint: u want this row to display Exact if $row = $qty.. so on n so forth..

that is exactly what i want, ill have a mess around with the code now

that is exactly what i want, ill have a mess around with the code now

kewl,:) lemme know what happens, i'll be around for a while if you need any help.

Is your database connecting at all?

Is this an inherited program?

i dont think im getting anywhere with this, im just not knowledgable enough about php at the moment, i literally only have about 4 weeks of php knowledge.

aany help would be much appreciated

i dont think im getting anywhere with this, im just not knowledgable enough about php at the moment, i literally only have about 4 weeks of php knowledge.

aany help would be much appreciated

c if this works.

//echo "<td width='20%'>".$result[stock]."</td>";
//instead of this, try the following
echo "<td width='20%'>"; 
if ($result['stock'] == $qty){
   echo $exact; 
}
echo "</td>";
commented: really took the time to help me out +1

ur an absolute genius thanks for this its a massive help and now works great, sorry i wasnt much of a help its just im still getting used to php

thanks again

ur an absolute genius thanks for this its a massive help and now works great, sorry i wasnt much of a help its just im still getting used to php

thanks again

good luck mate but while u r at it, i think the following blocks of codes are not necessary.

$qresult = mysql_query("SELECT part,stock FROM table WHERE stock >= $qty AND part = \"$pt\"");
//AND
$row = mysql_fetch_array($qresult);

//since u r doing the same with the following code. 

$string = ("SELECT * FROM table WHERE stock >= $qty AND part = \"$pt\"")
or die("Please enter a part number");
//and while at it change $string to the following. u dont need or die() coz u r doin so under $query = mysql_query..... get what i mean?
//$string = "SELECT * FROM table WHERE stock >= $qty AND part = \"$pt\"";
$query = mysql_query($string) or die (mysql_error());

//AND of course all those if's at the very bottom aint necessary.

and i find this site, http://www.tizag.com/phpT/, very helpful for learnin n for references.

this is my new code and its works a lot better now thanks, theres still a few changes ive gotta make but the basics is there

$out = "Out of stock";
$in = "In Stock";
$limit = "Limited Stock Available";	

$qresult = mysql_query("SELECT * FROM crossref WHERE stock >= $qty AND part = \"$pt\"");
    $row = mysql_fetch_array($qresult);
{
do 
{
	echo "<table style='font-size:10px; font-family: arial;' width=1000px>";
    echo "<tr>";
	echo "<td width='20%'>".$row[part]."</td>";
	echo "<td width='40%'>".$row[desc]."</td>";
	echo "<td width='15%'>".$row[xref]."</td>";
	echo "<td width='15%'>".$row[zref_model]."</td>";
	echo "<td width='10%'>"; 
		if ($row['stock'] == $qty){
   			echo $limit;
			}
		elseif ($row['stock'] >= $qty){
   			echo $in;
			}
		elseif ($row['stock'] <= $qty){
   			echo $out;
			}
	echo "</td>";
    echo "</tr>";
	echo "<tr><td colspan='5'><hr></td></tr>";
	echo "</table>";
    
}
while($row = mysql_fetch_array($qresult));
}

this is my new code and its works a lot better now thanks, theres still a few changes ive gotta make but the basics is there

$out = "Out of stock";
$in = "In Stock";
$limit = "Limited Stock Available"; 

$qresult = mysql_query("SELECT * FROM crossref WHERE stock >= $qty AND part = \"$pt\"");
    $row = mysql_fetch_array($qresult);
{
do 
{
    echo "<table style='font-size:10px; font-family: arial;' width=1000px>";
    echo "<tr>";
    echo "<td width='20%'>".$row[part]."</td>";
    echo "<td width='40%'>".$row[desc]."</td>";
    echo "<td width='15%'>".$row[xref]."</td>";
    echo "<td width='15%'>".$row[zref_model]."</td>";
    echo "<td width='10%'>"; 
        if ($row['stock'] == $qty){
            echo $limit;
            }
        elseif ($row['stock'] >= $qty){
            echo $in;
            }
        elseif ($row['stock'] <= $qty){
            echo $out;
            }
    echo "</td>";
    echo "</tr>";
    echo "<tr><td colspan='5'><hr></td></tr>";
    echo "</table>";

}
while($row = mysql_fetch_array($qresult));
} 

end quote.

kewl, but u also might wanna keep the table head outside the loop. sorry i couldnt tell u this earlier. slipped ma mind. anyways, if u r gonna keep ur code this way, u r creating a new table each time the loop is run n ur pages MIGHT run slower as ur database gets bigger. so anyways, the best way is to play around with no fear :D it's always hard in the beginning. n now ive gotta go. so good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.