2016-01-13 88 views
-2

任何人都可以告诉我的编码有什么问题吗?每个“$ 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> 
+1

'$ current ...'只有在$ _GET中有一个id并找到了价值时才会设置在您的XML中;但是你是否显示它们是否符合测试 –

+0

在第一页加载时,IF内的PHP代码不会被执行,变量也不会被创建。因此,当您尝试在您的页面上显示(不存在)变量内容时,您会收到错误消息。 –

+1

首先,您必须定义$ current ...变量顶部的文件并为它们分配空值。 –

回答

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; 
      } 
+0

非常感谢!有用! = D – Sarah

+0

@Sarah如果它解决了你的问题接受这个问题 – Butterfly

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; 
     } 
+0

非常感谢!这很有帮助! = d – Sarah