我无法从此代码中删除任何行

时间:2014-04-30 08:48:38

标签: php mysqli

这是我的数据库更新和删除代码 但我有这个错误 首先,当打开这个文件名是主题时,即使我没有上传或删除任何内容,我也会收到此消息。就在我打开这个文件的时候。

(Notice: Undefined index: page in C:\xampp\htdocs\art-legend\12\admin\topics.php on line 73)

第二个错误是我更新任何行时。这是工作,但我有这样的信息:

(Notice: Undefined index: topic in C:\xampp\htdocs\art-legend\12\admin\edit.php on line 8)

第三个错误,当我删除它的任何行时,我有这个消息:

(Notice: Undefined index: page in C:\xampp\htdocs\art-legend\12\admin\topics.php on line 73)

(Notice: Undefined index: hiddencounter in C:\xampp\htdocs\art-legend\12\admin\topics.php on line 211)

这是我的全部代码:


<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script>
   $(document).ready(function() {$("tr:odd").addClass("odd");});
</script>

<style type="text/css">
.odd{
 background-color:#ccc;
    }


div.pagination {
    padding: 3px;
    margin: 3px;
}

div.pagination a {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #AAAADD;

    text-decoration: none; /* no underline */
    color: #000099;
}
div.pagination a:hover, div.pagination a:active {
    border: 1px solid #000099;

    color: #000;
}
div.pagination span.current {
    padding: 2px 5px 2px 5px;
    margin: 2px;
        border: 1px solid #000099;

        font-weight: bold;
        background-color: #000099;
        color: #FFF;
    }
    div.pagination span.disabled {
        padding: 2px 5px 2px 5px;
        margin: 2px;
        border: 1px solid #EEE;

        color: #DDD;
    }

</style>


<?php

    /*
        Place code to connect to your DB here.
    */
    $connect=mysqli_connect('localhost','root','adminpass','flip'); // include your code to connect to DB.

    $tbl_name="threads";        //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysqli_fetch_array(mysqli_query($connect,$query));
    $total_pages = $total_pages['num'];

    /* Setup vars for query. */
    $targetpage = "topics.php";     //your file name  (the name of this file)
    $limit = 10;                                //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0

    /* Get data. */
    $sql = "SELECT id,topic FROM $tbl_name order by id DESC LIMIT $start, $limit";
    $result = mysqli_query($connect,$sql);

    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">previous</a>";
        else
            $pagination.= "<span class=\"disabled\">previous</span>";   

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next </a>";
        else
            $pagination.= "<span class=\"disabled\">next </span>";
        $pagination.= "</div>\n";       
    }
?>
            <form action="" method="post">  
    <?php
              echo "<table border='1'  width='50%' cellpadding='0' cellspacing='0'";



              ?>


              <tr>
                <td>Id</td>
                <td>Subject</td>
                <td>#</td>
                <td>#</td>
                <td>Del</td>
              </tr>

              <?
              while($row=mysqli_fetch_object($result)){

                  ?>
                  <tr>
                    <td><? echo $row->id; ?></td>
                    <td><? echo $row->topic; ?></td>
                    <td><a href='edit.php?id=<? echo $row->id; ?>'>Update</a></td>
                    <td><input type="checkbox" name="checkbox[]" value="<?php echo $row->id ; ?>"></td>
                    <td><input type="submit" name="delete" value="delete" ></td>



                  <?
                  }


            echo "</table>";  
            $num=mysqli_num_rows($result);
            if(isset($_POST['delete'])){ /* IF DELETE IS CLICKED */

$checkbox=$_POST['checkbox'];
$hiddencounter=mysqli_real_escape_string($connect,$_POST['hiddencounter']); /* PREVENT A BIT OF SQL INJECTION */

for($i=0;$i<$hiddencounter;$i++){ /* FOR LOOP BASED ON THE NUMROWS BELOW */

   if(!empty($checkbox[$i])){ /* IF THE CHECKBOX IS TICKED */
        $del_id=mysqli_real_escape_string($connect,$checkbox[$i]);  /* PREVENT A BIT OF SQL INJECTION */
        $sql2="DELETE FROM threads WHERE id='$del_id'";
        $query2=mysqli_query($connect,$sql2); /* IMPLEMENT THE DELETE QUERY */

   }

   if($i==$hiddencounter){ /* IF FOR LOOP ENDS */

   echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";

   }

} 

}  

    ?>
    </form>

<?=$pagination?>

,编辑代码为:

<?php

if($connect=mysqli_connect('localhost','root','adminpass','flip')) {
    $id = $_GET["id"];
    $topic = $_GET["topic"];
    $sql="SELECT * from threads WHERE id='$id'";
    $query=mysqli_query($connect,$sql);

    while($row=mysqli_fetch_object($query)) {

        ?>

        <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="get" >

        <input type="hidden" name="id" value="<?php echo $row->id; ?>" >
        <input type="text" name="topic" value="<?php echo $row->topic; ?>" >
        <input type="submit" name="submit" value="Update" >


        </form>

        <?
        }

        $sql2="UPDATE threads SET topic= '$topic' WHERE id='$id'";
        $query2=mysqli_query($connect,$sql2);

        if(isset($_GET['submit'])){

        echo "updat done ";

        }

    }

    else {

        echo "erro connection ";

        }


?>

,删除文件为:

<h1>delete Done</h1>
<br />
<a href="topic.php">return to Pr</a>

非常感谢

3 个答案:

答案 0 :(得分:1)

错误与未定义的索引pagetopichiddencounter有关。

替换:

$page = $_GET['page'];

使用:

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

替换:

$topic = $_GET["topic"];

使用:

$topic = isset($_GET["topic"]) ? (int)$_GET["topic"] : 0;

替换:

$_POST['hiddencounter']

使用:

(isset($_POST['hiddencounter']) ? (int)$_POST['hiddencounter'] : 0)

答案 1 :(得分:0)

$checkbox=$_POST['checkbox'];
$page = $_GET['page'];
$id = $_GET["id"];
$topic = $_GET["topic"];

这些应该以{{1​​}}开头,如果没有设置则默认为某些内容(或者其他代码应该在if子句中。

答案 2 :(得分:0)

尝试把它放在第一位

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

$topic = isset($_GET["topic"]) ? intval($_GET["topic"]) : 0;

并替换猎人检查