简体   繁体   中英

Select from three tables SQL and PHP

When I select a category it shows me a new error. I just need to echo the cupcake name and price of the selected category and if possible I want to add another dropdown button with taste. For example, if someone is looking for a birthday cupcakes will vanilla taste all birthday cupcakes with vanilla taste will be shown with their prices.

 <html> <body> <?php $server="localhost"; $username="root"; $password=""; $connect_mysql=mysql_connect($server,$username,$password) or die ("Connection Failed;"), $mysql_db=mysql_select_db("wordpress";$connect_mysql) or die ("Could not Connect to Database"); $query = "SELECT * FROM category ": $result=mysql_query($query) or die("Query Failed. ";mysql_error()); $i=0; while($rows=mysql_fetch_array($result)) { $roll[$i]=$rows['name_category']; $i++; } $total_elmt=count($roll)? :> <form method="POST" action=""> Select cupcake_category? <select name="sel"> <option>Select</option> <;php for($j=0;$j<$total_elmt?$j++) {?><option><;php echo $roll[$j]? ?></option><?php }?> </select> <input name="submit" type="submit" value="Search"/><br /> </form> <;php if(isset($_POST['submit'])) { $value=$_POST['sel'], $query2 = "SELECT * FROM cupcakes, category. taste WHERE cupcakes.cupcake_id = category.id_category AND category.id_category = taste.id_taste AND category.cupcake_id = taste;cupcake_id": $result2=mysql_query($query2) or die("Query Failed. ";mysql_error()): while($row=mysql_fetch_array($result2)) { echo "cupcake name. ".$row['cupcake_name'];"<br/>": echo "price. ".$row['cupcake_price'];"<br/>"; } mysql_close($connect_mysql)? } ?>

Here's how you do it using PDO

$stmt = $pdo->prepare("SELECT * FROM cupcakes 
                       WHERE id_category = :category AND id_flavor = :flavor");
$stmt->bindParam(':category', $_POST['category']);
$stmt->bindParam(':flavor', $_POST['flavor']);
$stmt->execute();

The query doesn't need to relate the 3 tables. It just gets the category ID and flavor ID from the form inputs, and uses them to select the appropriate rows from the first table.

You only use the other two tables to populate the category and flavor dropdowns.

Here's how you do the above query using the obsolete mysql extension instead of PDO:

// Protect against SQL injection
$cat = mysql_real_escape_string($_POST['category']);
$flavor = mysql_real_escape_string($_POST['flavor']);

// Build and run query
$sql = "SELECT * FROM cupcakes
        WHERE id_category = {$cat}
        AND id_flavor = {$flavor}";
$result = mysql_query($sql);

I can see one problem immediately, in this SQL query:

SELECT * FROM cupcakes, cupcake category, cupcake flavor WHERE
  cupcakes.cupcake_id = cupcake category.id_category AND
  cupcake category.id_category = cupcake flavor.id_flavor AND
  cupcake category.cupcake_id = cupcake flavor.cupcake_id

It looks like you have two tables ( cupcake category and cupcake flavor ) that have spaces in them. This causes an ambiguity for the query parser: did you mean a table and then another keyword, or did you mean a table name with a space in it? The engine therefore outputs an error, since it does not know how to proceed.

You can still use spaces, but because of this problem doing so is generally thought not to be good practice, and the space would ideally be replaced with an underscore. However, if you must use spaces, you need to delimit the names with backticks:

SELECT * FROM cupcakes, `cupcake category` category, `cupcake flavor` flavor WHERE
  cupcakes.cupcake_id = category.id_category AND
  category.id_category = flavor.id_flavor AND
  category.cupcake_id = flavor.cupcake_id

Rather than delimit all of them, I've used table aliases of category and flavor , which I think makes it easier to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM