-1

Okay so guys, I'm new to PHP. I'm having problems adding an "Edit" button as fourth element on 4th column(Action) which is a th. I'll need to add an Edit button on each of book titles and bring the user to an Edit page so they can Edit the book information. I don't know what to do next. I don't know how to add the button and at the same time pass the edit request through POST. What should I change? How do I embed the html within php

Btw, I'm using XAMPP and notepad++

Thanks.

<?php
include('databaseConnection.php');
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/librarySystem.css">
<title> Book List </title>
</head>

<div id="booklist_container">

    <table id="booklist_table">
        <?php

                $select_all_books = "SELECT * FROM booklist";
                $result = mysql_query($select_all_books);

                echo "<tr>";
                                echo "<th>" ."Title". "</th>" ;   
                                echo "<th>" ."Author". "</th>" ;
                                echo "<th>" ."ISBN" . "</th>" ;
                                echo "<th>" ."Action". "</th>";
                echo "</tr>";

                echo  "<form method="POST" action="edit.php">" ;

                    while($row = mysql_fetch_array($result))
                    {
                        $authorVar = $row['Author'];  
                        $titleVar = $row['Title'];
                        $ISBN = $row['ISBN'];

                        echo "<tr>";
                                        echo "<td>" . $titleVar. "</td>";   
                                        echo "<td>" . $authorVar. "</td>";
                                        echo "<td>" . $ISBN . "</td>";
                                            echo"<input type="button" value="Edit Info">"  ;

                        echo "</tr>"; 
                    } // end of while loop

                echo  "</form>" ;
        ?>

    </table>
</div>

</html>
heisenberg
  • 1,784
  • 4
  • 33
  • 62

2 Answers2

1

You have a syntax error when you do the button. Do something like

echo '<input type="button" value="Edit Info">';

Replace outer double quotes with single quote.

MikeWu
  • 3,042
  • 2
  • 19
  • 27
1

Remove the form tags from outside of the while loop, and try this for the body of the loop:

$authorVar = $row['Author'];  
$titleVar = $row['Title'];
$ISBN = $row['ISBN'];

echo "<form method='POST' action='edit.php'>";
echo "<tr>";
echo "<td>" . $titleVar. "</td>";   
echo "<td>" . $authorVar. "</td>";
echo "<td>" . $ISBN . "</td>";
echo "<td>";
echo "<input type='submit' value='Edit Info'>";
echo "</td>";
echo "</tr>";
echo "<input type='hidden' name='ISBN' value='" . $ISBN . "'>"; 
echo "</form>" ;

I fixed the problems with your quotation marks and set up a separate form for each book entry that submits the (unique?) ISBN number using a hidden input. You can use this ISBN id to generate your edit.php file to match the selected book. Notice I also changed your input button from type button to type submit so that it actually submits the form. Also notice that this button is now inside of td tags so that it renders properly.

Jason
  • 2,725
  • 2
  • 14
  • 22
  • thank you very much. this solved my problem and your solution is very easy to understand for someone new to php. – heisenberg Nov 02 '15 at 04:59