Page 2 of 5

Posted: Mon Dec 04, 2006 4:02 pm
by ok
I tested the code that I gave you and it works. So, try again and check that you pass 'id' to the PHP file (?id=1).

Posted: Mon Dec 04, 2006 4:13 pm
by JustinMs66
ok wrote:I tested the code that I gave you and it works. So, try again and check that you pass 'id' to the PHP file (?id=1).
ok well it dosn't work for me, so here is the EXACT code i'm using (to test it)

Code: Select all

<?php
$dbhost = 'my host';
$dbuser = 'my user';
$dbpass = 'my pass';
$dbname = 'phptest1';

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

if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
 $id = $_GET['id'];
}
else
{
 die('No id');
}

$sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
$result = mysql_query($sql_delete);
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
}
?>
and i mean i''m not exactly sure where ID is getting ID from. like where is it getting the message ID!?

anyway, with that code, i get this:
No id

Posted: Mon Dec 04, 2006 4:16 pm
by Zoxive
Are you putting ?id=104 at the end?

example.. http://www.website.com/delete.php?id=104

104 being the id that will get deleted.

Posted: Mon Dec 04, 2006 4:19 pm
by JustinMs66
well i cant put id=4 because it needs to be "id=$id" because it needs, in the loop, to store the ID of each message or something.

you know?

and currently, all it does is refresh the page when you click the image

Posted: Mon Dec 04, 2006 4:25 pm
by ok
You have it online?

Posted: Mon Dec 04, 2006 4:27 pm
by JustinMs66
yes... here, here is the exact code:

Code: Select all

<?php

$dbhost =
$dbuser =
$dbpass = 
$dbname =
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$query  = "SELECT `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');

$htmlsubbefore="<div class=\"ts hazardrow\"><div class=\"coltitle_dark tlarge\">";
$htmlsubafter="</div><div style=\"clear:both;padding:0px\"></div></div>";

$htmlbodbefore="<table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\"><tr class=\"hazardrow\"><td width=\"50%\" colspan=\"1\">";

// ID check:
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
 $id = $_GET['id'];
}
else
{
 //die('No id');
} 
//end ID check

//deltion check
if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
 mysql_query($sql_delete) or die(mysql_error()); // gives errors
}
// END DELETE CODE

echo "<table>";
while($row = mysql_fetch_row($result))
{
    $Subject    = $row[0];
    $Message = $row[1];
ECHO $row[2];
ECHO $row[3];

    echo $htmlsubbefore . $row[0] . $htmlsubafter.
         $htmlbodbefore . $row[1] . $htmlbodafter . "</table>";

echo "<a href='delete7.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
// $MESSAGE_ID  $_GET['id'] $id
}
   $htmlbodafter="</td>/tr></table>";
?>

Posted: Mon Dec 04, 2006 4:35 pm
by RobertGonzalez
A little PHP 101. Values passed by forms are available in the $_POST array. Values passed by Query String (URL) are available in the $_GET array.

Posted: Mon Dec 04, 2006 4:36 pm
by ok
Now I understand. It goes like that:

Code: Select all

//...
$query  = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
//...
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
 $id = $_GET['id'];
}
else
{
 die('No id');
}
//end ID check

//deltion check
if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
 mysql_query($sql_delete) or die(mysql_error()); // gives errors
}
// END DELETE CODE

echo "<table>";
while($row = mysql_fetch_assoc($result))
{
    $Subject    = $row["csubject"];
    $Message = $row["cmessage"];
    //============================
    //***********************************
    $id = $row["cid"];
    //***********************************
    //============================
ECHO $row[2];
ECHO $row[3];

    echo $htmlsubbefore . $row[0] . $htmlsubafter.
         $htmlbodbefore . $row[1] . $htmlbodafter . "</table>";

echo "<a href='delete7.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
// $MESSAGE_ID  $_GET['id'] $id
} 
//...

Posted: Mon Dec 04, 2006 4:39 pm
by RobertGonzalez
You might need to speak the logic out loud to yourself so you can understand what you want to take place. You are pulling information from a database table (one piece of which is a record ID) which you are using to populate a list which is used to push a value to a page that receives the data processes a query based on it. Does that make sense to you? If so, code that process I just described.

Posted: Tue Dec 05, 2006 1:33 pm
by JustinMs66
ok first of all, i want to thank ALL of you for your help
i finally got almost everything to work, although i had to tweak it a bit.

here is the code i used:

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['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
 $id = $_GET['id'];
}
else
{
}

if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
 mysql_query($sql_delete) or die(mysql_error()); // gives errors
}

echo "<table>";
while($row = mysql_fetch_assoc($result))
{
    $Subject    = $row["csubject"];
    $Message = $row["cmessage"];
    $id = $row["cid"];

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

echo "<a href='delete8.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
}
?>
now their is just 1 more thing. after you click the image, it does delete it, but unless you refresh the page, it still shows the entry. is their a way so that once its deleted, it somehow refreshes but not refreshes? lol i mean like without refreshing the whole page?

Posted: Tue Dec 05, 2006 1:37 pm
by ok

Posted: Tue Dec 05, 2006 1:40 pm
by JustinMs66
ok wrote:You mean AJAX
oh it would have to be done with AJAX? damn. i am trying to avoid learning ajax lol. a little hard.
if their a tutorial specific to comment updating/refreshing you could link me to?

Posted: Tue Dec 05, 2006 1:59 pm
by ok
See viewtopic.php?p=335639#335639 for more AJAX links.

Posted: Tue Dec 05, 2006 2:52 pm
by JustinMs66
ok thanks i'l try to do it that way.
but if i cant, whats the PHP code to "Refresh" the page, or redirect?
i assume i would put it here:

Code: Select all

if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
 mysql_query($sql_delete) or die(mysql_error()); // gives errors
    --- REDIRECT CODE HERE ---
}

Posted: Tue Dec 05, 2006 3:03 pm
by ok
You have to make sure that you don't print nothing before the redirect.

Code: Select all

if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
 mysql_query($sql_delete) or die(mysql_error()); // gives errors
 
 header("Location: next.php");
}
P.S
If you don't have control on the output to the browser before the redirect header, you can put ob_start() in the start of the PHP code and before you redirecr add ob_clean_end().