if then command

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

if then command

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

if  ($what_stock ==  $stock_num) {
   header("Location: /error.html");
   exit;
   
} else { 
//................
just corrected your syntax errors
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

......and the error is.........?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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();
}
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

Parse error: parse error in /home/kmartin/www/admin/search.php on line 88
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

feyd wrote:......and the error is.........?
unmatched curly brace =)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

btw, hward, your table's going to look funny leaving some cells out.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post 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
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

header("Location: ...
needs full url last I checked.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

got it it needed a !

if (!$what_stock == $stock_num)
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

thanks everyone
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
Post Reply