I have a little specific case here and I'm struggling with it.
I'm trying to insert in information into database, but the situation is slightly different from the other cases I've watched.
The things I have to do is:
- Create a HTML form, and the values should come with
$_POST
request from it. - Create a credentials plus connection and database.
- Need to assign the variables which will save the values of
$_POST
, they need to be = NULL. - I need to put these
$_POST
values into "if"'s body. - All of this should be done only if
$_SERVER['REQUEST_METHOD'] == POST
.
This is my HTML Form:
<form action="" method="post">
<p>
<label for="userNameOne">User Name:</label>
<input type="text" name="user_name_one" id="userNameOne">
</p>
<p>
<label for="userNameTwo">User Phone:</label>
<input type="text" name="user_name_two" id="userNameTwo">
</p>
<p>
<label for="userEmail">Email Address:</label>
<input type="email" name="user_email" id="userEmail">
</p>
<input type="submit" value="submit" name="submit">
These are my credentials and database connection:
<?php
session_start();
$host = "localhost";
$user_name = "root";
$user_password = "";
$database = "our_new_database";
function db_connect($host, $user_name, $user_password, $database) {
$connection = mysqli_connect($host, $user_name, $user_password, $database);
if(mysqli_connect_errno()){
die("Connection failed: ".mysqli_connect_error());
}
mysqli_set_charset($connection, "utf8");
return $connection;
This is my database creation:
$foo_connection = db_connect($host, $user_name, $user_password, $database);
$sql = "CREATE TABLE user_info(
user_name_one VARCHAR(30) NOT NULL,
user_name_two VARCHAR(30) NOT NULL,
user_email VARCHAR(70) NOT NULL UNIQUE
)";
if(mysqli_query($foo_connection, $sql)){
echo "Table created successfully";
}
else {
echo "Error creating table".mysqli_connect_error($foo_connection);
}
And this is where I hardly stuck. When I try to assign the $_POST
form values, I'm getting error:
Notice: Undefined index: userNameOne Notice: Undefined index: userNameTwo Notice: Undefined index: userEmail
Also I don't know where to use this $_SERVER['REQUEST_METHOD'] == POST
.
Can you help me a little bit to finish this "mission" :).