【问题标题】:What is the error of my code? [duplicate]我的代码有什么错误? [复制]
【发布时间】:2016-01-13 11:35:02
【问题描述】:

谁能告诉我我的编码有什么问题?每个“$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>

【问题讨论】:

  • $current... 仅当您在 $_GET 中有一个 id 并在您的 XML 中找到值时才设置;但是无论是否满足测试,您都会显示它们
  • 在您的第一个页面加载时,不执行 IF 中的 PHP 代码,不创建变量。因此,当您尝试在您的页面上显示(不存在的)变量内容时,您会收到错误消息。
  • 首先您必须在文件顶部定义 $current... 变量并为它们分配空值..

标签: php html xml


【解决方案1】:

而不是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;
            }

【讨论】:

  • 非常感谢!有用! =D
  • @Sarah 如果它解决了您的问题,请接受此答案
【解决方案2】:

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

<?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;
        }

【讨论】:

  • 非常感谢!这很有帮助! =D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2016-11-29
  • 2012-07-17
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
相关资源
最近更新 更多