MySql Delete Comment
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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>';
?>- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
yea, here:
http://csscobalt.com/to_do_list_fpsb/t1/delete9.php
i just have random nonsence entries for right now.
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.
You need to run this:
or:
or:
or:
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
Code: Select all
COMMIT;Code: Select all
SET AUTOCOMMIT=1;Code: Select all
COMMIT WORKCode: Select all
COMMITFor 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
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
where would i put "COMMIT;" ?ok wrote:You need to run this:Code: Select all
COMMIT;
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok i tried that, using this code:
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
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 />';go here to test:
http://csscobalt.com/to_do_list_fpsb/t1/delete10.php
LOL...
COMMIT is a SQL statement:
COMMIT is a SQL statement:
Code: Select all
mysql_query("COMMIT") or die(mysql_error()); // gives errors- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Justin, please refrain from bumping your thread.
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.[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.
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>';
?>- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok. cool. EVERYTHING works Perfectly now.
using this code:
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?
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>';
}
?>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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok i think i can code the user login thing, but for the display code, would it have to be a cookie? like this: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.
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>';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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Whatever mechanism you use to manage user sessions will tell you what you need to know.
But again, try something first and see if it works.
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>';
}
}
?>