• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

PHP Specific Selection

Hi experts,

I'm stuck on a problem and have no idea how to go about it.

Here is what I'm trying to do.

I'm trying to write an SQL statement that will extract say the name and price from the products table but it will only display the information for a specific product code that I type in.

Example
Product Code = HT400

Display
Code     |     Name      | Price
HT400          Bike          $50

I hope my question is clear.

Thanks in advance,

Rick.
0
Ronniel Allan Castanito
Asked:
Ronniel Allan Castanito
  • 4
  • 3
1 Solution
 
kivan24Commented:
Something like that?
select code, name, price from product_table where code='HT400'

Open in new window

0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Thanks for the fast reply!

Something like?

<?php
require_once("dbconn.php");
$name = $_POST["productCode"];
$sql = "select * from product ";
$sql = $sql . "where productCode = HT400'";
$rs = mysql_query($sql, $dbConn)
or die ('Problem with query' . mysql_error()); ?>
<body>
<?php if (mysql_num_rows($rs)>0){ ?>
<table width="700" border="1" cellpadding="10">
<tr><td>Product Code</td><td>Name</td><td>Price</td></tr>
<?php while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["productCode"]?></td>
<td><?php echo $row["Name"]?></td>
<td><?php echo $row["Price"]?></td>
</tr>
<?php } ?></table>
<?php }
else {?> <p>Code does not exsist with <?php echo HT400 ?>
in autoservice database.</p>
<?php } ?> </body></html>

Open in new window

0
 
kivan24Commented:
line #5
$sql = $sql . "where productCode = '".$name."'";

Open in new window


lime #20
else {?> <p>Code does not exsist with <?php echo $name ?>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Thanks!,

I think I'm on the right track. I threw in that code in with the rest of my code.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>testing php</title>
</head>
<?php
$conn = mysql_connect("localhost", "test", "test1");
mysql_select_db("warehouse", $conn)
or die ('Database not found ' . mysql_error() );?>
<?php	
$name = $_POST["productCode"];
$sql = "select * from product ";
$sql = $sql . "where productCode = '".$name."'";
$rs = mysql_query($sql, $dbConn)
or die ('Problem with query' . mysql_error()); ?>
<body>
<?php if (mysql_num_rows($rs)>0){ ?>
<table width="700" border="1" cellpadding="10">
<tr><td>Product Code</td><td>Name</td><td>Price</td></tr>
<?php while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["productCode"]?></td>
<td><?php echo $row["name"]?></td>
<td><?php echo $row["price"]?></td>
</tr>
<?php } ?></table>
<?php }
else {?> <p>Code does not exsist with <?php echo $name ?>p>
<?php } ?> </body></html>
<?php }
mysql_close($conn); ?>
</table></body></html>

Open in new window


I'm running into the problem of the database not loading up at all now.
0
 
kivan24Commented:
What error displaying?
check line #9 connection parameters $conn = mysql_connect("localhost", "test", "test1"); host user and password
0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
The Error:

 The website cannot display the page
 HTTP 500  
   Most likely causes:
•The website is under maintenance.
•The website has a programming error.

The host user and password is right, I've been using the same while testing some other PHP stuff.
0
 
Ronniel Allan CastanitoIT ManagerAuthor Commented:
Still unresolved but have a better understanding now.

Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now