 |
|
|
|
03-10-2008, 01:32 PM
|
Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
The following code echos # was delete if i click delete. But it doesn't actually delete from the db.
PHP Code:
<?php //parse and display stats
$page = 'stats'; include('/websites/LinuxPackage02/gk/s_/uk/gks.uk.com/public_html/includes/db.php'); include('/websites/LinuxPackage02/gk/s_/uk/gks.uk.com/public_html/includes/config.php');
//if the php is called without parameters on the url, assign //the variables default starting values. if(!isset($start)){ $start = "0"; } $finish = $start + $increment;
//take all stat data from the db.
dbConnect();
$StatNo = mysql_query("select StatNo from countstats");
//echo table headings
echo(' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head > <title>The God King Scenario </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> ' . $css . '
</head>
<body>');
echo $header2; include $nav2 ;
echo('
<center><div id="heading"> <p> </p> <p><img src="http://gks.uk.com/images/stats.jpg"></p> </div> </center>
<br><center><div class="style18 style19" id="main">');
include 'menu.php';
echo(' <table border="1" cellpadding="2" cellspacing="1" bgcolor="#000000" bordercolor="#ff8000"> <tr style="color:#ff8000;"> <td width="145"><B>Page Name</B></td> <td width="100"><B>Visitor IP</B></td> <td width="100"><B>Date / Time</B></td> <td width="145"><B>Referer</B></td> <td width="70"><B>#</B></td> <td width="70">Doofery</td>
</tr>');
$deletestat=$_GET['delete']; if(isset($_GET['delete'])) { mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$StatNo'"); echo $deletestat . ' was deleted.<br>'; }
$displaystats = mysql_query("SELECT * FROM countstats"); while($row = mysql_fetch_array($displaystats)) { echo ' <tr style="color:#ff8000;"> <td width="145">'. $row['Name'] .'</td> <td width="100">'. $row['IP'] .'</td> <td width="100">'. $row['DateTime'] .'</td> <td width="145">'. $row['referer'] .'</td> <td width="70">'. $row['StatNo'] .'</td> <td width="70"><a href="displaystat.php?delete='. $row['StatNo'] .'">Delete</a></td>
'; }
//echo the data up to the finish / increment limit.
echo('</table><BR>');
//evaluate the new values for pages in next / previous linkings.
$fstart = $start + $increment; $ffinish = $finish + $increment; $bstart = $start - $increment; $bfinish = $finish - $increment;
//this part decides whether a next x link is relevant (obviously not if you are at the end of the data), and whether a //previous x link is required (obviously not if you are at the beginning of the record).
if($finish < $rows){ echo('<a href="displaystat.php?start='.$fstart.'">Show next '.$increment.'</A><BR> ');}
if($start > 0) {echo('<a href="displaystat.php?start='.$bstart.'">Show previous '.$increment.'</A><BR> ');}
//this section divides the number of rows, ie stat records, by the increment amount, thus discovering how many pages //this data will take up in the current format. It then prints the correct link with parameters to make the script show //these records.
echo('<table border="0" width="450" cellpadding="2" cellspacing="0" bgcolor="#000000"> <tr bgcolor="#000000"> Page: '); $pages = $rows / $increment; for ($x=0;$x<=$pages;$x++) { $v1 = $increment * $x; $v2 = $v1 + $increment; echo('<a href="displaystat.php?start='.$v1.'">'.$x.'</A> | '); } echo('<BR><BR></tr></table></div> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
' . $footer . ' </center> </body> </html> '); ?>
Is there something wrong? I'm getting no errors
|
|
|
|
03-10-2008, 01:54 PM
|
Re: Delete Row from MySQL
|
Posts: 5,662
Name: John Alexander
|
$deletestat=$_GET['delete'];
if(isset($_GET['delete']))
{
mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$StatNo'");
echo $deletestat . ' was deleted.<br>';
}
Why are there funny italic single quotes around your object names?
|
|
|
|
03-10-2008, 02:05 PM
|
Re: Delete Row from MySQL
|
Posts: 1,228
|
Code:
mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$StatNo'");
You also might be having some problems because of your quote marks. It looks like you might have pasted part of the code in from a different editor (like Word). Try replacing the ` around countstats and StatNo with a '.
Edit: Sorry Learning Newbie, I didn't see that you mentioned the same thing.
Last edited by VirtuosiMedia; 03-10-2008 at 02:06 PM..
|
|
|
|
03-10-2008, 03:50 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
I tried replacing the funny quotes and i even tried removing them. No luck.
|
|
|
|
03-10-2008, 04:59 PM
|
Re: Delete Row from MySQL
|
Posts: 5,662
Name: John Alexander
|
Try echoing the SQL command instead of running it, and then try pasting that into your SQL client tools, running it, and seeing if tehre's an error message and if so what it is. I can't really say more than that because I use Microsoft SQL Server and ASP.NET with C# but that's a general principal I use when this kind of thing is foobaring me.
|
|
|
|
03-10-2008, 05:42 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
Echoing the sql command, wouldn't that just remove all the stats?
Entered the command directly into phpmyadmin but replaced $StatNo with any random stat number.
|
|
|
|
03-10-2008, 06:05 PM
|
Re: Delete Row from MySQL
|
Posts: 6,442
Name: James
Location: In the ocean.
|
Try this and see if you get an error:
PHP Code:
$result = mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$StatNo'"); if (!$result) { die('Invalid query: ' . mysql_error()); }
|
|
|
|
03-10-2008, 07:07 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
Nope no error, i even added an else statement to be sure.
|
|
|
|
03-10-2008, 07:14 PM
|
Re: Delete Row from MySQL
|
Posts: 5,662
Name: John Alexander
|
Are you sure you have a valid database connection, then? Either that, or your passing in ID values that don't exist.
|
|
|
|
03-11-2008, 08:57 AM
|
Re: Delete Row from MySQL
|
Posts: 246
|
I think the problem is this:
Quote:
|
mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$StatNo'");
|
What you probably want to do is:
Quote:
|
mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$deletestat'");
|
|
|
|
|
03-11-2008, 05:26 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
Yeh, i guess i was. It worked, thanks
|
|
|
|
03-11-2008, 06:17 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
How can I add a checkbox to delete several stats at once.
|
|
|
|
03-11-2008, 08:07 PM
|
Re: Delete Row from MySQL
|
Posts: 246
|
Inside your <form> tags, add something like this:
HTML Code:
<input type="checkbox" name="deleteid1" value="1">
<input type="checkbox" name="deleteid2" value="2">
...
And in your php code, add this:
PHP Code:
$numberOfDeleteBoxes = 50; // or however many you have
for ($i = 0; $i <= $numberOfDeleteBoxes; $i++) {
if (isset($_POST["deleteid{$i}"])) {
$deleteStat = $_POST["deleteid{$i}"];
mysql_query("DELETE FROM `countstats` WHERE `StatNo` = '$deleteStat'");
}
}
(All the above is untested, but you get my drift.  )
|
|
|
|
03-12-2008, 04:26 AM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
thanks, but the stats are increasing all time (auto_increment)
how can I make it relate to that
|
|
|
|
03-12-2008, 06:14 AM
|
Re: Delete Row from MySQL
|
Posts: 246
|
Same principles, just use PHP to loop through your data.
Inside the <form> tags:
PHP Code:
<?
$numberOfDeleteBoxes = 0;
$sqlresult = mysql_query("select distinct StatNo from countstats") or die("Error selecting StatNo");
while ($row = mysql_fetch_array($sqlresult)) {
$numberOfDeleteBoxes++;
$statno = $row["StatNo"];
echo "<input type=\"checkbox\" name=\"deleteid{$numberOfDeleteBoxes}\" value=\"{$statno}\">";
}
?>
Wherever you do the deleting:
PHP Code:
<?
$sqlresult = mysql_query("select count(*) as numberOfDeleteBoxes from countstats") or die("Error selecting count");
$row = mysql_fetch_array($sqlresult);
$numberOfDeleteBoxes = $row["numberOfDeleteBoxes"];
for ($i = 0; $i <= $numberOfDeleteBoxes; $i++) {
if (isset($_POST["deleteid{$i}"])) {
$deleteStat = $_POST["deleteid{$i}"];
mysql_query("delete from `countstats` where `StatNo` = '$deleteStat'");
}
}
?>
|
|
|
|
03-12-2008, 12:35 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
this makes 19 boxes on each stat.
heres my code.
PHP Code:
<?php //parse and display stats
$page = 'stats'; include('/websites/LinuxPackage02/gk/s_/uk/gks.uk.com/public_html/includes/db.php'); include('/websites/LinuxPackage02/gk/s_/uk/gks.uk.com/public_html/includes/config.php');
//if the php is called without parameters on the url, assign //the variables default starting values. if(!isset($start)){ $start = "0"; } $finish = $start + $increment;
//take all stat data from the db.
dbConnect();
$StatNo = mysql_query("select StatNo from countstats");
//echo table headings
echo(' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head > <title>The God King Scenario </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> ' . $css . '
</head>
<body>');
echo $header2; include $nav2 ;
echo('
<center><div id="heading"> <p> </p> <p><img src="http://gks.uk.com/images/stats.jpg"></p> </div> </center>
<br><center><div class="style18 style19" id="main">');
include 'menu.php';
echo '<form action="displaystat.php" method=post">';
echo(' <table border="1" cellpadding="2" cellspacing="1" bgcolor="#000000" bordercolor="#ff8000"> <tr style="color:#ff8000;"> <td width="145"><B>Page Name</B></td> <td width="100"><B>Visitor IP</B></td> <td width="100"><B>Date / Time</B></td> <td width="145"><B>Referer</B></td> <td width="70"><B>#</B></td> <td width="70">Doofery</td>
</tr>');
$sqlresult = mysql_query("select count(*) as numberOfDeleteBoxes from countstats") or die("Error selecting count"); $row = mysql_fetch_array($sqlresult); $numberOfDeleteBoxes = $row["numberOfDeleteBoxes"]; for ($i = 0; $i <= $numberOfDeleteBoxes; $i++) { if (isset($_POST["deleteid{$i}"])) { $deleteStat = $_POST["deleteid{$i}"]; mysql_query("delete from `countstats` where `StatNo` = '$deleteStat'"); } }
$deletestat=$_GET['delete']; if(isset($_GET['delete'])) { mysql_query("DELETE FROM countstats WHERE StatNo = '$deletestat'"); echo $deletestat . ' was deleted.<br>'; }
$displaystats = mysql_query("SELECT * FROM countstats"); while($row = mysql_fetch_array($displaystats)) { echo ' <tr style="color:#ff8000;"> <td width="145">'. $row['Name'] .'</td> <td width="100">'. $row['IP'] .'</td> <td width="100">'. $row['DateTime'] .'</td> <td width="145">'. $row['referer'] .'</td> <td width="70">'. $row['StatNo'] .'</td> <td width="70">'; $numberOfDeleteBoxes = 0; $sqlresult = mysql_query("select distinct StatNo from countstats") or die("Error selecting StatNo"); while ($row = mysql_fetch_array($sqlresult)) { $numberOfDeleteBoxes++; $statno = $row["StatNo"]; echo "<input type=\"checkbox\" name=\"deleteid{$numberOfDeleteBoxes}\" value=\"{$statno}\">"; }
echo '</td>
'; }
//echo the data up to the finish / increment limit.
echo('</table><BR>');
echo '<input type="submit" value="delete"></form>';
//evaluate the new values for pages in next / previous linkings.
$fstart = $start + $increment; $ffinish = $finish + $increment; $bstart = $start - $increment; $bfinish = $finish - $increment;
//this part decides whether a next x link is relevant (obviously not if you are at the end of the data), and whether a //previous x link is required (obviously not if you are at the beginning of the record).
if($finish < $rows){ echo('<a href="displaystat.php?start='.$fstart.'">Show next '.$increment.'</A><BR> ');}
if($start > 0) {echo('<a href="displaystat.php?start='.$bstart.'">Show previous '.$increment.'</A><BR> ');}
//this section divides the number of rows, ie stat records, by the increment amount, thus discovering how many pages //this data will take up in the current format. It then prints the correct link with parameters to make the script show //these records.
echo('<table border="0" width="450" cellpadding="2" cellspacing="0" bgcolor="#000000"> <tr bgcolor="#000000"> Page: '); $pages = $rows / $increment; for ($x=0;$x<=$pages;$x++) { $v1 = $increment * $x; $v2 = $v1 + $increment; echo('<a href="displaystat.php?start='.$v1.'">'.$x.'</A> | '); } echo('<BR><BR></tr></table></div> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
' . $footer . ' </center>
</body> </html> '); ?>
|
|
|
|
03-12-2008, 02:22 PM
|
Re: Delete Row from MySQL
|
Posts: 246
|
To solve the duplicate items you need to remove the nested loop where you are displaying the <input> checkboxes.
Other good ideas:
This line, nearish the top can be removed:
Code:
$StatNo = mysql_query("select StatNo from countstats");
Looping through to test if the boxes are checked for deletion should be done inside this if statement:
Code:
if(isset($_GET['delete'])) {
Naturally, before the code goes live, you will want to lock down the code to prevent sql injection.
|
|
|
|
03-12-2008, 02:49 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
Quote:
Originally Posted by CouponGuy
To solve the duplicate items you need to remove the nested loop where you are displaying the <input> checkboxes.
|
How would I do this?
|
|
|
|
03-12-2008, 07:35 PM
|
Re: Delete Row from MySQL
|
Posts: 246
|
Currently you have:
PHP Code:
$displaystats = mysql_query("SELECT * FROM countstats");
while($row = mysql_fetch_array($displaystats))
{
echo '
<tr style="color:#ff8000;">
<td width="145">'. $row['Name'] .'</td>
<td width="100">'. $row['IP'] .'</td>
<td width="100">'. $row['DateTime'] .'</td>
<td width="145">'. $row['referer'] .'</td>
<td width="70">'. $row['StatNo'] .'</td>
<td width="70">';
$numberOfDeleteBoxes = 0;
$sqlresult = mysql_query("select distinct StatNo from countstats") or die("Error selecting StatNo");
while ($row = mysql_fetch_array($sqlresult)) {
$numberOfDeleteBoxes++;
$statno = $row["StatNo"];
echo "<input type=\"checkbox\" name=\"deleteid{$numberOfDeleteBoxes}\" value=\"{$statno}\">";
}
echo '</td>
';
}
I would change it to:
PHP Code:
$numberOfDeleteBoxes = 0;
$displaystats = mysql_query("SELECT * FROM countstats");
while($row = mysql_fetch_array($displaystats)) {
echo '
<tr style="color:#ff8000;">
<td width="145">'. $row['Name'] .'</td>
<td width="100">'. $row['IP'] .'</td>
<td width="100">'. $row['DateTime'] .'</td>
<td width="145">'. $row['referer'] .'</td>
<td width="70">'. $row['StatNo'] .'</td>
<td width="70">';
$numberOfDeleteBoxes++;
$statno = $row["StatNo"];
echo "<input type=\"checkbox\" name=\"deleteid{$numberOfDeleteBoxes}\" value=\"{$statno}\">";
echo '</td>
';
}
|
|
|
|
03-13-2008, 04:46 PM
|
Re: Delete Row from MySQL
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
Now nothing deletes.
|
|
|
|
|
« Reply to Delete Row from MySQL
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|