-1
<?php

$name_user = $_POST['name'];
$city_user = $_POST['the_city'];

$conn = mysqli_connect('127.0.0.1','root','123','people');

mysqli_query($conn, "INSERT INTO people_data (name, the_city) VALUES ('$name_user','$city_user')");

echo "Data has been added:";
echo "<br/>";
echo "Name : $name_user";
echo "<br/>";
echo "The city ";

switch ($city_user)
{
    case "01":
        echo "London";
        break;
    case "02":
        echo "New York";
        break;
    case "03":
        echo "Bali";
        break;
    default:
        echo "Unknown City";
}
?>

I have run the code, but it returns error: Notice: Undefined index: the_city in /opt/lampp/htdocs/add.php on line 7

I see the name is added, but the city is still blank in the table people_data. I guess the error is in switch statement, but I do not know where's the error.

Expected result in table people_data: the_city contains value like 01 or 02 or 03

I forgot to add the html file, the html file:

<!DOCTYPE HTML>
<HTML>
<body>
<p>
    Add Data
    <br/>
    <form action="add.php" method="post">
        Name : <input type="text" name="name" /><br/>
        The city :
        <select>
            <option value = "01">London</option>
            <option value = "02">New York</option>
            <option value = "03">Bali</option>
        </select>
        <input type="submit" value="Send">
    </form>
</p>
</body>
</HTML>

I run the html file..

  • 1
    _I guess the error is in switch statement_. You don't need to guess - it is telling you where the error is. Duplicate - https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined – waterloomatt Nov 17 '19 at 18:42
  • You are trying to access `the_city` from the post array. It doesn't exist. You need to figure out why it doesn't exist. – waterloomatt Nov 17 '19 at 18:45
  • Perhaps https://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html/17139538 might help. – Nigel Ren Nov 17 '19 at 19:20
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 17 '19 at 22:18

1 Answers1

-1

The problem is that your $_POST['the_city'] is NULL because you don't send it via POST. Your <select> tag has no reference name. The correct code would look like this.

<select name="the_city">
...
</select>

Furthermore, please consider using isset() method in PHP.

if (isset($_POST['the_city]))
{
  //your variable has been initialized
}
davidev
  • 7,694
  • 5
  • 21
  • 56