我有一个要在星期一渲染的项目。它的目的很简单,一个添加用户,删除和编辑的表格。我的问题是在更新上。例如,我的数据库中有三行,我想编辑第二行,不可能!我的代码将始终更新数据库的最后一行。如果要测试,请在此处进行测试。 https://a-marie.000webhostapp.com/ 我真的不知道该怎么办
<?php
include_once("inc/connexion.php");?>
<HTML><HEAD>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script type= text/javascript>
function confsuppr(id_personne, nom){
if (confirm("Etes-vous surre de supprimer "+nom)){
window.location.replace("https://a-marie.000webhostapp.com/?idsuppr="+id_personne)
}
}
</script>
<TITLE>personnes</TITLE></HEAD><BODY>";
<?php PRINT_R($_REQUEST); ?><hr>
<form class="w3-container w3-light-grey"><FORM ACTION="" METHOD="POST"><legend>Ajout d'une personne :</legend>
<LABEL>Prenom : </LABEL><input class="w3-input w3-border w3-round" type="text" style="width:30%" name="prenom" required>
<LABEL>Nom : </LABEL><input class="w3-input w3-border w3-round" type="text" style="width:30%" name="nom" required>
<LABEL>E-Mail : </LABEL><input class="w3-input w3-border w3-round" type="text" style="width:30%" name="email">
<LABEL>Statut : </LABEL><input class="w3-input w3-border w3-round" type="text" style="width:30%" name="statut">
<LABEL>Date de naissance :<br/></LABEL> <input class='w3-input w3-border w3-round' type=date name="annee_naissance" required>
<INPUT TYPE=SUBMIT class='w3-button w3-block w3-teal w3-round' style='width:30%' NAME='valider' VALUE='Ajouter'>
</form>
<?php
if (isset($_REQUEST['valider'])=="Ajouter") {
$prenom=mysqli_real_escape_string($cnx, $_REQUEST['prenom']);
$nom=mysqli_real_escape_string($cnx, $_REQUEST['nom']);
$annee_naissance=$_REQUEST['annee_naissance'];
$email=mysqli_real_escape_string($cnx,$_REQUEST['email']);
//$statut=$_REQUEST['statut'];
$statut=mysqli_real_escape_string($cnx,$_REQUEST['statut']);
$req="INSERT INTO personnes VALUES(NULL,'$nom','$annee_naissance','$prenom','$email','$statut');";
$result=mysqli_query($cnx,$req); if(!$result) $msg="requete invalide : ".mysqli_error(); else $msg=$req;
}
if (isset($_REQUEST['idsuppr'])!="") {
$supprid=$_REQUEST['idsuppr'];
$req="DELETE FROM personnes WHERE id_personne='$supprid';";
$result=mysqli_query($cnx,$req); if(!$result) $msg="requete invalide : ".mysqli_error(); else $msg=$req;
}
if (isset($_REQUEST['maj'])!="") {
$id_personne=$_REQUEST['id_personne'];
//$id_personne = $_GET['maj'];
$prenom=ucfirst(mysqli_real_escape_string($cnx,$_REQUEST['prenom']));
$nom=strtoupper(mysqli_real_escape_string($cnx,$_REQUEST['nom']));
$annee_naissance=$_REQUEST['annee_naissance'];
$email=mysqli_real_escape_string($cnx,$_REQUEST['email']);
$statut=mysqli_real_escape_string($cnx,$_REQUEST['statut']);
$req="UPDATE personnes SET nom='$nom',annee_naissance='$annee_naissance',prenom='$prenom',email='$email',statut='$statut' WHERE id_personne='$id_personne';";
$result=mysqli_query($cnx,$req); if(!$result) $msg="requete invalide : ".mysqli_error(); else $msg=$req;
}
$str="<div class='w3-container'><table class=w3-table-all w3-hoverable><caption>Personnes</caption><thead><TR class='w3-light-grey'><TH>Id</TH><TH>Prenom</TH><TH>Nom</TH><TH>Naiss</TH><TH>EMAIL</TH><TH>Statut</TH><TH>Suppr</TH><TH>maj</TH></TR></thead>";
$req="SELECT * FROM personnes ORDER BY id_personne ASC;";
$result=mysqli_query($cnx,$req); if(!$result) $msg="requete invalide : ".mysqli_error();
//$result=mysqli_query($cnx,$req); $msg="requete invalide : ".mysqli_error(); $msg=$req;
else{
foreach ($result as $ligne) {
$str.="<TR><FORM METHOD='POST' ACTION=''>";
$prenom=$ligne["prenom"];
$nom=$ligne["nom"];
$id_personne=$ligne["id_personne"];
$ddn=$ligne["annee_naissance"];
$email=$ligne["email"];
$statut=$ligne["statut"];
$str.="<TD><input type=hidden name=id_personne value='$id_personne'></TD>";
$str.="<TR><TD>".$id_personne."</TD>";
$str.="<TD><input class='w3-input' type=text name='prenom' value=$prenom></TD><TD><input class='w3-input' type=text name='nom' value=$nom></TD><TD><input class='w3-input' type=date name='annee_naissance' value=$ddn></TD><TD><input type=email name='email' value=$email></TD><TD><input type=text name='statut' value=$statut></TD>";
$str.="<TD><input class='w3-input' type=button onclick=confsuppr($id_personne,\"$nom\"); value=Suppr></TD>";
$str.="<TD><input class='w3-input' type=submit NAME=maj value=maj /><input type=hidden name=id_personne value=$ligne[id_personne] /> </TD></TR>";
}
}
$str.="</TABLE></div>"; echo $str;?>
<div class="w3-row"><div class="w3-twothird w3-container">
<div class="w3-panel w3-amber"><p><?php if (isset($msg)) { echo $msg;}else{echo $req;} ?><p></div>
</div></div>
</BODY></HTML>
答案 0 :(得分:1)
关闭您的 form标记
$str.="<TR><FORM METHOD='POST' ACTION=''>";
$prenom=$ligne["prenom"];
$nom=$ligne["nom"];
$id_personne=$ligne["id_personne"];
$ddn=$ligne["annee_naissance"];
$email=$ligne["email"];
$statut=$ligne["statut"];
$str.="<TD><input type=hidden name=id_personne value='$id_personne'></TD>";
$str.="<TR><TD>".$id_personne."</TD>";
$str.="<TD><input class='w3-input' type=text name='prenom' value=$prenom></TD><TD><input class='w3-input' type=text name='nom' value=$nom></TD><TD><input class='w3-input' type=date name='annee_naissance' value=$ddn></TD><TD><input type=email name='email' value=$email></TD><TD><input type=text name='statut' value=$statut></TD>";
$str.="<TD><input class='w3-input' type=button onclick=confsuppr($id_personne,\"$nom\"); value=Suppr></TD>";
$str.="<TD><input class='w3-input' type=submit NAME=maj value=maj /><input type=hidden name=id_personne value=$ligne[id_personne] />
//close your form
</TD></Form></TR>";
答案 1 :(得分:0)
在正确关闭表单的同时,您需要正确地将HTML封装在表单中。最简单的方法是为每一行创建一个新表,并用form元素包装该表。
Future<void> _navigateToDetail(Debt debt) async {
DebtActionResult result = await Navigator.push(context, MaterialPageRoute(builder: (context) => DebtDetail(debt)));
if (result == null) {
// Do nothing, no change
} else {
if (result.action == 2) {
// oh, it's a delete request
setState( () {
widget.debtList.removeWhere( (d) => d.id == result.debtId );
});
} else if (result.action == 1) {
// modify the debtList here .. write your code similar to delete above.
}
}
}