MySql Delete Comment

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

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Output buffering is a band-aid 99.999% of the time when it's used for header() issues.

Use full URLs with header redirections.. If you want to know why, search the multitude of times I've said this.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This does not require AJAX at all. Just post the page back to itself...

Code: Select all

<?php
include 'config.php';

mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Cannot select DB');

// Check to see if there is a request to delete a record first
if (isset($_GET['act']) && $_GET['act'] == 'del') 
{
  // Now check to see if there is an ID selected for this request
  if (isset($_GET['id']) && $_GET['id'] != null && is_numeric($_GET['id']))
  {
    // There is, so remove the record, then select the list
    $id = $_GET['id'];

    $sql_delete = "DELETE FROM `deltest1` WHERE `cid` = $id";
    mysql_query($sql_delete) or die(mysql_error()); // gives errors
    echo 'Record id ' . $id . ' has been deleted...<br />';
  }
}

// This query is your list builder, minus the recently deleted record
$query  = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
echo '<table>';
while ($row = mysql_fetch_array($result))
{
  echo $row['csubject'] . $row['cmessage'];
  echo '<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '?act=del&id=' . $row['cid'] . '" title="Delete"><img src="http://image.fpsbanana.com/ico/del.gif"></a>';
}
echo '</table>';
?>
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

wow, thanks for that code.
its alot better now. but the only change is that it says "Comment Deleted" at the top, it still displays the comment instead of showing that the comment has been deleted by not showing it :/
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is this hosted someplace where we can see it in action?
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

yea, here:
http://csscobalt.com/to_do_list_fpsb/t1/delete9.php

i just have random nonsence entries for right now.
Last edited by JustinMs66 on Wed Dec 06, 2006 2:19 pm, edited 1 time in total.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You need to run this:

Code: Select all

COMMIT;
or:

Code: Select all

SET AUTOCOMMIT=1;
or:

Code: Select all

COMMIT WORK
or:

Code: Select all

COMMIT
This will commit the delete sentence in the DB.

For more information see:
http://dev.mysql.com/doc/refman/5.0/en/commit.html
http://www.nis.com/oracle/sql_commit_work.html
http://publib.boulder.ibm.com/iseries/v ... 4comit.htm
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok wrote:You need to run this:

Code: Select all

COMMIT;
where would i put "COMMIT;" ?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

After the DELETE statement.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok i tried that, using this code:

Code: Select all

$sql_delete = "DELETE FROM `deltest1` WHERE `cid` = $id";
    mysql_query($sql_delete) or die(mysql_error()); // gives errors
    COMMIT;
    echo 'Record id ' . $id . ' has been deleted...<br />';
but it still only says that text at the top, it dosnt look like it's been deleted until you refresh :/

go here to test:
http://csscobalt.com/to_do_list_fpsb/t1/delete10.php
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

LOL...

COMMIT is a SQL statement:

Code: Select all

mysql_query("COMMIT") or die(mysql_error()); // gives errors
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Justin, please refrain from bumping your thread.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
Try this code and see if it changes anything. I can't imagine why you would need a COMMIT here, as this should delete the record as soon as you send the query to the database.

Code: Select all

<?php
include 'config.php';

mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Cannot select DB');

// Check to see if there is a request to delete a record first
if (isset($_GET['act']) && $_GET['act'] == 'del')
{
  // Now check to see if there is an ID selected for this request
  if (isset($_GET['id']) && $_GET['id'] != null && is_numeric($_GET['id']))
  {
    // There is, so remove the record, then select the list
    $id = $_GET['id'];

    $sql = "DELETE FROM `deltest1` WHERE `cid` = $id";
    $result = mysql_query($sql);
    if (!$result || !mysql_affected_rows() 
    {
        die(mysql_error()); // gives errors
    }
    else
    {
        echo 'There were ' . mysql_affected_rows() . ' deleted from the table, including record id ' . $id . ' ...<br />';
    }
  }
}

// This query is your list builder, minus the recently deleted record
$sql  = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($sql) or die('Error, query failed ##1');
echo '<table>';
while ($row = mysql_fetch_array($result))
{
  echo $row['csubject'] . $row['cmessage'];
  echo '<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '?act=del&id=' . $row['cid'] . '" title="Delete"><img src="http://image.fpsbanana.com/ico/del.gif"></a>';
}
echo '</table>';
?>
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok. cool. EVERYTHING works Perfectly now.
using this code:

Code: Select all

<?php
include('config.php');
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Cannot select DB');
$query  = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
if (isset($_GET['act']) && $_GET['act'] == 'del')
{
  if (isset($_GET['id']) && $_GET['id'] != null && is_numeric($_GET['id']))
  {
    $id = $_GET['id'];
    $sql_delete = "DELETE FROM `deltest1` WHERE `cid` = $id";
    mysql_query($sql_delete) or die(mysql_error()); // gives errors
    mysql_query("COMMIT") or die(mysql_error()); // gives errors 
    echo 'Record id ' . $id . ' has been deleted...<br />';
  }
} 
while ($row = mysql_fetch_array($result)) 
{
    $id = $row["cid"];

    echo $row["csubject"] . $row["cmessage"];

  echo '<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '?act=del&id=' . $id . '" title="Delete"><img src="http://image.fpsbanana.com/ico/del.gif"></a>'; 
}
?>
anyway, what i need to do now, is this:
i want, when a user first looks at this page, for it do display the comments, but WITHOUT the trash can image. and at the top of the page, their will be a login box. now once the user logs in, the trash can images will be displayed, and the login box will disapear. can you help me with this?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, I can help with that. But first you need to try it. Think about the logic behind. Then write the logic out in the form of code comments. Then code the comments and try it out. If there are problems with what you try post back and we will help you.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

Everah wrote:Yes, I can help with that. But first you need to try it. Think about the logic behind. Then write the logic out in the form of code comments. Then code the comments and try it out. If there are problems with what you try post back and we will help you.
ok i think i can code the user login thing, but for the display code, would it have to be a cookie? like this:
first, where i have this code:

Code: Select all

echo '<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '?act=del&id=' . $id . '" title="Delete"><img src="http://image.fpsbanana.com/ico/del.gif"></a>';
would have to be where i put my code. now it would be something like(logically)
if cookie.content = 1 then }
echo "image code";
else if cookie.content = 0 then{
echo "login to trash comments";
}

would it be something like that logically? or is their a more efective way to do it then using cookies?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Whatever mechanism you use to manage user sessions will tell you what you need to know.

Code: Select all

<?php
while ($row = mysql_fetch_array($result))
{
    $id = $row["cid"];

    echo $row["csubject"] . $row["cmessage"];

    if ($user_is_logged_in === true) 
    {
        echo '<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '?act=del&id=' . $id . '" title="Delete"><img src="http://image.fpsbanana.com/ico/del.gif"></a>';
    }
}
?>
But again, try something first and see if it works.
Post Reply