Herman Herman - 25 days ago 7
MySQL Question

How can I submit a user input in an if statement after already submitting a user input

I just made an account for this. So I've been tasked with setting up a menu that can do certain things with a MYSQL database (northwind).

I'm still fairly new to PHP, however, I have to say I'm disappointed that I can't get past the simplest part of the project.

The project simply asks "Your program should interact using plain text only. To make compiling and grading easier, do not include graphics or any other fancy interface.

Your top menu (and the tasks you are asked to implement) includes the following:


  1. add a customer

  2. add an order

  3. remove an order

  4. ship an order

  5. print pending orders with customer information

  6. restock parts

  7. exit



Now I'm supposed to loop around this menu and pick one option. Once I pick an option, I have to perform an SQL query (which I haven't gotten to yet and isn't really part of the question). Once the query stuff is done, it will loop to the menu again.

I've done this so far. It's a bit dirty, but I'm just trying to get this job done so I could refine later. Here's my code:

<?php
include "database-include.php";

$connection = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);

$error = mysqli_connect_error();
if($error != null){
$output = "<p>Unable to connect to database<p>".$error;
exit($output);
}

echo "1. add a customer <br/>
2. add an order <br/>
3. remove an order <br/>
4. ship an or0der <br/>
5. print pending orders with customer information <br/>
6. restock parts <br/>
7. exit <br/>";

echo "<form action = 'Northwind_Database_Shahbaaz_Singh.php' method='get'>";
echo "<input type = 'text' value ='' name ='choice'/>";
echo "</form>";
if (isset($_GET['choice'])){
$choice = $_GET['choice'];

echo "<br/>";
if ($choice == 1){
echo "<form action = 'Northwind_Database_Shahbaaz_Singh.php' method='get'>";
echo "<input type = 'text' value ='Customer Name' name ='customer_name'/>";
echo "<input type='submit' value='Submit'>";
if (isset($_GET['customer_name'])){
echo "success";
}
echo "</form>";
} else if ($choice == 2){
echo "2";
} else if($choice == 3){
echo "3";
} else if($choice == 4){
echo "4";
} else if($choice == 5){
= echo "5";
} else if($choice == 6){
echo "5";
} else if($choice == 7){
exit("Goodbye!");
}
}
?>


The problem I have is that after I pick a choice and type an input, the form submits the customer value, not the choice value. The only way the interface works is if I manually input the choice AND the customer value again. Is there anyway I can get the customer value to be submitted while the POST/GET knows that the choice is the same?

This would have been 100x easier to do in Java but I chose to do it in PHP for phpmyadmin support. PHP doesn't seem to have an equivalent to Java's in.nextLine.

Any help would be appreciated!

Answer

So you have lots of issues, for example trying to use PHP as Java and not using static HTML... but you did say you're new, so I'll just get to the point.

When you choose "1" as an option in your text input (which could easily be changed for a radio button or <select> tag), you are creating 2 separate forms, taking choice completely out of the environment and only sending customer-name to the server. Do this instead:

<?php
include 'database-include.php';

$connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);

$error = mysqli_connect_error();
if($error != null){
    $output = '<p>Unable to connect to database<p>'.$error;
    exit($output);    
}

echo '<ol>
    <li>Add a customer</li>
    <li>Add an order</li>
    <li>Remove an order</li>
    <li>Ship an order</li>
    <li>Print pending orders with customer information</li>
    <li>Re-stock parts</li>
    <li>Exit</li>
</ol>
<form action="Northwind_Database_Shahbaaz_Singh.php" method="get">
    <input type="text" value="'.$_GET['choice'].'" name="choice">';
if(isset($_GET['choice'])){
    $choice = $_GET['choice'];
    echo '<br/>';    

    if($choice == 1){
        echo '<input type="text" placeholder="Customer Name" name="customer_name">
        <button>Submit</button>';
        if(isset($_GET['customer_name'])){
            echo 'Success';
        }
    } else if ($choice == 2){
        echo "2";
    } else if($choice == 3){
        echo "3";
    } else if($choice == 4){
        echo "4";  
    } else if($choice == 5){
        echo "5";
    } else if($choice == 6){
        echo "5";
    } else if($choice == 7){
        exit("Goodbye!");
    }
}
echo '</form>'; 
?>

This way, you'll be only adding content to your existing form, keeping only one form and always sending all the available data.