我准备好的声明在执行时没有使用更新后的值,它使用我在绑定之前设置的值,它应该如何工作?因为在W3schools的例子中它使用了更新的值,所以我不确定我在这里做错了什么,//赢家评论是有问题的部分,$ assqlst2不是使用$ pointswon的更新值(I把这个回声只是为了确保值是正确的,并且我没有在输出中得到任何错误。)
<?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;
}
}
?>
答案 0 :(得分:0)
它真的必须嵌套吗?
您不能只是重新搜索查询并多次使用它。如果你愿意,你只需将它放在一个函数中,所以你只需要在需要时调用它:
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 */