When I run this page, it says You couldn’t execute the query. I think there is a syntax problem, but I cannot see the problem.
The variables look fine when I echo them. I added the column names as a code comment. All the columns are VARCHAR.
I don’t believe it is a connection problem since I can perform SELECT AND DELETE operations from other php pages.
`<?php include “header.php”;
if(isset ($_POST [‘submit’])) {
include “config.php”;
$user_id = $_POST ['user_id'];
$fname = $_POST ['f_name'];
$lname = $_POST ['l_name'];
$usern = $_POST ['user_name'];
$sql = "UPDATE user SET first_name='$fname', last_name='$lname', username='$usern' WHERE user_id='$user_id'";
$result = mysqli_query ($conn, $sql) or die ("Query Failed");
if (mysqli_num_rows ($result) > 0) {
echo "<p style='margin:10px 0; color:red; text-align:center;'>Username already exists.</p>";
}
else{
$sql1 = "INSERT INTO user (first_name, last_name, username)
VALUES ( '{$fname}', '{$lname}' , '{$username}')";
if (mysqli_query ($conn, $sql1)) {
header ("location: {$hostname}/admin/users.php");
}
}
}`
Varun Sood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.