我正在尝试向mysql数据库中插入一些数据
$db = new DataBase($config);
// connect to the database RETURNS true if success false if fails
$conn = $db->connect();
// check whether the connection is successfull or not...
if ($db->isConnected())
{
//prepare the query
$query = 'INSERT INTO scoreboard (score) VALUES(:score) WHERE username=:username';
$bindings = array(
'score' => $score,
'username' => ($_SESSION['username'])
);
// call the query function from db class and retrieve the results as an array of rows.
$results = $db->setData($conn,$query,$bindings);
if ($results)
echo "Your Score is Updated!";
else
echo "Your Score is Not Updated!";
}
这是setData()的作用:
function setData($conn,$query,$bindings)
{
try {
// prepare the query
$stmt = $conn->prepare($query);
//binde the query with the data .here the data is $bindings
$stmt->execute($bindings);
return ( $stmt->rowCount() > 0 )
// return the result if the query is success else return false.
? $stmt
: false;
}
catch(Exception $e) {
// return error if something goes wrong
return false;
}
}
每次运行此脚本时,都会得到“您的分数未更新”作为输出。
我要去哪里错了?
是$ _SESSION ['username']引起麻烦了吗?
任何有适当解释的帮助将不胜感激!
最佳答案
您要更新还是插入?在phpmyadmin上使用静态数据手动尝试查询,然后确保设置了会话并成功插入数据,则返回true