Adding comments to a database based on id - php

I am making a recipe site at the moment and am struggling to get my comment function to work, I previously had it working until but now I keep getting an error which tells me I cannot access the database when I try and submit a comment
I know this might seem quite simple but I'm completely stuck I've been trying to figure out a way round this for weeks but really am not making any headway..
Here the PHP process:
if (!$db_server){
die("unable to Connect to MYSQL: " . mysqli_connect_error($db_server));
$db_status = "not connected";
}else{
if(trim($_POST['submit']) =="submit"){
}else{
if (isset($_POST['dropoption']) && ($_POST['dropoption'] != '')){
if (isset($_POST['meal']) && ($_POST['meal'] != '')) {
$dropoption = clean_string($db_server, $_POST['dropoption']);
$meal = clean_string($db_server, $_POST['meal']);
$quer = "SELECT * FROM `recipename` WHERE `cuisine_type` ='$dropoption' AND `b_l_d` ='$meal'LIMIT 0,1";
mysqli_select_db($db_server, $db_database);
$querya= mysqli_query($db_server, $quer);
if (!$querya) die("database access failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($querya)){
$recipeid .= $row['recipeid'];
$recipename .="<h1>". "Why dont you have ".$row['mealname']."</h1>";
$ingredients .="<p>".$row['ingredients']."</p>";
$recipe .="<p>" .$row['recipe']."</p>";
$cookingtime .="<h4>" .$row['hours']." Hours".$row['minutes']." Minutes </h4>";
$mealpic .="<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/".$row['imagepath']."'/>";
}
if ($comment != ''){
$userid = trim($_SESSION['userid']);
$comment = trim($_POST['comment']);
$userid = clean_string($db_server, $_SESSION['userid']);
$comment = clean_string($db_server, $_POST['comment']);
$query = "INSERT INTO Comments (comment,userid,recipeid) VALUES ('$comment','$userid','$receipeid')";
mysqli_select_db($db_server, $db_database);
mysqli_query($db_server, $query) or
die("Insert failed: " . mysqli_error($db_server));
}
}//if(meal)//
}//if(cuisine)//
} //if(trim)//
}
$query = "SELECT * FROM Comments";
$result = mysqli_query($db_server, $query);
if (!$result) die ("Database access failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)) {
$str_comments .= "<p>" . $row['comment'] . "</p>";
}
and here is the html form:
<?php echo $recipename;
echo $mealpic;
?>
<h2>Ingredients</h2>
<?php
$ingredientchunks = (explode(",",$ingredients));
for($i = 1; $i < count($ingredientchunks); $i++){
echo "$i.$ingredientchunks[$i] <br/>";}
echo $cookingtime;
?>
<h2>Recipe</h2>
<?php
$recipechunks = (explode(",",$recipe));
for($i = 1; $i < count($recipechunks); $i++){
echo "$i.$recipechunks[$i] </br>";}
?>
<form id="results" form method="post" action="results.php">
<input type="submit" id="Like" name="Like" value="Like" />
<input type="submit" id="Next" name="Next" value="Next" />
</form>
<div id=Comments>
<form id="comments" form method="post" action='results.php?cuisine_type=" . $dropoption ."b_l_d=" . $meal . "'>
Comment: <textarea rows="2" cols="30" name="comment" id="comment" placeholder="Anything to say??"></textarea>
<input type="submit" id="comments" name="comments" value="comments" />
</form>
<?php
echo $str_comments;
require_once "db_close.php";
?>
</div>
</p>
<? require_once ('home_stop.php')?>

Related

PHP trying to connect to mysql database! but the code doesnt work

PHP trying to connect to MySQL database! but the code doesn't work.
Here is the code, when I put the code in a PHP file, the display shows actually the code I wrote.
I am stuck, please help!
<?php // sqltest.php
require_once '../../htdocs/login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post($conn, 'isbn');
$query = "DELETE FROM classics WHERE isbn='$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>" .
$conn->error . "<br><br>";
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post($conn, 'author');
$title = get_post($conn, 'title');
$category = get_post($conn, 'category');
$year = get_post($conn, 'year');
$isbn = get_post($conn, 'isbn');
$query = "INSERT INTO classics VALUES" .
"('$author', '$title', '$category', '$year', '$isbn')";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query<br>" .
$conn->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 = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
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;
}
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>
I am reading this book called Learning PHP, MySQL, & JavaScript
4th Edition By Robin Nixon, but when I write the exact code, it doesn't show like in the book but I get that like in the photo. I am trying to connect MySQL with PHP, using xampp. I created also the php.login file. I wrote my username and password, and saved it in the same directory with this code!
please help

How to post data to mySQL database through PHP form?

I am new to PHP and I am following a tutorial that gets information from a mySQL database table by the row and outputs a form to create new table rows. For some reason I can't figure out what is wrong with my code? The page is blank when I refresh the page and I have been staring at this code forever. Does anybody know what I am doing wrong? The database connection is fine as it is used on another page and i have checked.
The mySQL database I have is extremely simple, with 1 table called users that has 5 columns (ID, username, firstName, lastName, title) with the ID being a unique field.
<?php // sqltest.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['delete']) && isset($_POST['ID']))
{
$id = get_post('ID');
$query = "DELETE FROM users WHERE ID='$id'";
if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br>" .
mysql_error() . "<br><br>";
}
if (isset($_POST['ID']) &&
isset($_POST['username']) &&
isset($_POST['firstName']) &&
isset($_POST['lastName']) &&
isset($_POST['title']))
{
$id = get_post('ID');
$username = get_post('username');
$firstName = get_post('firstName');
$lastName = get_post('lastName');
$title = get_post('title');
$query = "INSERT INTO users VALUES" .
"('$id', '$username', '$firstName', '$lastName', '$title')";
if (!mysql_query($query, $db_server))
echo "INSERT failed: $query<br>" .
mysql_error() . "<br><br>";
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
ID <input type="text" name="ID">
username <input type="text" name="username">
firstName <input type="text" name="firstName">
lastName <input type="text" name="lastName">
title <input type="text" name="title">
<input type="submit" value="ADD RECORD">
</pre></form>
_END;
$query = "SELECT * FROM users";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
ID $row[0]
username $row[1]
firstName $row[2]
lastName $row[3]
title $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="title" 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]);
}
Any help would be super awesome!
You are trying to retrieve variables with the wrong way.
Example
$username = get_post('username');
should be
$username = $_POST['username'];
You should do the same with the same code that you are trying to retrieve post variables.
And the query to insert values should be
$query = "INSERT INTO users VALUES('".$id."', '".$username."', '".$firstName."', '".$lastName."', '".$title."')";

php, sql, html updating a unique record with corresponding button

I am new to PHP and SQL and trying to figure out how I can make the HTML Approve (submit) button interact specifically with its corresponding record. Currently when the Approve button is clicked, each of the fields are updated, but the top (first) record available is always the one updated. I would like the user to be able to skip the first record and update a different record. Any and all suggestions/help are greatly appreciated.
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$unapproved = $row['CheckedBy'];
if($unapproved == null)
{
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval.php" method="post">
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
_____________________________________________________________________<br>
<?php
if (isset($_POST['submit']))
{
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
break;
$conn->close();
echo "Record Updated.";
header("Location: adminapproval.php");
}
}
}
}
echo "<br><br> No further items need to be approved at this time.";
?>
TWO FILES
adminapproval.php
<?php
session_start();
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$unapproved = $row['CheckedBy'];
if($unapproved == null){
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval-exec.php?id=<?php echo $row['id']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="checkedby" value="<?php echo $SESSION['CheckedBy']; ?>" />
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
<?php
}
}
} else {
echo "<br><br> No further items need to be approved at this time.";
}
?>
adminapproval-exec.php
<?php
session_start();
if (isset($_POST['submit'])){
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_POST['CheckedBy'];
$id = $_GET['id'];
// OR
// $id = $_POST['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
$conn->close();
// echo "Record Updated.";
header("Location: adminapproval.php");
}
?>
<?php
$server = "localhost";
$username = "username";
$password = "password";
$dbname = "db";
$con = mysqli_connect($server, $username, $password, $dbname);
if (!$con) {
die("Faild: " . mysqli_connect_error());
}
$sql = "UPDATE xxx SET lastname='Jan' WHERE id=2"; // This is importat
if (mysqli_query($con, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($con);
}
mysqli_close($con);
?>
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
Should the row id be coming out of the session as well? If not, then it will always be pointing to the first item in the row.

MySql result in an array

I know that this question have been asked more times than amen in church. But i am gonna ask anyway.
I am going to an boardgame convention in Denmark and i figured "Hey we could use a locate a players SMS service"
So i build it. BUT when i have to take some data out of one Database like the phonenumber and the name. And i need to insert that into another Database (where my SMS injection looks) But a variable can only store 1 number and i need to store 10 ~ 50 numbers and Names
Here is what i got so far.
// escape variables for security
$Name = mysqli_real_escape_string($con, $_POST['Name']);
$Players = mysqli_real_escape_string($con, $_POST['Players']);
$Game = mysqli_real_escape_string($con, $_POST['Game']);
$besked = "Du har oprettet et spil med navn $Name og er et $Game som mangler $Players spiller(e).";
echo $besked;
echo "<br>";
$result = mysqli_query($con,"SELECT * FROM brugere where $Game = 'Ja'");
while($row = mysqli_fetch_array($result)) {
echo $row['Navn'] . " " . $row['VCNummer'];
echo "<br>";
$PhoneNumber = $row['Phonenumber'];
}
mysqli_close($con);
?>
<html>
<body>
<form action="smssend.php" method="post">
<input type="hidden" name="besked" value="<?php echo "$besked"; ?>"><br>
<input type="hidden" name="PhoneNumber" value="<?php echo "$PhoneNumber"; ?>"><br>
<input type="submit" value="Send data">
</form>
</body>
</html>
And when i am trying to put it in the other Database
<?php
$con=mysqli_connect("localhost","USER","PASSWORD","SMSDATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$besked = mysqli_real_escape_string($con, $_POST["besked"]);
$PhoneNumber = mysqli_real_escape_string($con, $_POST["PhoneNumber"]);
$sql="INSERT INTO outbox (DestinationNumber, Class, TextDecoded)
VALUES ('$PhoneNumber', '1', '$besked')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
<meta http-equiv="refresh" content="0;url=../index.php">
I hope this makes sense.
I have tried the following
<?php
$con=mysqli_connect("localhost","USER","PASSWORD","DATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$Name = mysqli_real_escape_string($con, $_POST['Name']);
$Players = mysqli_real_escape_string($con, $_POST['Players']);
$Game = mysqli_real_escape_string($con, $_POST['Game']);
$besked = "Du har oprettet et spil med navn $Name og er et $Game som mangler $Players spiller(e).";
echo $besked;
echo "<br>";
$result = mysqli_query($con,"SELECT * FROM brugere where $Game = 'Ja'");
$phone_numbers = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$phone_numbers[] = $row['Phonenumber'];
}
mysqli_close($con);
?>
<html>
<body>
<form action="smssend.php" method="post">
<input type="hidden" name="besked" value="<?php echo "$besked"; ?>"><br>
<input type="hidden" name="PhoneNumber" value="<?php echo serialize($phone_numbers); ?>">
<input type="submit" value="Send data">
</form>
</body>
</html>
And the next PHP file
<?php
$con=mysqli_connect("localhost","USER","PASSWORD","SMSDATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$besked = mysqli_real_escape_string($con, $_POST["besked"]);
$Phone = unserialize($_POST["PhoneNumber"]);
var_dump($Phone);
/*$sql="INSERT INTO outbox (DestinationNumber, Class, TextDecoded)
VALUES ('$PhoneNumber', '1', '$besked')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Beskeden er sendt";
mysqli_close($con);
*/
?>
Please notice that i have canceled the insert into the database in this part.
But when i try to run the file i get the following
bool(false)
instead of this
while($row = mysqli_fetch_array($result)) {
echo $row['Navn'] . " " . $row['VCNummer'];
echo "<br>";
$PhoneNumber = $row['Phonenumber'];
}
try this
$phone_numbers = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$phone_numbers[] = $row['Phonenumber'];
}
then try a
var_dump($phone_numbers);

PHP Issue with deleting from MySQL

I do have programming experience, but new to php. I do have an issue with an example I was doing from this tutorial. I looked over it millions of times, googled, ect ect. I don't have an idea why my code isnt working.
The purpose is to basically just test inserting and deleting in sql from php, using a button for Add Record and Delete Record. The Add record button works perfectly, but delete doesnt do a thing other than reload the page. Heres the code...
<?php // sqltest.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['type']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post('author');
$title = get_post('title');
$type = get_post('type');
$year = get_post('year');
$isbn = get_post('isbn');
if (isset($_POST['delete']) && $isbn != "")
{
echo "worked!!!!!!!!!!!!!!";
$query = "DELETE FROM classics WHERE isbn='$isbn'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_affected_rows($result) > 0) echo 'user deleted';
//if (!mysql_query($query, $db_server))
//echo "DELETE failed: $query" . mysql_error();
}
else
{
echo "nooooooooooooooooooo";
$query = "INSERT INTO classics VALUES" .
"('$author', '$title', '$type', '$year', '$isbn')";
if (!mysql_query($query, $db_server))
{
echo "INSERT failed: $query" . mysql_error();
}
}
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author" />
Title <input type="text" name="title" />
Type <input type="text" name="type" />
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 ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Type $row[2]
Year $row[3]
ISBN $row[4]
<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>
</pre>
_END;
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
I have looked over this many times, still no idea why this won't work. Is it the for loop that is making this button not work? Note, you will see echo "worked!!!"; and in the else echo "noooooooo"; that was for me to test whether the button was being tested, yet nothing prints. So maybe i missed something in the button code itself? Also, no errors are printed, and my editor (and myself) have missed the syntax error (if thats the case).
The code for the delete button is at the end, before I closed the DB.
Thanks for your help in advance.
Your problem is your first if block.
You're checking for the presence of the posted variables author title type year isbn. Whereas in your delete code the only variables sent are delete and isbn. Therefore the first if block is completely missed (including the delete code).
You need to modify your first if to be if(isset($_POST)) { // a form has been posted. Then it should work.
Another way to do it:
if(isset($_POST['delete']) && isset($_POST['isbn']) && !empty($_POST['isbn'])){
//delete code here
}
if(isset($_POST['author']) && isset($_POST['title']) && isset....){
// insert code here
}
EDIT: rewritten code:
<?php // sqltest.php
// I don't know what's in here, so I've left it
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST))
{
if (isset($_POST['delete']) && !empty($_POST['isbn']))
{
echo "Deleting";
$query = "DELETE FROM classics WHERE isbn='".mysql_real_escape_string($_POST['isbn'])."'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_affected_rows($result) > 0) echo 'user deleted';
}
else
{
echo "Inserting";
$query = "INSERT INTO classics VALUES ('".mysql_real_escape_string($_POST['author'])."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['type'])."', '".mysql_real_escape_string($_POST['year'])."', '".mysql_real_escape_string($_POST['isbn'])."')";
if (!mysql_query($query))
{
echo "INSERT failed: $query" . mysql_error();
}
}
}
// you don't need echo's here... just html
?>
<form action="sqltest.php" method="post">
<pre>
Author <input type="text" name="author" />
Title <input type="text" name="title" />
Type <input type="text" name="type" />
Year <input type="text" name="year" />
ISBN <input type="text" name="isbn" />
<input type='submit' value='ADD RECORD' />
</pre>
</form>
<?php
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
// a better way to do this:
while($row = mysql_fetch_array($result)){
?>
<pre>
Author <?php echo $row[0]; ?>
Title <?php echo $row[1]; ?>
Type <?php echo $row[2]; ?>
Year <?php echo $row[3]; ?>
ISBN <?php echo $row[4]; ?>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name='isbn' value="<?php echo $row[4]; ?>" />
<input type='submit' value='DELETE RECORD' />
</form>
</pre>
<?php
}
mysql_close($db_server);
?>
Verify the method you used in your form. Make sure it's POST like this:
Form action="yourpage.php" method="POST"
and in your code above, replace the following:
$author = get_post('author');
$title = get_post('title');
$type = get_post('type');
$year = get_post('year');
$isbn = get_post('isbn');
with
$author = $_POST['author'];
$title = $_POST['title'];
$type = $_POST['type'];
$year = $_POST['year'];
$isbn = $_POST['isbn'];
Finally, there is no need to check again if the $isbn is not null as you did it in your isset() method. So remove $isbn!="" in the if below:
if (isset($_POST['delete']) && $isbn != "")
{
}
becomes:
if (isset($_POST['delete']))
{
}
Since you are testing, checking if the user clicked the delete button is of less importance. So you can also remove it for a while and add it later because you are sure that, that code is accessible after clicking the delete button.
You have no form field named delete, so it is impossible for your delete code path to ever be taken.
I'm guessing you're tryign to use the value of the submit button to decide what to do? In that case, you're also missing a name attribute on the submit button - without that, it cannot submit any value with the form. You probably want:
<input type="submit" name="submit" value="DELETE RECORD" />
and then have
if (isset($_POST['submit']) && ($_POST['submit'] == 'DELETE RECORD')) {
...
}

Categories