gpt4 book ai didi

php - MySqli 准备好的语句在执行语句时不使用更新的变量值

转载 作者:行者123 更新时间:2023-11-29 20:53:01 27 4
gpt4 key购买 nike

我准备好的语句在执行时没有使用更新的值,而是使用我在绑定(bind)之前设置的值,这是它应该如何工作的吗?因为在 w3schools 的示例中它使用的是更新值,所以我不确定我在这里做错了什么,在//winner comment is Problems 部分下,$assqlst2 没有使用 $pointswon 的更新值(我把 echo 只是确保值正确,并且我在输出中没有收到任何错误)。

<?php 
$dbserver = ""; //database information is populated in my script, i didn't put it here because of security resons.
$dbusername = "";
$dbpassword = "";
$dbname = "";
$dbrecievedPassword = $_POST['dbPassword'];
$winnerid = intval($_POST['winnerid']);
$winnername = $_POST['winnername'];
$pointswon = intval($_POST['pointswon']);
$loserid = intval($_POST['loserid']);
$losername = $_POST['losername'];
$pointsph = $pointswon;
if($dbrecievedPassword != null && $dbrecievedPassword == $dbpassword) {
echo "PWD_OK";
}
else {
echo "PWD_INCORRECT";
}

$conn = new mysqli($dbserver,$dbusername,$dbrecievedPassword);
mysqli_select_db($conn,$dbname);
if($conn->connect_error) {
die("Connection failed: ".connect_error);
}
if($winnerid != null) {
if($sqlst = $conn->prepare("SELECT * FROM table_name WHERE uid=?")) {
if($assqlst2 = $conn->prepare("UPDATE table_name SET as_points=? WHERE as_uid=?")) {
if($sqlst3 = $conn->prepare("INSERT INTO table_name (as_uid,as_points,as_username) VALUES (?,?,?)")) {

$sqlst->bind_param("i",$winnerid);
$assqlst2->bind_param("ii",$pointswon,$winnerid);

$sqlst3->bind_param("iis",$winnerid,$pointswon,$winnername);
if($loserid != null) {
//winner

if(!$sqlst->execute()) {
echo $conn->error;
}
$sqlst->bind_result($b1,$b2,$b3);
$sqlst->fetch();
$pointswon = $pointsph + intval($b3);
echo $pointswon." ".$winnerid;
if(!$assqlst2->execute()) {
echo $conn->error;
}
//loser
$winnerid = $loserid;
if(!$sqlst->execute()) {
echo $conn->error;
}
$sqlst->bind_result($c1,$c2,$c3);
$sqlst->fetch();
$pointswon = intval($c3) - $pointsph;
$sqlst->close();
if(!$assqlst2->execute()) {
echo $conn->error;
}
$assqlst2->close();

}else {
if(!$sqlst->execute()) {
echo $conn->error;
}
$sqlst->bind_result($k1,$k2,$k3);
$sqlst->fetch();
if($k1 != null) {
$pointswon = intval($pointsph) + intval($k3);
$sqlst->close();
if(!$assqlst2->execute()) {
echo $conn->error;
}
$assqlst2->close();

}else {
$pointswon = $pointsph;
$sqlst3->execute();
$sqlst3->close();
}
}
}else {
echo $conn->error;
}
}else {
echo $conn->error;
}
}else {
echo $conn->error;
}

}
?>

最佳答案

它真的必须嵌套吗?

您不能只是重新哈希查询并多次使用它。如果你愿意,你只需将它放在一个函数中,这样你只需在需要时调用它即可:

function query1($conn, $winnerid){

if($sqlst = $conn->prepare("SELECT col1, col2, col3 FROM table_name WHERE uid = ?")){
$sqlst->bind_param("i", $winnerid);
$sqlst->execute();
$sqlst->bind_result($b1, $b2, $b3);
$sqlst->fetch();
$sqlst->close();
}

return array($b1, $b2, $b3);

}

function query2($conn, $pointswon, $id){

if($assqlst2 = $conn->prepare("UPDATE table_name SET as_points = ? WHERE as_uid = ?"))
$assqlst2->bind_param("ii", $pointswon, $id);
$assqlst2->execute();
$assqlst2->close();
}

}

function query3($conn, $winnerid, $pointswon, $winnername){

if($sqlst3 = $conn->prepare("INSERT INTO table_name (as_uid,as_points,as_username) VALUES (?,?,?)")){
$sqlst3->bind_param("iis", $winnerid, $pointswon, $winnername);
$sqlst3->execute();
$sqlst3->close();
}

}

设置完功能后,我们就可以继续实现您想要实现的功能了:

if($winnerid != NULL){

/*** WINNER UPDATE ***/
list($b1, $b2, $b3) = query1($conn, $winnerid); /* CALL THE SELECT QUERY */

if($loserid != NULL){
$pointswon = $pointsph + intval($b3); /* POINTS GATHERED BY THE WINNER */
echo $pointswon." ".$winnerid;
} /* END OF CONDITION $loserid IS NOT NULL */

query2($conn, $pointswon, $winnerid); /* CALL THE UPDATE QUERY */


/*** LOSER UPDATE ***/
list($c1, $c2, $c3) = query1($conn, $loserid); /* CALL THE SELECT QUERY */

$pointswon = intval($c3) - $pointsph; /* POINT GATHERED BY THE LOSER */

query2($conn, $pointswon, $loserid); /* CALL THE UPDATE QUERY */


} /* END OF CONDITION $winnerid IS NOT NULL */

else {

list($k1, $k2, $k3) = query1($conn, $winnerid); /* CALL THE SELECT QUERY */

if($k1 != null){

$pointswon = intval($pointsph) + intval($k3);

query2($conn, $pointswon, $winnerid); /* CALL THE UPDATE QUERY */

} else {

query3($conn, $winnerid, $pointsph, $winnername); /* CALL THE INSERT QUERY */

}

} /* END OF ELSE */

关于php - MySqli 准备好的语句在执行语句时不使用更新的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37871191/

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