我正在尝试将一些数据插入到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 ['用户名']会造成麻烦吗?
任何有关正确解释的帮助都将受到高度赞赏!
答案 0 :(得分:0)
您正在更新或插入?尝试使用phpmyadmin上的静态数据手动查询,然后确保您的会话已设置&如果插入成功数据则返回true