Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mywebsite";

$fname=val($_POST["fname"]);
$lname=val($_POST["lname"]);
$email=val($_POST["email"]);
$id=val($_POST["id"]);

function val($data){
	$data=trim($data);
	$data=stripslashes($data);
	$data=htmlspecialchars($data);
	return $data;
	}
$conn=new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql="UPDATE users SET firstname='$fname',lastname='$lname',email='$email' WHERE id='$id'";
if($conn->query($sql)===TRUE){
	header ("location:delete.php");
	}
	else{
		echo "error updating records".$conn->error;
		}
		$conn->close();
		?>


What I have tried:

I have checked all the code again and but it is not updating database and also not showing any error
Posted
Updated 6-Oct-21 13:44pm

1 solution

Quote:
I have checked all the code again and but it is not updating database and also not showing any error

Because of the way you build your SQL request, its syntax depends on the contain of variables, thus making impossible to know the real request as seen on SQL server side. This problem is known as 'SQL Injection'.

I would:
PHP
$sql="UPDATE users SET firstname='$fname',lastname='$lname',email='$email' WHERE id='$id'";
echo "SQL request:".$sql;

Just to see what is the request.

Or better:
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900