这段代码让我感到恶心
这是结果:
以下是代码:
<?php
$conexion = mysql_connect('localhost','root',"");
mysql_select_db('mybd', $conexion);
$consulta_mysql='select * from agenda';
$resultado_consulta_mysql=mysql_query($consulta_mysql,$conexion);
echo "<form action='' method='post'>";
echo "<select name='id'>";
while($fila=mysql_fetch_array($resultado_consulta_mysql)){
echo "<option value='".$fila['id']."' >".$fila['id']." ".$fila['nombre']."</option>";
}
echo "</select><br>";
echo "<input type='submit' value='borrar'";
echo "</form> <br>";
if (isset($_POST['id'])){
$id = $_POST['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("mybd",$link);
$sql = "DELETE from prueba.agenda WHERE id=$id";
$result = mysql_query($sql);
echo "!Hemos eliminado los datos selecionados¡";
}
//doesnt enter into the if
else {it doesnt enter
echo "no entro en el aif";}
?>
我是个小新手抱歉:/
答案 0 :(得分:1)
这是您的代码:
<?php
$conexion = mysql_connect('localhost','root',"");
mysql_select_db('mybd', $conexion);
$consulta_mysql='select * from agenda';
$resultado_consulta_mysql=mysql_query($consulta_mysql,$conexion);
?>
<form action="" method="post">
<select name="id">
<?php
while($fila = mysql_fetch_array($resultado_consulta_mysql)) {
echo "<option value='".$fila['id']."' >"
.$fila['id']." ".$fila['nombre']."</option>";
}
?>
</select><br>
<input type='submit' value='borrar'>
</form> <br>
<?php
if(isset($_POST['id'])) {
$id = $_POST['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("mybd",$link);
$sql = "DELETE from prueba.agenda WHERE id=$id";
$result = mysql_query($sql);
echo "!Hemos eliminado los datos selecionados¡";
} else {
//doesnt enter into the if
echo "no entro en el aif";
}
?>
好吧,如果它进入了else语句,就意味着$ _POST [&#39; id&#39;]还没有设置,这意味着你要么不发布到这个页面或您未将id
发布到此页面。这可能是HTML的问题。我注意到您的<input type='submit' value='borrar'>
没有>
,这意味着HTML可能会搞砸并造成这种情况。
有时,错误可能是输入错误或忘记做某事。
附加说明:
mysql_
个功能,因此您应该考虑使用mysqli_
或PDO
我个人推荐PDO
我的第一点不仅相关,而且您的代码也容易受到SQL注入攻击。什么是SQL注入?当用户输入没有被清理并且其变量直接放在SQL中时。为什么这么糟糕?它允许用户将SQL放入其输入中并实际修改运行的SQL代码,这反过来又可以让他们获取您不希望获得的信息,并且可能会更糟。
将代码格式化为更多&#34;可读&#34;标准帮助其他人阅读您的代码。有&#34;标准&#34;为了列出您的代码,虽然许多人不会坚持这些标准,但最好尽可能地保持这些标准。
答案 1 :(得分:0)
使用此
if (isset($_POST['submit'])){
$id = $_POST['id'];
$sql = "DELETE from prueba.agenda WHERE id=$id";
$result = mysql_query($sql);
echo "!Hemos eliminado los datos selecionados¡";
}
//doesnt enter into the if
else {it doesnt enter
echo "no entro en el aif";}
?>
此行中的代码使用name = submit,将$ _POST ['id']更改为$ _POST ['submit']
"<input type='submit'
答案 2 :(得分:0)
$id = "" ;
if (isset($_GET['id'])){
$id = trim(strip_tags($_GET['id']));
}
if (!empty($id)) {
if(mysql_query("DELETE FROM prueba.agenda WHERE id='$id'")){
if(TRUE){
?><META http-equiv="refresh" content="0;URL=yourURL.php?teade=good"><?php
} else{
?><META http-equiv="refresh" content="0;URL=yourURL.php?teade=bad"><?php
}
}
}
尝试这样的事情;)