PDA

View Full Version : Simple script problem


0810
11-29-2002, 08:54 PM
hi how are you doing? What I am doing is that When a user visits my guestbook, sometimes I would like to delete some users Recordes. Therefore, when I check the checkbox and click "submit" butoon, it automatically delete user's information(name,location,url,comments,time).

my script doesn't work, so could you help me out for my script.

Thanks your guys. my php is 4.23


if(isset($submit)){
$sql="delete from guestbook ";


}else{

$sql="select name,location,url,comments,date_format(msgtime, \"%Y/%m/%d %H:%i:%s\") as mtime from guestbook order by msgtime desc";

}
$result=mysql_query("$sql") or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo"<body background=\"/image/back.jpg\">";
echo"<center>";
echo"<td><b><i><font color=\"red\" size=5 >Name:</i></font></b><td></tr>";
echo "<tr><td>".$row["name"]."</td></tr>";
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >Location:</font></b>";
echo $row["location"];
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >url:</font></b>";
echo $row["url"];
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >Commments:</font></b>";
echo $row["comments"];
echo "<br>\n";
echo "<b><font color=\"red\" size=5>Time</font></b>";
echo $row["mtime"];
echo "<br>\n";
echo "<br>\n";
echo "<form action=\"guestbookview.php\" method=\"post\">";
echo "Delete Record for user";
echo "<input type=\"checkbox\" name=\"guestbook\" value=\"guestbook\"><br>\n";
echo "<br>\n";
echo "<br>\n";



echo "--------------------------------------------------------------------------------------------------------";
echo "</center>";
echo "<br>\n";
echo "<br>\n";

}

mysql_free_result($result);

?>
<input type="submit" name="submit" value="Submit to delete">
</form>

0810
11-30-2002, 01:41 AM
Does anybody help my script?

Thanks

firepages
11-30-2002, 02:09 AM
try... changing
the checkbox string to...

<?
echo "<input type=\"checkbox\" name=\"guestbook[]\" value=\"$row["name"]\"><br>\n";
?>


then your sql would be


<?
$STR="WHERE name='".implode("' AND name='",$HTTP_POST_VARS['guestbook'])."'";
$sql="delete from guestbook $STR";
?>


but that will delete ALL records for each name checked as opposed to a particular record, you perhaps should add an ID to your database table.

and you will get an error on the mysql_fetch_array after a successfull delete so you should catch that.

<edit> dang forum wont let me show escaped single quotes</edit>

0810
11-30-2002, 02:19 AM
Hi thanks but not working now. What I want to do is let's say three people signed up my guestbook,then I would like to delete only one. When I check a checkbox and click submit button, it automatically delete one usr's information. not all of them. I tried your way, but not working.
I got two errors. Could you help me my script. Thanks


PHP:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/itohideo/public_html/guestbookview.php on line 33

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/itohideo/public_html/guestbookview.php on line 67



I fixed like this






if(isset($submit)){

$sql="delete from guestbook where message_id='$message_id'";


}else{

$sql="select name,location,url,comments,date_format(msgtime, \"%Y/%m/%d %H:%i:%s\") as mtime from guestbook order by msgtime desc";

}




$result=mysql_query("$sql") or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo"<body background=\"/image/back.jpg\">";
echo"<center>";
echo"<td><b><i><font color=\"red\" size=5 >Name:</i></font></b><td></tr>";
echo "<tr><td>".$row["name"]."</td></tr>";
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >Location:</font></b>";
echo $row["location"];
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >url:</font></b>";
echo $row["url"];
echo"<br>\n";
echo"<b><font color=\"red\" size=5 >Commments:</font></b>";
echo $row["comments"];
echo "<br>\n";
echo "<b><font color=\"red\" size=5>Time</font></b>";
echo $row["mtime"];
echo "<br>\n";
echo "<br>\n";
echo "<form action=\"guestbookview.php\" method=\"post\">";
echo "Delete Record for user";
echo "<input type=\"checkbox\" name=\"guestbook\" value=\"message_id\"><br>\n";
echo "<br>\n";
echo "<br>\n"
;


echo "--------------------------------------------------------------------------------------------------------";
echo "</center>";
echo "<br>\n";
echo "<br>\n";

}

mysql_free_result($result);

?>

<input type="hidden" name="message_id" value="<?php echo $row['message_id']?>">
<input type="submit" name="submit" value="Submit to delete">
</form>

firepages
11-30-2002, 02:32 AM
<EDIT />I missed a bit !

lol actually it wont work anyway I did not see the way you were working the form, & you cant do things like include the <body> tags in your loops, we only need it once :)

just add your connection


<?
if(isset($HTTP_POST_VARS['submit']) && isset($HTTP_POST_VARS['guestbook'])){
$STR="WHERE name='".implode("' AND name='",$HTTP_POST_VARS['guestbook'])."'";
$sql="delete from guestbook $STR";
$result=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<body background="/image/back.jpg">
<form action="guestbookview.php" method="post">
<center>
<table>
<?
//your connection ?//
$sql="select name,location,url,comments,date_format(msgtime, \"%Y/%m/%d %H:%i:%s\") as mtime from guestbook order by msgtime desc";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){
?>
<tr><td>
<b><i><font color="red" size=5 >Name:</i></font></b>
<?=$row["name"];?>
<br />
<b><font color="red" size=5 >Location:</font></b>
<?=$row["location"];?>
<br />
<b><font color="red" size=5 >url:</font></b>
<?=$row["url"];?>
<br />
<b><font color="red" size=5 >Commments:</font></b>
<?=$row["comments"];?>
<br />
<b><font color="red" size=5>Time</font></b>
<?=$row["mtime"];?>
<br /><br />
Delete Record for user
<input type="checkbox" name="guestbook[]" value="<?=$row['name'];?>" />
<br /><br />
</td></tr>

<?
}
?>

</table>
<input type="submit" name="submit" value="Submit to delete">
</form>
</center>
--------------------------------------------------------------------------------------------------------
<br />
<br />
</body>
</html>



I would suggest you get used to breaking in and out of <? php tags as the end result is much more readable (IMO) than
echo ...
echo....
echo...

& if we can read it then we can help !

0810
11-30-2002, 06:06 AM
I tested but not working. I got an error which said "No Database Selected"

I seted my database right but not working

my database is itohideo_guestbook


please help me again Thanks


<?
if(isset($_POST['submit']) && isset($_POST['guestbook'])){
$STR="WHERE name='".implode("' AND name='",$_POST['guestbook'])."'";
$sql="delete from guestbook $STR";
$result=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<body background="/image/back.jpg">
<form action="guestbookview.php" method="post">
<center>
<table>
<?
$dc = mysql_pconnect("localhost","itohideo_gi","1234567");
$dt=mysql_select_db("itohideo_guestbook",$dc) or die("can't select your database");
$msgtime="date_format(msgtime,\"%Y/%m%d %H:%i:%s\")as mtime";
$sql="select message_id, name,location,url,comments,date_format(msgtime, \"%Y/%m/%d %H:%i:%s\") as mtime from guestbook order by msgtime desc";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){
?>
<tr><td>

<b><i><font color="red" size=5 >id:</i></font></b>
<?=$row["message_id"];?>
<br />

<b><i><font color="red" size=5 >Name:</i></font></b>
<?=$row["name"];?>
<br />
<b><font color="red" size=5 >Location:</font></b>
<?=$row["location"];?>
<br />
<b><font color="red" size=5 >url:</font></b>
<?=$row["url"];?>
<br />
<b><font color="red" size=5 >Commments:</font></b>
<?=$row["comments"];?>
<br />
<b><font color="red" size=5>Time</font></b>
<?=$row["mtime"];?>
<br /><br />
Delete Record for user
<input type="checkbox" name="guestbook[]" value="<?=$row['message_id'];?>" />
<br /><br />
</td></tr>

<?
}
?>

</table>
<input type="submit" name="submit" value="Submit to delete">
</form>
</center>
--------------------------------------------------------------------------------------------------------
<br />
<br />
</body>
</html>

firepages
11-30-2002, 06:31 AM
<?
mysql_connect("localhost","itohideo_gi","1234567");
mysql_select_db("itohideo_guestbook") or die("can't select your database");
?>