Page 1 of 2

if then command

Posted: Wed Apr 21, 2004 5:17 pm
by hward

Code: Select all

if  what_stock =  stock_num {
	header("Location: /error.html");
	exit;
	
} else {

	session_start();

}
what_stock in coming in from previous page

stock_num is out of the database


i wanted to add this to a search page that i don't want it to return any results and redirect to another page if there not the same


the page as it is now works fine but when you search a number thats not in the database it just returns the results as empty

Posted: Wed Apr 21, 2004 5:37 pm
by Weirdan

Code: Select all

if  ($what_stock ==  $stock_num) {
   header("Location: /error.html");
   exit;
   
} else { 
//................
just corrected your syntax errors

Posted: Wed Apr 21, 2004 5:50 pm
by hward
now i am getting a parser error after i add it in

Code: Select all

<?PHP 
 if  ($what_stock ==  $stock_num) &#123; 
   header("Location: /error.html"); 
   exit; 
    
&#125; else &#123; 
//................


// connect to database 

$database = "*******";
$table_name = "items";

 


$connection = @mysql_connect("localhost", "*******", "*******") 
	or die("Couldn't connect.");

$db = @mysql_select_db($database, $connection)
	or die("Couldn't select database.");

// ---------------------------- 
// create the sql statement 
$sql = "SELECT stock_num, price, cost, item_type, item_disc, photo
	FROM $table_name WHERE stock_num = ($what_stock)"; 

// execute the sql statement 
$result = mysql_query($sql, $connection) or die("problem here"); 

 
?> 

<?PHP 
while ($row = mysql_fetch_array($result)) &#123; 

// get data from the current row 
$qty = $row&#1111;"qty"]; 
$price = $row&#1111;"price"]; 
$cost = $row&#1111;"cost"]; 
$item_disc = $row&#1111;"item_disc"]; 
$photo = $row&#1111;"photo"];

?>


<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
  <?PHP 
&#125; 
?>

<div align="left">
  <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="389" height="127" id="AutoNumber1">
    <tr>
      <td width="195" height="124" rowspan="3">
      <p align="center"><img name="photo" src="/test/pictures/<?PHP echo $photo; ?>" width="151" height="128" alt=""> </td>
      <td width="194" height="1">&nbsp;Stock#<form method="POST" action="/test/do_delcontact.php"><input type="text" name="del_item" size="20"value="<?PHP echo $what_stock; ?>"></td>
    </tr>
    <tr>
      <td width="194" height="11">&nbsp;Description:</td>
    </tr>
    <tr>
      <td width="194" height="50">
      <p align="justify"><?PHP echo $item_disc; ?></td>
    </tr>
    <tr>
      <td width="194" height="29">&nbsp;Price $<?PHP echo $price; ?></td>
      <td width="194" height="29">&nbsp;Quantity :<?PHP echo $qty; ?> </td>
    </tr>
    <tr>
      <td width="194" height="28">&nbsp;Cost&nbsp; $<?PHP echo $cost; ?></td>
    </tr>
    </table>
</div>
</p>

<input type="submit" value="Delete" name="delete"></p>
</form>

</html>

Posted: Wed Apr 21, 2004 5:54 pm
by feyd
......and the error is.........?

Posted: Wed Apr 21, 2004 5:55 pm
by Weirdan
sure you have. I used
//........
to omit the code that you don't need to change.
Here is full version

Code: Select all

if  ($what_stock ==  $stock_num) {
   header("Location: /error.html");
   exit;
} else { 
   session_start();
}

Posted: Wed Apr 21, 2004 5:56 pm
by hward
Parse error: parse error in /home/kmartin/www/admin/search.php on line 88

Posted: Wed Apr 21, 2004 5:56 pm
by Weirdan
feyd wrote:......and the error is.........?
unmatched curly brace =)

Posted: Wed Apr 21, 2004 5:59 pm
by feyd
btw, hward, your table's going to look funny leaving some cells out.

Posted: Wed Apr 21, 2004 6:09 pm
by hward
ok found the error but it still does the same thing that it did before it is returning empty tables instead of redirecting

Posted: Wed Apr 21, 2004 6:11 pm
by hward
oh and on the empty table thing i mean its returning a complete empty table when the search doesn't match anything in the database

Posted: Wed Apr 21, 2004 6:13 pm
by feyd
header("Location: ...
needs full url last I checked.

Posted: Wed Apr 21, 2004 6:20 pm
by hward
got it it needed a !

if (!$what_stock == $stock_num)

Posted: Wed Apr 21, 2004 6:21 pm
by hward
thanks everyone

Posted: Wed Apr 21, 2004 6:22 pm
by Unipus
That looks like weird syntax to me, it may not always do what you expect. I think you might want:

Code: Select all

if ($what_stock != $stock_num)

Posted: Wed Apr 21, 2004 6:26 pm
by feyd
your operator precedence is screwy...
!$what_stock inverts the boolean value of $what_stock.
the page will likely redirect if $stock_num isn't 0 or 1, or if $what_stock is nonzero and $stock_num is anything.

I think what you're really looking for is

Code: Select all

if($what_stock != $stock_num)