I have created a MySQL database that currently has just one table called "users". Fields are: id, first_name, last_name, username, email, about, location, website. I am inserting the data through an auto submitted HTML form with PHP.
The insertions are happening with no problem, but what it is tripping me up, is that when the insertion is made through the HTML form, the data inside is not searchable. For example if I try to perform a search query to find one user with a matching email or username, the user is not found even though that it does exists in the database. It's only when I search for a user with his ID (which is an auto-increment and inserted automatically by MYSQL) that the search query finds the user. Below is my code. I have striped everything from CSS to verification and security functions, in order to rule out factors that might be causing this.
<?php
if (isset($_POST['submit'])) {
//$user = new User();
$first_name =$_POST["first_name"];
$last_name =$_POST["last_name"];
$email =$_POST["email"];
$username =$_POST["username"];
$connection=mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$sql = "INSERT INTO users ";
$sql .= "(first_name, last_name, email, username) ";
$sql .= "VALUES ('{$first_name}', '{$last_name}', '{$email}', '{$username}')";
$result = mysqli_query($connection, $sql);
if ($result) {
echo "Insertion succeed";
} else {
echo "Insertion failed";
}
}
?>
<h2>Sign up</h2>
<form action="sign_up2.php" method="post"/>
<ul>
<li>
First_name: <input type="text" name="first_name" value=" "/>
</li>
<li>
Last_name:<input type="text" name="last_name" value=" "/>
</li>
<li>
Email:<input type="text" name="email" value=" "/>
</li>
<li>
Username:<input type="text" name="username" value=" "/>
</li>
<li>
Password:<input type="password" name="password" value=""/>
</li>
<li>
<input type="submit" name="submit" value="Sign in" />
</li>
</ul>
</form>
On the other hand, if the data is inserted to the database straight through the MSQL query script avoiding the HTML table and $_POST super globals, like this...
$connection=mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$sql = "INSERT INTO users ";
$sql .= "(first_name, last_name, email, username) ";
$sql .= "VALUES ('John', 'Doe', 'John@gmail.com', 'john_d')";
$result = mysqli_query($connection, $sql);
if ($result) {
echo "Insertion succeed";
} else {
echo "Insertion failed";
}
....all of the data inside all the fields can be used to find and match any existing user: email, username, first_name, etc, not just exclusively with the ‘ID’ field as I mentioned before that happen when the insertion is made through the HTML form.
I am using WAMP server 2.4, MySQL version is 5.6.12 and PHP version is 5.4.12
I hope I was clear with my description of the problem and mostly I hope that you can help me to figure out why is this happening.
Many thanks in advance!
Arturo.