bertrc Posted November 20, 2022 Share Posted November 20, 2022 I have two files index.php and cocoa_type_chocolate.php I have 3 links are dark, milk, white chocolate. I want one query when I press one link but I don't now if I need to use if , else if else if. index.php <a href="cocoa_type_chocolate.php?type_chocolate=dark">Dark</a> <a href="cocoa_type_chocolate.php?type_chocolate=white">White</a> <a href="cocoa_type_chocolate.php?type_chocolate=milk">Milk</a> cocoa_type_chocolate.php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='dark'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='milk'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='white'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/ Share on other sites More sharing options...
bertrc Posted November 20, 2022 Author Share Posted November 20, 2022 New Members 2 Posted just now I have two files index.php and cocoa_type_chocolate.php I have 3 links are dark, milk, white chocolate. I want one query when I press one link but I don't know if I need to use if , else if else if. index.php <a href="cocoa_type_chocolate.php?type_chocolate=dark">Dark</a> <a href="cocoa_type_chocolate.php?type_chocolate=white">White</a> <a href="cocoa_type_chocolate.php?type_chocolate=milk">Milk</a> cocoa_type_chocolate.php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='dark'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='milk'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='white'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602770 Share on other sites More sharing options...
bertrc Posted November 20, 2022 Author Share Posted November 20, 2022 I have two files index.php and cocoa_type_chocolate.php I have 3 links are dark, milk, white chocolate. I want one query when I press dark, another query when I press white, and another one when I press milk, but I don't know if I need to use if , else if else if. with method get or post index.php <a href="cocoa_type_chocolate.php?type_chocolate=dark">Dark</a> <a href="cocoa_type_chocolate.php?type_chocolate=white">White</a> <a href="cocoa_type_chocolate.php?type_chocolate=milk">Milk</a> cocoa_type_chocolate.php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "P-11fl32fg14", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='dark'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "P-11fl32fg14", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='milk'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "P-11fl32fg14", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='white'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602773 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 I removed the passwords from your posts. You can do the reformatting yourself if you want people to read them. You do seem to be connecting to the DB server 3 ties in same file though - once is sufficient. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602774 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 try this for cocoa_type_chocolate.php <?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); if (isset($_GET['type_chocolate'])) { $res = $mysqli->prepare("SELECT c.id_chocolate , c.name_chocolate , t.id_type_chocolate , t.type_chocolate FROM chocolate c INNER JOIN type_chocolate t ON c.id_chocolate = t.id_type_chocolate WHERE type_chocolate = ? "); $res->bind_param('s', $_GET['type_chocolate']); $res->execute(); $res->bind_result($id_chocolate, $name_chocolate, $id_type_chocolate, $type_chocolate) ; echo '<pre>'; while ($res->fetch()) { printf("%s %s %s %s\n", $id_chocolate, $name_chocolate, $id_type_chocolate, $type_chocolate); } echo '</pre>'; } PS not sure if the join is right as I don't know your table structure edit - forgot the execute (I hate mysqli - too many things to do. Give me PDO any day) Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602775 Share on other sites More sharing options...
bertrc Posted November 20, 2022 Author Share Posted November 20, 2022 (edited) Parse error: syntax error, unexpected token "<", expecting end of file in /var/www/html/cacao/cacao_type_chocolate.php on line 8 I am beginer with php and my english is not very good I am catalan I have one error, thank you How to put the get in the sentence where dark $query = "SELECT xocolata.id_xocolata,xocolata.nom_xocolata,tipus_xocolata.id_tipus_xocolata, tipus_xocolata.tipus_xocolata ,tipus_xocolata.id_xocolata FROM xocolata INNER JOIN tipus_xocolata ON xocolata.id_xocolata =tipus_xocolata.id_tipus_xocolata WHERE tipus_xocolata='dark'"; I don't know php oriented objects I have the database in catalan, I translate in english Edited November 20, 2022 by bertrc Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602776 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 I get no syntax errors when I run it. What's your code now? (Preferably in a readable format) Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602777 Share on other sites More sharing options...
bertrc Posted November 20, 2022 Author Share Posted November 20, 2022 arse error: syntax error, unexpected token "<", expecting end of file in /var/www/html/cacao/cacao_type_chocolate.php on line 8 I am beginer with php and my english is not very good I am catalan I have one error, thank you How to put the get in the sentence where dark $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='dark'"; don't know php oriented objects I have the database in catalan, I translate in english Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602778 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 You have a syntax error on line 8 and you only show one line!? Sorry, can't help. Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602779 Share on other sites More sharing options...
bertrc Posted November 20, 2022 Author Share Posted November 20, 2022 (edited) <?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "P-11fl32fg14", "cacau"); if (isset($_GET['tipus_xocolata'])) { $res = $mysqli->prepare("SELECT c.id_xocolata , c.nom_xocolata , t.id_tipus_xocolata , t.tipus_xocolata FROM xocolata c INNER JOIN tipus_xocolata t ON c.id_xocolata = t.id_tipus_xocolata WHERE tipus_xocolata = ? "); $res->bind_param('s', $_GET['tipus_xocolata']); $res->bid_result($id_xocolata, $nom_xocolata, $id_tipus_xocolata, $tipus_xocolata) ; echo '<pre>'; while ($res->fetch()) { printf("%s %s %s %s\n", $id_xocolata, $nom_xocolata, $id_tipus_xocolata, $tipus_xocolata); } echo '</pre>'; } ?> Parse error: syntax error, unexpected token "<", expecting end of file in /var/www/html/cacau/cacau_tipus_xocolata.php on line 6 Edited November 20, 2022 by bertrc Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602780 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 I edited my code just after posting but you evidently were quick off the mark and missed the changes in your copy. $res->bind_param('s', $_GET['tipus_xocolata']); $res->bid_result($id_xocolata, $nom_xocolata, $id_tipus_xocolata, $tipus_xocolata) ; was changed to $res->bind_param('s', $_GET['tipus_xocolata']); $res->execute(); // added $res->bind_result($id_xocolata, $nom_xocolata, $id_tipus_xocolata, $tipus_xocolata) ; // changed bid_result to bind_result But I don't see where the "<" is coming from. Unexpected end-of-file is usually the result of a missing end "}" or unclosed quotes. * * * * * * As for the where... 'dark', that's done by the placeholder "?" in the query and the bind_param to replace the placeholder with the value when the query is executed Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602795 Share on other sites More sharing options...
Phi11W Posted November 21, 2022 Share Posted November 21, 2022 23 hours ago, bertrc said: mysqli_connect("localhost", "root", "*****", ... DO NOT use the root user for Applications. ALWAYS create a dedicated account for each Application and grant that account the correct privileges in the database. Why? You should always keep the biggest and best tools in the toolbox for yourself, because it will be you cleaning up the mess made by other people and processes. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/315560-a-hrefcocoa_type_chocolatephptype_chocolatedarkdark/#findComment-1602824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.