带有未定义偏移量的PHP错误

时间:2014-02-09 11:52:24

标签: php undefined offset

并且有点卡在这里同样的错误Undefined offset, 在每个按钮点击,如果是两个if,甚至是来自“name”的值!= guy1它不跳过它,它完成工作,它删除应该的,但它带有错误

<form  action="deleteprog.php" method="post" >
   <button type="submit" name="1000"  id="1000" value="1000">DELETE</button>
   <button type="submit" name="1001"  id="1001" value="1001">DELETE2</button>
</form>



<?php

mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("project") or die(mysql_error());


$guy1= 1000;
$guy2= 1001;
$name = htmlentities($_POST['1000']);
echo $name;
$name1 = htmlentities($_POST['1001']);
echo $name1; 


if ($name == $guy1 )
{
    mysql_query("DELETE FROM progammers WHERE Sifra_zaposlenog= '1000'") 
    or die(mysql_error());
    echo "Member 1000 deleted"; 
}
var_dump($name);
if ($name1 == $guy2 )
{
    mysql_query("DELETE FROM progammers WHERE Sifra_zaposlenog= '1001'") 
    or die(mysql_error());
    echo "Member 1001 deleted"; 
}
 var_dump($name1);


?>

2 个答案:

答案 0 :(得分:0)

$name  = (isset($_POST['1000'])) ? (int)$_POST['1000'] : null;
$name1 = (isset($_POST['1001'])) ? (int)$_POST['1001'] : null;

答案 1 :(得分:0)

在分配变量之前进行检查:

<?php
if(isset($_POST)){
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("project") or die(mysql_error());

    if(isset($_POST)){
        if (isset($_POST['1000']){
        $name1 = $_POST['1000'];
        $guy1= 1000;
        if ($name2 == $guy2 ){
            mysql_query("DELETE FROM progammers WHERE Sifra_zaposlenog= '1000'") 
            or die(mysql_error());
            echo "Member 1000 deleted"; 
        }
        }elseif(isset($_POST['1001'])){
        $name2 = $_POST['1001'];
        $guy2= 1001;
        if ($name2 == $guy2 ){
            mysql_query("DELETE FROM progammers WHERE Sifra_zaposlenog= '1001'") 
            or die(mysql_error());
            echo "Member 1001 deleted"; 
        }
        }else{  
           echo "Member not set";
        }
    mysql_close();
    }
}
?>