插入MYSQL数据库WHERE username =:username

时间:2016-03-07 22:24:21

标签: php mysql

所以,我挣扎了2天,我还没有在google上看到任何关于它有效的例子,所以我猜它不会像这样工作:

$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));

它不会产生任何错误,但也不会将其放入数据库中。

我真的不知道如何让它进入用户表,我也尝试更新该字段:

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

有谁知道解决方案?提前谢谢!

1 个答案:

答案 0 :(得分:1)

INSERT用于创建新记录,您要查看的内容是更新当前记录。您需要使用UPDATE查询,如下所示:

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

请注意,我们将参数作为数组传递给execute()函数。