in Education by
I need to take the users input from the select box for Allergen1 once an option is selected and the user clicks submit a query will run based upon the selected value of the select box. I hope to achieve this via the use of a case and switch to specify which query to run, from this i would like to then echo the results of the query into a table. the table being "tableleft" - <?php //create connection vars $dbhost = '-'; $dbuser ='-'; $dbpass = '-'; $db = '-'; //create connection $con=mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($db); switch($_GET['Allergen1']){ case 'Wheat': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Wheat = 0"); break; case 'Spelt': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Spelt = 0"); break; case 'Kamut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Kamut = 0"); break; case 'Rye': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Rye = 0"); break; case 'Barley': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Barley = 0"); break; case 'Oats': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Oats = 0"); break; case 'Fish': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Fish = 0"); break; case 'Crustaceans': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Crustaceans = 0"); break; case 'Molluscs': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Molluscs = 0"); break; case 'Eggs': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Eggs = 0"); break; case 'Soybeans': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Soybeans = 0"); break; case 'Milk': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Milk = 0"); break; case 'Almonds': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Almonds = 0"); break; case 'Hazelnut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Hazelnut = 0"); break; case 'Walnut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Walnut = 0"); break; case 'CashewNut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE CashewNut = 0"); break; case 'PecanNut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE PecanNut = 0"); break; case 'BrazilNut': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE BrazilNut = 0"); break; case 'Pistacio': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Pistacio = 0"); break; case 'Macadamia': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Macadamia = 0"); break; case 'Peanuts': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Peanuts = 0"); break; case 'Celery': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Celery = 0"); break; case 'Mustard': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Mustard = 0"); break; case 'Sesame': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Sesame = 0"); break; case 'Sulphites': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Sulphites = 0"); break; case 'Lupin': mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Lupin = 0"); break; } ?> <!DOCTYPE html> Allergen Menu

Allergen Menu

<!-- NEED TO ECHO RESULT OF THE QUERY HERE BASED UPON THE SELECT OPTION FROM THE FORM BELOW !-->
I expect the query once ran to output via an echo to the "table left" any help with this would be appreciated. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
First you should use MySQLi or PDO_MySQL extension. And you don't need a switch case. Here a full example: <!DOCTYPE html> Query <!-- Bootstrap -->

Allergen Menu


" method="post">

Result


<?php if(isset($_POST['submit'])){ // Connect to DB $dsn = 'mysql:host=localhost; dbname=Your_DB_Name; charset=utf8mb4'; try{ $connection = new PDO($dsn, 'DB_username', 'DB_password'); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(Exception $e){ die($e->getMessage()); } // Get Input data $allergen = htmlentities(addslashes($_POST['allergen'])); // prepare query $sql = 'SELECT * FROM allergendishmenu WHERE ' . $allergen . ' = 0'; $result = $connection->query($sql)->fetchAll(PDO::FETCH_OBJ); } ?> <?php if(isset($result)){ foreach($result as $data): ?> <?php endforeach; }else{ echo '<tr>No data loaded'; } ?>
Dishname Allergens
<?php echo $data->DishName; ?> <?php echo $data->$allergen; ?>

Related questions

0 votes
    I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format? Here's what I'm doing now: mysql -u uid -ppwd -D dbname...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I try to print images with a for loop but the images dont load. The images are in the same ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I try to print images with a for loop but the images dont load. The images are in the same ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How can I access an array/object? (6 answers) Closed 3 years ago. i have a ... print_r($_POST); i can see all the variable in the right way...
asked May 22, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How can I access an array/object? (6 answers) Closed 3 years ago. i have a ... print_r($_POST); i can see all the variable in the right way...
asked May 17, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need ... there an automated way I can take the text: and change it to:...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [ ... not supply any columns. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a very large MySQL table (billions of rows, with dozens of columns) I would like to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
...