0

I am trying to create a drop down menu for users to insert their orders into my database system.

I am having trouble creating the drop down menu, but I get the general idea of what I am supposed to do. I am new to this, so bear with me please. Here is my uml if it helps:enter image description here

<?php

$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";
if(isset($_POST['submit'})){

$sql = "SELECT size FROM size";
$result = mysqli_query($sql); 
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
    $opt .= "option value='{$row['size']}' >{$row['size']}></option>";
}

$opt .="</select>";

if ($conn->query($sql)){
    $msg = "Data inserted successfully";
} else{
    $msg = "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Pizza</title>
</head>
<body>
<form action="size.php" method="POST">
    <select name="size">

    </select>
    <button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

I know my html code is wrong or incomplete. How do I make it so that multiple drop down menus are created and how do I use multiple $sql insert statements in one script in order to submit my data into my database? Any help would be appreciated.

Nandhi Kumar
  • 343
  • 1
  • 11
the_crouton
  • 53
  • 1
  • 8

1 Answers1

0
  1. You have few typos. In line 13, if(isset($_POST['submit'})){ should be if(isset($_POST['submit'])){. At options while loop, you should have had $opt .= "<option value='{$row['size']}' >{$row['size']}></option>"; (missing "<")

  2. Also, there is some redundant code. mysqli_query() and $conn->query() does the same (see this SO tread). Simply OOP vs Procedural syntax.

You can remove

if ($conn->query($sql)){
    $msg = "Data inserted successfully";
}
else{
     $msg = "Error: " . $sql . "<br>" . $conn->error;
}

Afterwards, something like this should be enough

<?php

$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";
if(isset($_POST['submit'])){

    $sql = "SELECT size FROM size";
    $result = mysqli_query($sql); 
    $opt = "<select name='size'>";
    while($row = mysqli_fetch_assoc($result)) {
        $opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
    }

    $opt .="</select>";
    $conn->close();
}
?>

However, as I understand here, you have an empty form, which on submit gets all the sizes. If you wanted to have it like php get sizes > generate form > submit form and redirect to next step you should do it like or similar to this (and remove the if $_POST['submit'] condition from the php part)

<!DOCTYPE html>
<html>
<head>
    <title>Pizza</title>
</head>
<body>
<form action="submit-chosen-size.php" method="POST">
    <label for="size">Select your size:</label>
    <?php echo($obj); ?>
    <button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

EDIT: Please, try with this code. It's very similar to code above but will help with finding the problem

<?php
$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";

$sql = "SELECT size FROM size";
$result = mysqli_query($sql); 
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
    $opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
    echo $row['size']; // you can remove this later
}
$opt .="</select>";
echo $obj; //you can remove this later
$conn->close();
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Pizza</title>
    </head>
    <body>
        <form action="submit-chosen-size.php" method="POST">
            <label for="size">Select your size:</label>
            <?php echo(isset($obj) ? $obj : "Empty"); ?>
            <button type ="submit" name="submit"> Submit</button
        </form>
    </body>
</html>
kravis
  • 380
  • 2
  • 15
  • Thank you, i did try that, it is almost done, the drop down menu I am trying to get will say pizza sizes like small, medium, and large as if you are ordering a pizza online, and I cannot get that to pull from my database with the code. – the_crouton Dec 05 '17 at 05:58
  • So, you are accessing the file, which contains both the php and the html parts, right? What error is there? Empty select or something else? – kravis Dec 05 '17 at 06:42
  • im trying to access this table https://imgur.com/9OcROqy from my database and turn it into a drop down menu like this https://imgur.com/CfzY3cs, the code you fixed for me was this https://imgur.com/py1ZcVB which is almost there I don't believe there are any errors that it is reporting, I just need it into a dropdown menu with the size table from my database if that makes any sense? – the_crouton Dec 05 '17 at 14:20
  • I updated my answer, please, try again. Let's try to find the problem :) – kravis Dec 06 '17 at 11:23