简体   繁体   中英

search script php mysql

I have a search box where users can search for products, However I am stuck on one problem.

I would like to use single query to search multiple tables and then show the results. The problem is after the search results show up, when the users clicks on one of the products he should see the item detail.

I am able to do this with single table just passing the id variable via url to the second page and echoing all the rows on the second page. How can I do this with multiple table? The columns don't match in number. can someone give me a hint on how to show the details for multiple tables? Just like ebay or amazon does.

 $stmt = $mysqli->prepare("SELECT * FROM TABLE1, TABLE2, WHERE TABLE1.title = ? OR TABLE2.title = ?");
 $stmt->bind_param('ss', $title, $title);
 $stmt->execute();

try this

     $stmt = $mysqli->prepare("SELECT t1.* ,t2.* FROM TABLE1 t1, TABLE2 t2 
                               WHERE t1.title = ? OR t2.title = ? ");

For best result I think you need to redesign those products table. Try the following approach. create one table for product_categories with at least two fields, category_id,category_name . then create another table for product_lists . with at least three fields product_id, category_id, product_name .Note category_id is foreign key on product_lists table. Then create another table for product_details with at least three fields detail_id,product_id,details .Note product_id is foreign key on product_details .

This is just example you can do more than this. Then you can join the above table using their foreign keys and primary key, you solve the problem of multiple tables ids. As you see on your following page the id will be useful on product_lists table.

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