gpt4 book ai didi

php - 将从 MySQL 查询获得的字符串/val 转换为 php 中的 int?

转载 作者:行者123 更新时间:2023-11-29 02:32:05 25 4
gpt4 key购买 nike

我试图从 MySQL 数据库中检索值并将其转换为 int,而不是向其添加 3,而不是尝试将更新 int 存储到数据库中。我没有收到任何错误或任何信息。我​​从数据库中检索到的初始值为5 而不是我尝试添加 3 而不是更新数据库。现在更新的值应该是 8 但它不是 3。我不知道我做错了什么所以请帮助我。

我在 index.php 中的 php 代码如下:

else if ($tag == 'addQuestion'){
$username = mysql_real_escape_string($_POST['username']);
$question = mysql_real_escape_string($_POST['question']);
$tag1 = mysql_real_escape_string($_POST['tag1']);
$tag2 = mysql_real_escape_string($_POST['tag2']);
$tag3 = mysql_real_escape_string($_POST['tag3']);
$time = $_POST['time'];
$addQu = $db->addQuestion($username, $question, $tag1, $tag2, $tag3,$time);
if($addQu){
$q_id = $addQu["id"];
$addQTA = $db->addQTA($username,$q_id,$question,$tag1,$tag2,$tag3);
if($addQTA){
$getKP= $db->getKP($username);
if($getKP){

//Having trouble at this part
$kp = (int)$getKP['karma_points'];
$ask_question_points = $kp + 3;

$updateKP= $db->updateKP($username,$ask_question_points);
if($updateKP){
$response["error"] =1;
$response["msg"] = "updateKP in AddQuestion Succesfull";
echo json_encode($response);
}
else{
$response["error"] =1;
$response["error_msg"] = "Error updateKP in AddQuestion";
echo json_encode($response);
}
}
else{
$response["error"] =1;
$response["error_msg"] = "Error inserting getKP in AddQuestion";
echo json_encode($response);
}
}
else{
$response["error"] =1;
$response["error_msg"] = "Error inserting QTA";
echo json_encode($response);
}
}else{
$response["error"] =1;
$response["error_msg"] = "Error inserting question";
echo json_encode($response);
}
}

这是 DB_functions.php 中处理更新查询的函数代码:

public function updateKP($username,$karma_points){
$result = mysql_query("UPDATE users SET karma_points = '$karma_points' WHERE username = '$username'") or die(mysql_error());
return($result);
}

谢谢!!

最佳答案

你应该避免让自己头疼,并在一个查询中完成:

$result = mysql_query("UPDATE users SET karma_points = karma_points + $karma_points WHERE username = '$username'") or die(mysql_error());

这将自动为用户 $username 增加您的 karma_points 列,值为 $karma_points。不用在 PHP 中进行计算并将其发送回 MySQL,只需在 MySQL 中进行即可。

关于php - 将从 MySQL 查询获得的字符串/val 转换为 php 中的 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11597062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com