PHP MySql Update statement

Hey there, I’m new to PHP and I’m having some problems with an update statement for an application I’m building. When I click on a client to update and then I update their data all of the data in the database for all users changes to the record I just updated. Which is NOT what I want. My code is below. Help would be appreciated!

//Grab requested Client ID for Updating
$clientid=$_GET[‘id’];
$stmt=$db->prepare(“SELECT * FROM clients where id = :id”);
$stmt->bindParam(’:id’, $clientid);
$stmt->execute();
$result = $stmt->fetchall(PDO::FETCH_ASSOC);

//Validate & Filter
if(isset($_POST[‘submit’])){
if(!filter_var($_POST[‘website’], FILTER_VALIDATE_URL))
{
echo “The website you entered is not valid!”;
}else{
$clientid = $_GET[‘id’];
$fname = $_POST[‘fname’];
$lname = $_POST[‘lname’];
$phone = $_POST[‘phone’];
$email = $_POST[‘email’];
$website = $_POST[‘website’];
$stmt=$db->prepare(“UPDATE clients SET fname=’”.$fname."’, lname=’".$lname."’, phone=’".$phone."’, email=’".$email."’, website=’".$website."’");
$stmt->execute();
header(‘Location: client.php’);
die();
}
}

You are missing out on how prepared statements actually work, look here for some direction.

As for your question, you need a where clause. It says, “I want to update the following information where this matches.”

The format is,
[php]
UPDATE
table
SET
column_name = new_value
WHERE
column_name = generally_the_primary_key_value;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service