SQLi代码未使用用户ID插入正确的行

时间:2014-02-06 19:59:39

标签: php mysql sql sqlite

我一直在尝试修复此代码并且它永远不会工作无论我用它做什么。它应该插入到数据库中你已经发现了一个秘密的游戏进入登录用户的行使用存储在会话中的用户ID有一种方法可以解决这个代码,因为我是难倒的。

<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 mysqli_query($con,"INSERT INTO login (code, link)
VALUES ('You Found a Secret Game!', '/12283719823838hdhj/') WHERE id = " . 
$_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
mysqli_close($con);
?>

2 个答案:

答案 0 :(得分:0)

您不能将任何WHERE与INSERT INTO一起使用。

试试这个:

[编辑] 好的,试试这个。

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
   echo "MySQL Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
unset($query);
$query  = "UPDATE login SET code = 'You Found a Secret Game!', link = '/12283719823838hdhj/' ";
$query .= "WHERE id = '".$_SESSION['user']['id']."'";
var_dump($query);
if (!$mysqli->query($query) {
    echo "Mysql Error: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>

答案 1 :(得分:0)

试试这个

mysqli_query($con,"INSERT INTO login (code, link)
                   VALUES ('You Found a Secret Game!', '/12283719823838hdhj/') 
                 WHERE id ='".$_SESSION['user']['id']."' ",ENT_QUOTES, 'UTF-8' );