Tom Nolan
Tom Nolan

Reputation: 1957

How to update a MySQL row with a particular value?

I'm trying create a profile page for a web application I'm building, and I have the profile page built out, and it pulls their data out of the database fine. When I try to update the information is where I have trouble.

It should only update the row that matches the current logged in member's username. This is what I have so far:

I pull the data from the form:

$phpro_email = filter_var($_POST['phpro_email'], FILTER_SANITIZE_STRING);
$phpro_fname = filter_var($_POST['phpro_fname'], FILTER_SANITIZE_STRING);
$phpro_lname = filter_var($_POST['phpro_lname'], FILTER_SANITIZE_STRING);

I connect to the database:

$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

And I try to update the correct row using UPDATE three columns in my table name which is phpro_users and I specify the row using WHERE phpro_username is equal to the value of the current logged in users.

$stmt = $dbh->prepare("UPDATE phpro_users SET phpro_fname = :phpro_fname, phpro_lname = :phpro_lname, phpro_email = :phpro_email WHERE phpro_username = :phpro_username");
$stmt->bindParam(':phpro_username', $phpro_username);
$stmt->bindParam(':phpro_fname', $phpro_fname, PDO::PARAM_STR);
$stmt->bindParam(':phpro_lname', $phpro_lname, PDO::PARAM_STR);
$stmt->bindParam(':phpro_email', $phpro_email, PDO::PARAM_STR);

$stmt->execute();

When I click submit on the form with this script as it's action and it's method=post I just get a blank white screen. Any ideas on what I'm doing wrong?

Upvotes: 0

Views: 59

Answers (2)

MortimerCat
MortimerCat

Reputation: 874

Change

$stmt->bindParam(':phpro_username', $phpro_username);

to

$stmt->bindParam(':phpro_username', $phpro_username, PDO::PARAM_STR);

It may be defaulting to something strange, best to force it to a value.

Upvotes: 3

Machavity
Machavity

Reputation: 31624

Try changing your SQL to use proper UPDATE syntax

UPDATE phpro_users SET
    phpro_fname = :phpro_fname, 
    phpro_lname = :phpro_lname, 
    phpro_email = :phpro_email
WHERE phpro_username = :phpro_username

You also don't need to use filter_var() since you're using prepared statements

Upvotes: 0

Related Questions