November 9th, 2012, 07:31 PM
Passing variables from a table row to PHP script
I'm developing a web application for our village public library. This is a labor of love. I can put things in the database, and view the database just fine. With updates and deletes, I have a problem.
In short, I have a pretty plain HTML file where the user selects the books he or she wishes to update or delete. That, in turn, calls a PHP routine, called upd2.php, which does the MySQL database query, and displays the results in a table, along with UPDATE and DELETE buttons and editable values in the fields of the table. Clicking one of those buttons calls a second PHP script, called upd3.php. The isbn field is the primary key on the database.
Investigation shows that there appear to be no variables or values passed to the second PHP script, except for the button values (UPDATE or DELETE).
So, passing parameters worked from the regular HTML file to the upd2.PHP script. That, in turn, wrote an HTML page, with a table having multiple columns and one or more rows. However, I cannot seem to use the HTML generated from this PHP script to call another PHP script.
If I hard code a $bookid into the upd3.PHP script, it will delete that book fine.
There are other columns in the tables and in the database, but for simplicity I've removed them here.
echo '<form action="upd3.php" method="post">';
echo "<table border='1'>
for ($i=0; $i <$num_results; $i++)
$row = $result->fetch_assoc();
$tti = $row['title'];
$bookid = stripslashes($row['isbn']);
echo '<td width=65> <input name=upda type=submit value="Update" > </td>';
echo '<td width=65> <input name=dele type=submit value="Delete"> </td>';
// Make this a hidden input later, since it would not make sense to edit this.
echo '<td width=30><input name=bkid value="';
echo '" size=15 maxlength=30> </td>';
echo '<td width=220><input name=titlee value="';
echo '" size=20 maxlength=100> </td>';
echo '<td width=110><input name=lauth value="';
echo '" size=20 maxlength=30> </td>';
If I hard code something in for $bookid, like $bookid="444" right above the $query= line, this works great.
// create short variable names
//This works okay, but I left out the details for this post
@ $db = new mysqli([I]parameters[/I]);
echo 'Error: Could not connect to database. Please try again later.';
$bookid = addslashes($bookid);
$query = "delete from books where isbn = '".$bookid."'";
$result = $db->query($query);
echo $db->affected_rows.' book deleted from database.';
The UPDATE code in upd3.php works similarly, but I'm working on getting it debugged on the simpler case of DELETE.
Any help would be appreciated.
November 9th, 2012, 07:52 PM
PHP variables are case-sensitive. $_POST and $_Post are two different things.
For development you should always have two php.ini settings set a certain way: error_reporting=E_ALL (or better) and display_errors=on. Enable those, restart your webserver, and run your script as it is now. You should see error messages.