-1

This might be a dumb problem. My php code gives me below error.

Notice: Undefined variable: country in line 19

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "titan";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['country'])){ $country = $_POST['country']; }
if(isset($_POST['name'])){ $name = $_POST['name']; }
if(isset($_POST['tel'])){ $name = $_POST['tel']; }

$sql = "INSERT INTO orders (country, name, tel)
VALUES ($country, $name, $tel)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

Thanks in advance.

Chami M
  • 146
  • 1
  • 7
  • don't forget to harden against sql injection. Note that it's far better to do it the first time than to believe, likely wrongly, that you'll get to it before it's discovered. – erik258 Dec 11 '18 at 18:00

2 Answers2

-1

After isset $_POST[tel] why it is $name not $tel

-1

your question already answered here "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP

additionally :

no it's not dumb, but to be honest it's a beginner common mistakes

you almost do it this right, by doing

if(isset($_POST['country'])){ $country = $_POST['country']; }

however it will not completely solve your problem. there are two ways to solve it, depend on your preferences.

first solutions is you can save it using default value

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "titan";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//default values, coz you will used it whetter isset true or not in line 25
$country = '';
$name = '';
$tel = '';

if(isset($_POST['country'])){ $country = $_POST['country']; }
if(isset($_POST['name'])){ $name = $_POST['name']; }
if(isset($_POST['tel'])){ $tel = $_POST['tel']; } //you should use $tel instead $name

// use escaped double quote
$sql = "INSERT INTO orders (country, name, tel)
VALUES (\"$country\", \"$name\", \"$tel\")";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

alternatively, the second solution is to only insert if all the data valid

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "titan";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//only insert if all data valid!
if(isset($_POST['country']) && isset($_POST['name']) && isset($_POST['tel'])) {
    $country = $_POST['country']; }
    $name = $_POST['name']; }
    $tel = $_POST['tel']; } //you should use $tel instead $name

    $sql = "INSERT INTO orders (country, name, tel)
    VALUES (\"$country\", \"$name\", \"$tel\")";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
} else {
    echo "Error: data not valid";
}

$conn->close();

hopefully it can help you in some way,

have a nice day!

hifebriansyah
  • 341
  • 1
  • 6
  • Hi, thanks for the quick response. I get below error after adding this.**Error: INSERT INTO orders (country, name, tel) VALUES (, , ) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' , )' at line 2** – Chami M Dec 11 '18 at 18:16
  • You should use prepared statements, but if not at least put quotes around the string values in the SQL statement. – Nigel Ren Dec 11 '18 at 18:22
  • @NigelRen gave the right answer. i have update my snippet – hifebriansyah Dec 11 '18 at 18:27
  • @hifebriansyah thanks guys. It works. – Chami M Dec 11 '18 at 18:36