-2

Can anyone tell what is wrong with my coding? There is undefined variable on every "$current...". Why does it has error while when i click on the search button, the result come out in the table?

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>
Sarah
  • 1
  • 1
  • 1
    `$current...` are only set if you've had an id in $_GET and found the values in your XML; but you're displaying them whether those if tests were met or not – Mark Baker Jan 13 '16 at 11:36
  • On your first page load, the PHP codes within the IF are not executed, the variables are not created. So you get the error message when you are trying to display the (non-existing) variable content on ur page. – Ahs N Jan 13 '16 at 11:38
  • 1
    First you have to define $current... variables top of the file and assign null value for them .. – nu1_ww Jan 13 '16 at 11:41

2 Answers2

0

instead of your section inside the foreach Try below

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;
            }
Anto S
  • 2,448
  • 6
  • 32
  • 50
0

They are defined only after you search, on first page load they are not defined

<?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;
        }
JosefPP
  • 642
  • 1
  • 5
  • 11