Ok, I have edited the hell out of this script because it seems like your new to PHP, HTML, and CSS and while the style your using works it is not the most efficient. The following are not criticism of your work they are just suggestions to save you some time in the future and make your pages faster.
First there is no need to header your php files with all this the title maybe. But you have a javascript function your not even implementing. You do not need all this extra HTML code in a php file. [php]
Testing delete with check box
[/php]
Second don’t use the generic text warning when in the die command with mysql, using mysql_error() will give you a more accurate description of the error that occurred, this is important when your variables are in fact varying not static. [php]$result=mysql_query($sql) or die(“error in query”); // BAD
$result=mysql_query($sql) or die(mysql_error()); // GOOD
$result=mysql_query($sql) or die(“The following error occurred with your query
”.mysql_error()); // BETTER
[/php]
Third when using tables in HTML the values default to zero unless you have changed that somewhere with CSS. So there is no need to include them. Also when your going to use large blocks of HTML you can store them in a variable and then echo that variable later (see my final code $display_block)
Fourth CSS color codes are six characters but when your are using the same six characters (#ffffff) you only need three. So #ffffff can be #fff also white is the default so there is not a need to define it on each line.
Fifth if you want to align an entire table to center just use CSS on the table heading or on your styles sheet.
[php]<table style=“text-align:center;” width=“400” cellspacing=“1”>
[/php]
Sixth tables has a heading feature that bolds the entries for static table heading use
instead of | .
Seventh tabbing (this should be first) code is much easier to read if you tab your code into sections
[php]$display_block = "
<form name=“form1” method=“post”>
<table width=“400” cellpadding=“3” border=“1” cellpadding=“3” cellspacing=“0” style=“text-align:center;” >
|
<td colspan=“4” >Delete multiple rows in mysql
# |
PICID |
class |
brand |
model |
";
[/php]
eighth nested tables? Why? If your just trying to get the gray border on your table I would again use CSS and just define the border color like so.
[php]<table style=“text-align:center; border-color:#ccc;” width=“400” cellpadding=“3” border=“1” cellpadding=“3” cellspacing=“0” >
[/php]
nine there is a very simple way to reload the page.
[php]header(“Location: a file name here”);[/php]
Ok Here is my final revision of your code. I have eliminated almost 30 lines of code.
[php]
Deleting Multiple Records using PHP & MySQL
<?php
$host="127.0.0.1"; // Host name
$username="Gradschool"; // Mysql username
$password=''; // Mysql password
$db_name="playing"; // Changed database name
$tbl_name="car";
mysql_connect($host, $username, $password)or die(mysql_error());
mysql_select_db($db_name)or die(mysql_error());
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql) or die(mysql_error());
$count=mysql_num_rows($result);
echo $_REQUEST['sql_delete']; //echo the sql line from the address bar
$display_block = "
|
Delete multiple rows in mysql |
# |
PICID |
class |
brand |
model |
";
while ($rows = mysql_fetch_array($result)) {
$display_block .= "
|
".$rows['picid']." |
".$rows['class']." |
".$rows['brand']." |
".$rows['model']." |
";
}
$display_block .= "
|
Record count:".number_format($count)."
";
echo $display_block;
if (!$_REQUEST) {
} else {
if(isset($_REQUEST['delete'])) {
$checkbox = $_REQUEST['checkbox'];
for($i=0;$i<count($_REQUEST['checkbox']);$i++) {
$del_id = $checkbox[$i];
$sql_delete="DELETE FROM $tbl_name WHERE picid='".$del_id."'";
print $sql_delete." ";
$result_delete = mysql_query($sql_delete);
header("Location: phphelp_delete_rows_7.12.12.php?sql_delete=".$sql_delete.""); // this is then name of my file you need to change it to yours.
}
}
}
mysql_close();
?>[/php]
I assume you do not want to see the SQL it was simply for testing purposes, but I left it in and passed it as a string through the address bar so that when the page reloaded you had your SQL from the deleted line. I would remove this before any production stages. |