我试图在我的sql数据库中获取id的值,并将其作为复选框的值插入


Im trying to get the value of id in my sql database and insert it as the value of my checkbox

我试图在sql数据库中获取id的值,并将其作为复选框的值插入。

while( $result = mysql_fetch_object( $requete ) ) {
            $temp = $temp + 1;
            echo(" <form><div align ='"center'">".$result->nom." ".$result->prenom."<input type='checkbox' name='sel[]' value='$temp'>");
            }
        echo("<br><br><input type='button' onClick='confirme($temp)' value='Supprimer'></form>");

$temp应该是sql中id的值,函数confirme()应该使用id的值运行。

<?php
while( $result = mysql_fetch_assoc($requete)) {
                    $temp++;
                    echo "<form><div align ='center'> $result[nom] $result[prenom] <input type='checkbox' name='sel[]' value='$temp'>";
                    }
                echo "<br><br><input type='button' onClick='confirme($temp)' value='Supprimer'></form>";
?>

试试这个

您忘记在echo中连接$temp变量,而您的第二个echo在循环之外:

while( $result = mysql_fetch_object( $requete ) ) {
    $temp = $temp+1;
    echo(" <form><div align ='"center'">".$result->nom." ".$result->prenom."<input type='checkbox' name='sel[]' value='".$temp."'>");
    echo("<br><br><input type='button' onClick='confirme(".$temp.")' value='Supprimer'></form>");
}

但是你说你想要id,$temp绝对不是id,而是一个计数器,它可能与数据库中的id一致,也可能不一致。你必须像对待其他列一样做,你的代码最终会变成:

while( $result = mysql_fetch_object( $requete ) ) {
    $temp = $result->id # assuming the column you want is actually named "id"
    echo(" <form><div align ='"center'">".$result->nom." ".$result->prenom."<input type='checkbox' name='sel[]' value='".$temp."'>");
    echo("<br><br><input type='button' onClick='confirme(".$temp.")' value='Supprimer'></form>");
}