Aric Kuter Aric Kuter - 5 months ago 15
SQL Question

Adding values to a mysql database using php not working?

This is the index.php file:

<?php
//Attempt MySQL server connection. Assuming you are running MySQL
//server with default setting (user 'root' with no password)
$link = mysqli_connect("localhost", "root", "MiggKuter5049", "ClientDataDB");

// Check connection
if($link === false){
die("Connection failed: " . mysqli_connect_error());
}

// Attempt select query execution
$sql = "SELECT * FROM ClientLogin";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Email</th>";
echo "<th>Username</th>";
echo "<th>Password</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records found!";
}
} else{
echo "Error could not execute query: " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

<head>
<meta charset="UTF-8">
<title>Create New User</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label>First Name:</label>
<input type="text" name="FirstName"/>
</p>
<p>
<label>Last Name:</label>
<input type="text" name="LastName"/>
</p>
<p>
<label>Email Address:</label>
<input type="text" name="Email"/>
</p>
<p>
<label>Username:</label>
<input type="text" name="Username"/>
</p>
<p>
<label>Password:</label>
<input type="text" name="Password"/>
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>


And this is the insert.php file:

<?php
//Attempt MySQL server connection. Assuming you are running MySQL
//server with default setting (user 'root' with no password)
$link = mysqli_connect("localhost", "root", "MiggKuter5049", "ClientDataDB");

//Check connection
if($link === false)
{
die("Connection failed: " . mysqli_connect_error());
}

//Get user inputs
$firstname = $_POST['FirstName']);
$lastname = $_POST['LastName']);
$email = $_POST['Email']);
$username = $_POST['Username']);
$password = $_POST['Password']);

// attempt insert query execution
$sql = "INSERT INTO ClientLogin (FirstName, LastName, Email, Username, Password) VALUES ('$firstname', '$lastname', '$email', '$username', '$password')";
if(mysqli_query($link, $sql)){
echo "User added successfully!";
} else{
echo "Error, could not execute query: " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>


I cannot for the life of me find my mistake. When you click the submit button it creates another entry in the table but all blank, none of the inputted values are used. Please help :)

Answer

It looks like you have a unnecessary paren around around your POST statement when retrieving the user input in your insert.php file.

Should look like this

$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$email = $_POST['Email'];
$username = $_POST['Username'];
$password = $_POST['Password'];