SQL statements in my PHP Page??

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
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

SQL statements in my PHP Page??

Post by otd »

This bit of code below is to delete an answer from my forum, then once deleted to go to the question table in mysql and - 1 off the reply column for the question the answer has just been deleted off, the answer is being deleted but the reply column is not dropping down one, wondering if any one can help me, the code for it is shown below.

Code: Select all

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form.
$id=$_GET['a_id']; 

// Do delete statement. 
mysql_query("delete from forum_answer where a_id='$id'");

$tbl_name2="forum_question"; // Switch to table "forum_question" 

// Get values from form.
$id=$_GET['id']; 

// Do delete statement. 
mysql_query("UPDATE forum_question SET reply = reply - 1 WHERE id = $id");

// Close database connection
mysql_close(); 

// Redirect to select.php.
header("location:main_forum.php");
This is the part that i believe to be the problem not sure whether ive written it right

Code: Select all

$tbl_name2="forum_question"; // Switch to table "forum_question" 

// Get values from form.
$id=$_GET['id']; 

// Do delete statement. 
mysql_query("UPDATE forum_question SET reply = reply - 1 WHERE id = $id");
Any help would be greatly appreciated

Many Thanks In Adavance

Chris
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);


// No need for "$var", just $var.
$mysql = mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name, $mysql)or die("cannot select DB");

$id=(int)$_GET['a_id'];

$query = "delete from forum_answer where a_id=$id";
mysql_query($query, $mysql) or die(mysql_error().': '. $query);
echo 'deleted: ', mysql_affected_rows($mysql), "<br />\n";

$query = "UPDATE forum_question SET reply = reply - 1 WHERE id=$id";
mysql_query($query, $mysql) or die(mysql_error().': '.$query);
echo 'update: ', mysql_affected_rows($mysql), "<br />\n";

// header("location:main_forum.php"); later
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

ok that deleted all answers but didnt adjust the reply figure, even thou it said

deleted 4
update 1

any ideas??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

4 records were deleted but reply = reply - 1 was executed only once.
otd
Forum Newbie
Posts: 18
Joined: Mon May 21, 2007 8:50 am

Post by otd »

yeah guess so but i was after it to just delete one answer, the one im deleting, but its deleted every answer in the table, and from what i can see even though it says it has adjust the reply column i cant see it has done this as the figues all seem the same.

Any ideas??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If it deletes more records than you want then either your condition where a_id=$id is wrong or the value you assign to the field a_id when inserting a new reocrd.
Post Reply