我的代码有什么错误?

时间:2016-01-13 11:35:02

标签: php html xml

有人能说出我的编码有什么问题吗?每个" $ current ..."都有未定义的变量。当我点击搜索按钮时,为什么会出现错误,结果会出现在表格中?

Here is the error occur

<html>
<head><title>Search Book</title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container" align="center">
<div class="row">
<div class="col-md-5">
<form method="get" action="" class="form-horizontal" name="search"><br>
<table border="0" bgcolor="#E0FFFF">

<td><h3>Search Book By ISBN:</h3></td>

<div class="form-group">
      <td><input type="text" name="id" class="form-control" placeholder="eg: 978-0320037825"></td>
      </div>
      <br/>
      <tr><td><center><button type="submit" class="btn btn-success">Search</button>&nbsp;</center></td>
      <td><a href="index.php">Back</a></td></tr>
<tr><td>
<?php
if (isset($_GET['id']) !='')
{
    $id = $_GET['id'];
    $xml = simplexml_load_file("bookstore.xml");
    $notThere = True;
    foreach ($xml->children() as $book)
    {

            if ($book->isbn == $id)
            {
                    $currentisbn = $book->isbn;
                    $currenttitle = $book->title;
                    $currentfirstname = $book->firstname;
                    $currentlastname = $book->lastname;
                    $currentprice = $book->price;
                    $currentyear = $book->year;
                    $currentpublisher = $book->publisher;
                    $notThere = False;
            }


    }
    if($notThere)
            {
                echo "ISBN Does Not Exist!";
            }
}
?>

</td>
</tr>
</table>
</form>


<html>


<form method="POST" action="" class="form-horizontal" name="delete">

  <table width="600" bgcolor="#ffffff" border="1" cellpadding="8">

  <tr colspan="2"><h2>Delete Book</h2></tr>
    <tr>
      <td width="150">ISBN</td>
      <td width="320"><?php echo $currentisbn;?></td>
    </tr>
    <tr>
      <td width="150">Title</td>
      <td width="320"><?php echo $currenttitle;?></td>
    </tr>
    <tr>
      <td>First Name</td>
      <td><?php echo $currentfirstname;?></td>
    </tr>
    <tr>
      <td>Last Name</td>
      <td><?php echo $currentlastname;?></td>
    </tr>
    <tr>
      <td width="150">Price</td>
      <td width="320"><?php echo $currentprice;?></td>
    </tr>
    <tr>
      <td width="150">Year</td>
      <td width="320"><?php echo $currentyear;?></td>
    </tr>
    <tr>
      <td width="150">Publisher</td>
      <td width="320"><?php echo $currentpublisher;?></td>
    </tr>
      <td colspan="2" align="center"> <input name="submit" type="submit" value="Delete"/> &nbsp;
      <a class="btn btn-default" href="index.php" role="button">Home</a></td>
    </tr>
</table>
</form>


<?php
if (isset($_GET['id'])!='' && isset($_POST['submit'])!=''){
    $id = $_GET['id'];
    $success=False;

    $dom = new DOMDocument();
    $dom -> load("bookstore.xml");
    $xpath = new DOMXpath($dom);
    $node = $xpath->query("//*[isbn='$id']");
    foreach ($node as $book) 
                {   
                    $book->parentNode->removeChild($book);
                    $success=True;
                } 

    if($success)
    {
        echo "<h1 style='text-align: center;'>Successfully Deleted!</h1>";
    }

     $dom->save("bookstore.xml");

}
?>  
<script>
function goBack() {
    window.history.back();
}
</script>

</html>

2 个答案:

答案 0 :(得分:0)

代替foreach尝试以下

中的部分
if ($book->isbn == $id)
            {
                    $currentisbn = ($book->isbn)?$book->isbn:'';
                    $currenttitle = ($book->title)?$book->title:'';
                    $currentfirstname = ($book->firstname)?$book->firstname:'';
                    $currentlastname = ($book->lastname)?$book->lastname:'';
                    $currentprice = ($book->price)?$book->price:'';
                    $currentyear = ($book->year)?$book->year:'';
                    $currentpublisher = ($book->publisher)?$book->publisher:'';
                    $notThere = False;
            }

答案 1 :(得分:0)

它们仅在您搜索后定义,在第一页加载时未定义

<?php
// add this at the very top of your file
$currentisbn = '';
$currenttitle = '';
$currentfirstname = '';
$currentlastname = '';
$currentprice = '';
$currentyear = '';
$currentpublisher = '';
$notThere = '';
?>

... code, html, etc ...

if ($book->isbn == $id)
        {
                $currentisbn = $book->isbn;
                $currenttitle = $book->title;
                $currentfirstname = $book->firstname;
                $currentlastname = $book->lastname;
                $currentprice = $book->price;
                $currentyear = $book->year;
                $currentpublisher = $book->publisher;
                $notThere = False;
        }
相关问题