Luc Stey
Luc Stey

Reputation: 115

Insert into MYSQL Database WHERE username = :username

So, im struggling with this for 2 days, i havent seen any example on google about that it works, so i guess it doesnt work like this:

$steamusername = "xx";
$uname = $_SESSION['username'];
$sql1 = "INSERT INTO users (steamusername) VALUES ( :steamusername) WHERE :username = username";

$query = $conn->prepare( $sql1 );
$result = $query->execute( array( ':steamusername'=>$steamusername, ':username'=>$uname));

It does not give any errors, but it also does not put it into the database.

I really have no idea how i can make it it goes into the user table, i also tried to update the field:

$sql1 = "UPDATE users SET steamusername = :steamusername WHERE username = :username";
$stmt1 = $conn->prepare($sql1);
$stmt1->bindParam(':username', $uname);
$stmt1->bindValue(':steamusername', $steamusername);
$stmt1->execute();

Does anyone know the solution? Thanks in advance!

Upvotes: 1

Views: 1348

Answers (1)

BenM
BenM

Reputation: 53208

INSERT is used to create a new record, what you're looking to do is update a current record. You need to use an UPDATE query, as follows:

$query = $conn->prepare( "UPDATE users SET steamusername = :steamusername WHERE username = :username" );
$query->execute(array( ':steamusername' => $steamusername, ':username' => $uname));

Notice that we pass the parameters to the execute() function as an array.

Upvotes: 1

Related Questions