PHP is not reading mysql DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

PHP is not reading mysql DB

Post by mdmartiny1977 »

Hello Everyone I am fairly new to PHP and I am trying to write a simple CMS for a friend of mine. I wrote a modification script so he can modify post when he needs to and it works without a problem.

Then I began writing a delete script. When I was writing that I somewhat cheated. I just saved the modification script and renamed it to delete. The first part of the delete script works. It allows you to pick the file you want deleted but it does not show you the contents of that file.

Since I just re-saved the modification script and renamed it it should work the same but it is not. I am unsure of why. I have attached the files to this post in hopes that someone can help me before I lose my mind.
CMS.rar
(3.91 KiB) Downloaded 33 times
Also, if anyone is able to help me please explain to me what it is that you have done or are doing so as I may learn.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP is not reading mysql DB

Post by social_experiment »

I downloaded the files but still don't have an idea to which one's you are refering to. Paste the code you are having a problem with. The code below is from pick_delete.php

Code: Select all

<?php
// you forgot to escape post it should be \"POST\"
$display_block = "<FORM METHOD=POST ACTION=\"show_delete.php\">
<p><strong>Title:</strong>
<select name=\"id\">
$option_block
</select>
<input type=\"submit\" name=\"submit\" Value=\"Get Ad\"></p>
</form>";
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

Re: PHP is not reading mysql DB

Post by mdmartiny1977 »

Sorry about that.. The code that I am having an issue with is

Code: Select all

<?php

if (!$_POST[id]) {
	header ("LOCATION: pick_modify.php");
	exit;
} else {
   //session start
   session_start();
}

if ($_SESSION[valid] != "yes") {
	header ("Location: admin_menu.php");
}

include('includes/connection.php');

//build and issue query
$sql = "SELECT id, title, year, make, model, descript FROM $table WHERE id = '$_POST[id]'";
$result = mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
	$title = $row['title'];
	$year = $row['year'];
	$make = $row['make'];
	$model = $row['model'];
	$descript = $row['descript'];
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Classified Ad</title>
</head>
<body>
<h1>Add Classified Ad</h1>
<h2><em>Modify a Ad</em></h2>
<h3>Ad being modified <?php echo "$title"; ?></h3>
<form method="POST" action="scripts/do_modify.php">
<input type="hidden" name="id" value="<?php echo "$_POST[id]"; ?>"
   <p> <strong>Title:</strong>
      <input type="text" name="title" id="title" value="<?php echo "$title"; ?>" length="25" maxlength="25" />
   </p>
   <p> <strong>Year:</strong>
      <input type="text" name="year" id="year" value="<?php echo "$year"; ?>" length="25" maxlength="25" />
   </p>
   <p> <strong>Make</strong>
      <input type="text" name="make" id="make" value="<?php echo "$make"; ?>" length="25" maxlength="25" />
   </p>
   <p> <strong>Model:</strong>
      <input type="text" name="model" id="model" value="<?php echo "$model"; ?>" length="25" maxlength="25" />
   </p>
   <p> <strong>Description:</strong>
      <textarea name="descript" id="descript" rows="5" cols="75"><?php echo "$descript"; ?></textarea>
   </p>
   <p>
      <input type="submit" name="submit" id="name" value="Update Ad Record" />
   </p>
</form>
<p><a href="admin_menu.php">Return to Administration Menu</a></p>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP is not reading mysql DB

Post by social_experiment »

mdmartiny1977 wrote:The first part of the delete script works. It allows you to pick the file you want deleted but it does not show you the contents of that file.
In the script you pasted, is the record deleted before you attempt to display it and do you receive any errors from mysql_error()?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

Re: PHP is not reading mysql DB

Post by mdmartiny1977 »

It lets me pick it.. It does not delete it from the record and there are no Mysql errors.

I planned on it being a 3 step process...pick it, check it to make sure it is the one that you want to delete and finally delete it.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP is not reading mysql DB

Post by social_experiment »

Ok, you need a delete query to delete the record (apologies if i am stating the obvious)

Code: Select all

<?php
 // this will be on your show_delete.php
 $id = $_POST['id'];
 //
 $query = "SELECT * FROM table WHERE id = '". mysql_real_escape_string($id) ."' ";
 $sql = mysql_query($query);
 
 while ($array = mysql_fetch_array($sql)) {
  // display the data how you want
  // and add a form, using something like
  echo '<form action="" method="post">
  echo '<input type="hidden" name="idValue" value="'. $array['id'] .'" />';
  echo '<input type="submit" value="Delete Record" name="deleteRecord" />';
  echo '</form>';
 }

 // below that have a check to see if the delete form has been submitted
 // or you can have this on another page.
 if (isset($_POST['deleteRecord'])) {
   $idValue = $_POST['idValue'];

   $deleteQuery = "DELETE * FROM table WHERE id = '". mysql_real_escape_string($idValue) ."' ";
   $deleteSql = mysql_query($deleteQuery);

   if ($deleteSql) { echo 'Record deleted'; }

?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

Re: PHP is not reading mysql DB

Post by mdmartiny1977 »

I will give this a shot when I get home
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

Re: PHP is not reading mysql DB

Post by mdmartiny1977 »

I have tried doing what yopu have suggested and now I am getting an error

Parse error: syntax error, unexpected $end in /home/content/m/i/k/mikedmartiny/html/CMS/show_delete.php on line 47
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

Re: PHP is not reading mysql DB

Post by mdmartiny1977 »

This is what my code looked like after inserting what you had suggested

Code: Select all

<?php

if (!$_POST[id]) {
	header ("LOCATION: pick_modify.php");
	exit;
} else {
   //session start
   session_start();
}

if ($_SESSION[valid] != "yes") {
	header ("Location: admin_menu.php");
}

include('/includes/connection.php');
$id = $_POST['id'];

//build and issue query
$query = "SELECT * FROM table WHERE id = '". mysql_real_escape_string($id) ."' ";
$sql = mysql_query($query);
$result = mysql_query($sql, $connection) or die(mysql_error());

 while ($array = mysql_fetch_array($sql)) {
  // display the data how you want
  // and add a form, using something like
  echo '<form action=\"do_delete.php\" method=\"post\">
		<input type=\"hidden\" name=\"idValue\" value=\"'. $array['id'] .'\" />
		<input type=\"submit\" value=\"Delete Record\" name=\"deleteRecord\" />
		<p> <strong>Title:</strong>
      <input type=\"text\" name=\"title\" id=\"title\" /> $title</p>
   <p> <strong>Year:</strong>
   </form>';
 }
 
  // below that have a check to see if the delete form has been submitted
 // or you can have this on another page.
 if (isset($_POST['deleteRecord'])) {
   $idValue = $_POST['idValue'];

   $deleteQuery = "DELETE * FROM table WHERE id = '". mysql_real_escape_string($idValue) ."' ";
   $deleteSql = mysql_query($deleteQuery);

   if ($deleteSql) { echo 'Record deleted'; }



?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP is not reading mysql DB

Post by social_experiment »

Code: Select all

<?php echo '<form action="" method="post"> ?>
In the original snippet of code i pasted for your i forgot a ' at the end of the line.

This is probably due to my error but you don't have to escape double quotations (") if you use a single quotation to incase the query

Code: Select all

<?php
echo '<form action=\"do_delete.php\" method=\"post\">
                <input type=\"hidden\" name=\"idValue\" value=\"'. $array['id'] .'\" />
                <input type=\"submit\" value=\"Delete Record\" name=\"deleteRecord\" />
                <p> <strong>Title:</strong>
      <input type=\"text\" name=\"title\" id=\"title\" /> $title</p>
   <p> <strong>Year:</strong>
   </form>';
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply