Jump to content

Recommended Posts

After finding out my last problem I have ran into another issue.....This time its deleting a topic in a forum!!!!!!!

 

Code for my delete_forum.php:

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="forum_question"; // Table name

 

// 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 value of id that sent from address bar

$id=intval($_GET['id']);

$q_id=intval($_GET['q_id']);

 

// Delete data in mysql from row that has this id

$sql="DELETE FROM forum_answer WHERE a_id='$id'";

$result=mysql_query($sql);

$result2=mysql_query("UPDATE forum_question SET reply=reply-1 WHERE id='$q_id'");

 

// if successfully deleted

if($result && $result2){

header('Location: Index.php');

}

 

else {

echo "ERROR";

}

 

?>

 

So i call this function when i click on DELETE present in my view_topic.php

<html>

<body>

<h1><u>My Forum</u></h1>

 

<center>

<font color="#000000"> </font><p><a href="index.php"><font color="#000000" size="4">Home</font></a>

<a href="delete_forum.php?id=<? echo $rows['a_id']; ?>&q_id=<? echo "$id" ?>Delete</a> 

</center>

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="forum_question"; // Table name

 

// 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 value of id that sent from address bar

$id=$_GET['id'];

 

$sql="SELECT * FROM ".$tbl_name." WHERE id='".$id."'";

$result=mysql_query($sql) or die(mysql_error());

$rows=mysql_fetch_array($result);

//mysql_close();

?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">

<tr>

<td bgcolor="#F8F7F1"><strong><?php echo $rows['topic']; ?></strong></td>

</tr>

 

<tr>

<td bgcolor="#F8F7F1"><?php echo $rows['detail']; ?></td>

</tr>

 

<tr>

<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $rows['name']; ?> <strong>Email : </strong><? echo $rows['email'];?></td>

</tr>

 

<tr>

<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $rows['datetime']; ?></td>

</tr>

</table></td>

</tr>

</table>

<BR>

<?php

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

 

$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";

$result2=mysql_query($sql2);

 

while($rows=mysql_fetch_array($result2)){

?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td bgcolor="#F8F7F1"><strong>ID</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><?php echo $rows['a_id']; ?></td>

</tr>

<tr>

<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>

<td width="5%" bgcolor="#F8F7F1">:</td>

<td width="77%" bgcolor="#F8F7F1"><?php echo $rows['a_name']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Email</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><?php echo $rows['a_email']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Answer</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>

</tr>

</table></td>

</tr>

</table><br>

 

<?php

}

 

$sql3="SELECT view FROM $tbl_name WHERE id='$id'";

$result3=mysql_query($sql3);

 

$rows=mysql_fetch_array($result3);

$view=$rows['view'];

 

// if have no counter value set counter = 1

if(empty($view)){

$view=1;

$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";

$result4=mysql_query($sql4);

}

 

// count more value

$addview=$view+1;

$sql5="update $tbl_name set view='$addview' WHERE id='$id'";

$result5=mysql_query($sql5);

 

mysql_close();

?>

<BR>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<form name="form1" method="post" action="add_answer.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td width="18%"><strong>Name</strong></td>

<td width="3%">:</td>

<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td>

</tr>

<tr>

<td><strong>Email</strong></td>

<td>:</td>

<td><input name="a_email" type="text" id="a_email" size="45"></td>

</tr>

<tr>

<td valign="top"><strong>Answer</strong></td>

<td valign="top">:</td>

<td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td>

</tr>

<tr>

<td> </td>

<td><input name="id" type="hidden" value="<?php echo $id; ?>"></td>

<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>

</tr>

</table>

</td>

</form>

</tr>

</table>

</body>

</html>

 

Now the issue is that when i click on the delete link it goes back to the index.php page but the topic still exists for some reason.

 

What I want to do is delete the topic and subtract the id from the database so wherever a user creates a new topic the numbering in the id be right!

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/146504-deleting-a-topic-in-forum/
Share on other sites

Well didn't like the code in the delete_forum.php as it was suggested by my friend so changed it so that it would be more simple for me to understand but still doesn't work.....I am pretty sure the MySQL statement is correct but it is not deleting data from the table...

 

<html>
<body>
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$id = '1';
$ok1 = @mysql_query("DELETE FROM ".$tbl_name." WHERE id='".$id."'");
if ($ok1) {
  echo('<p>Author deleted successfully!</p>');
} else {
  echo('<p>Error deleting author from database!<br />'.
       'Error: ' . mysql_error() . '</p>');
}

?>
</body>
</html>

I added some debugging lines in so you can check to SQL statements.  You were also using the @ symbol which suppresses the error messages.  Change these lines:

 

$id = '1';
$ok1 = @mysql_query("DELETE FROM ".$tbl_name." WHERE id='".$id."'");

 

to this:

 

$id = 1;
$sql = "DELETE FROM $tbl_name WHERE id='$id'";
echo $sql;
$ok1 = mysql_query($sql) or die(mysql_error());

Thanks "Maq"

 

I added your codes:

<html>
<body>
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$id = '1';
$sql = "DELETE FROM $tbl_name WHERE id='$id'";
echo $sql;
$ok1 = mysql_query($sql) or die(mysql_error());
if ($ok1) {
  echo('<p>Author deleted successfully!</p>');
} else {
  echo('<p>Error deleting author from database!<br />'.
       'Error: ' . mysql_error() . '</p>');
}

?>
</body>
</html>

 

and on the page it give me this:

DELETE FROM forum_question WHERE id='1'

 

Author deleted successfully!

 

I have a feeling there might be a problem with the id field....... ???

Well you're always setting $id = 1.  Take that line out and see if the correct $id gets passed through. 

 

You should echo out each place where the id is supposed to be read and passed to ensure that it's correct, or even there...

Well you're always setting $id = 1.  Take that line out and see if the correct $id gets passed through. 

 

You should echo out each place where the id is supposed to be read and passed to ensure that it's correct, or even there...

 

Thanks for help mate!

 

I am still a newbie so please dont mind me asking you for help mate!

 

Tried that out but since the id is not defined it give out an error saying Undefined Index.....

 

Still not sure how to echo out the id?

 

Cheers

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.