MySQL Forums
Forum List  »  PHP

I'm having extreme difficulty getting my ADD and DELETE buttons to work with PHP and MySQL tables.
Posted by: Malinda Smith
Date: April 25, 2013 02:49PM

I need to create a PHP page where I can input form data and see what was entered on the same page and another page that is the data output that will show the table with the data entered. At the bottom of the output page I need a an Update and Delete button for changing and clearing any data in the table. The sample that I experimented with has the input form at the top and the records underneath and the delete button looping at the end of each record row. After experimenting, I separated the coding for the input and output on two separate pages. I got the button out of the loop to just show at the bottom for the delete, but then the data in the table will not show and the delete button doesn't work. The same with the add button on the input form. It now will not add and I'm not sure what I did wrong. I could use help ASAP for I am on a deadline.

THIS IS WHAT I HAVE FOR THE INPUT FORM:

<?php // forminput.php
require_once 'login_publications.php';


if(isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))

{
$author=get_post('author');
$title=get_post('title');
$category=get_post('category');
$year=get_post('year');
$isbn=get_post('isbn');

$query= "INSERT INTO classics VALUES" .
"('$author','$title','$category','$year','$isbn')";

if(!mysql_query($query, $db_server))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}

echo <<<_END
<form action="formoutput.php" method="post"><pre>
Author <input type="text" name="author" />
Title <input type="text" name="title" />
Category <input type="text" name="category" />
Year <input type="text" name="year" />
ISBN <input type="text" name="isbn" />
<input type="submit" value="ADD RECORD" />
</pre></form>
_END;

function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>

THIS IS WHAT I HAVE FOR THE OUTPUT FORM:

<?php output.php
require_once 'login_publications.php';

if (isset($_POST['author']) &&
(isset($_POST['title']) &&
(isset($_POST['category'])) &&
(isset($_POST['year']) &&
(isset($_POST['isbn']))

//These two query lines is where I am stuck and after adding the above ISSET and this query statement, I lost the data.
$query = "SELECT * FROM classics";
$result = mysql_query($query);


if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

echo "<table><tr><th>Author</th><th>Title</th><th>Category</th><th>Year</th><th>ISBN</th></tr>";

for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo "<tr>";
for ($k = 0 ; $k < 5 ; ++$k)
echo "<td>$row[$k]</td>";
echo "</tr>";
}

echo "</table>";

{
$author=get_post('author');
$title=get_post('title');
$category=get_post('category');
$year=get_post('year');
$isbn=get_post('isbn');

$query = "DELETE FROM classics VALUES" .
"('$author','$title','$category','$year','$isbn')";
if (!mysql_query($query, $db_server)) echo "DELETE failed: $query<br />" . mysql_error() ."<br />";
}

echo <<<_END
<form type="formoutput.php" method="post">
<input type="text" name"record" />
<input type="hidden" name="delete" value="yes" />
<input type="submit" value="DELETE RECORD" />
</form>
_END;

mysql_close($db_server);

function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}

?>

The original test form that worked perfectly before I dissected it.
That form included the coding below:


<?php // sqltest.php
require_once 'login_publications.php';

if(isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post('isbn');
$query = "DELETE FROM classics WHERE isbn='$isbn'";

if(!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}

if(isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))

{
$author=get_post('author');
$title=get_post('title');
$category=get_post('category');
$year=get_post('year');
$isbn=get_post('isbn');

$query= "INSERT INTO classics VALUES" .
"('$author','$title','$category','$year','$isbn')";

if(!mysql_query($query, $db_server))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}

echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author" />
Title <input type="text" name="title" />
Category <input type="text" name="category" />
Year <input type="text" name="year" />
ISBN <input type="text" name="isbn" />
<input type="submit" value="ADD RECORD" />
</pre></form>
_END;

$query= "SELECT * FROM classics";
$result=mysql_query($query);

if(!$result) die ("Database access failed: " . mysql_error());
$rows=mysql_num_rows($result);

for($a = 0 ; $a < $rows ; ++$a)
{
$row=mysql_fetch_row($result);
echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Category $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="isbn" value="$row[4]" />
<input type="submit" value="DELETE RECORD" /></form>
_END;
}

mysql_close($db_server);

function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}

?>

Options: ReplyQuote


Subject
Written By
Posted
I'm having extreme difficulty getting my ADD and DELETE buttons to work with PHP and MySQL tables.
April 25, 2013 02:49PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.