I want on the code following delete one or few rows (multiple rows) in the mysql, But unfortunately dont work,testing on the server and localhost but dont work.i want just use css and div, dont use tabel,i want act on the local and the server,please help me.
[LIST=1]
[]The meta tag will not work outside the head element on an HTML document, so that method is out.
[]Your loop will only validate if the last delete statement worked, not all of them
[]You can use the header instruction instead, but you’ll need to reorder the code to accomplish what you want.
[]A non-php word of advice - avoid inline styles as much as possible. You’ll save yourself a lot of headaches. You can use multiple classes for the same element, just separating them by a space…
[*]The generated page will not validate because you’ve got form elements (the checkboxes) outside the form itself, so results can get sketchy…
[/LIST]This should get you what you need. If not, ensure that your ID is actually a character field - if not, remove the quotes around $del_id in the delete statement.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=stylesheet type=text/css href="style.css"> <title>Untitled Document</title>
<style type="text/css">
.bigandbold { font-size: 20px; font-weight: bold; }
.error { color: red; font-weight: bold; }
</style>
</head>
<body>
<?php include('config.php');
// First delete the records necessary
$errDesc = "";
if(isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
if (!$result) { $errDesc .= "<p>Error deleting $del_id";
}
// if successful redirect to delete_multiple.php
// NOTE: If **this** page is delete_multiple.php, then this whole if statement is not needed..
if(strLen($errDesc) = 0){
header("Location: delete_multiple.php");
}
}
$result = mysql_query("SELECT * FROM test_mysql ORDER BY name ASC");
$count=mysql_num_rows($result); ?>
<div class="container">
<div class="header"> Header </div>
<div class="nav4 bigandbold">1</div>
<div class="nav4 bigandbold">#</div>
<div class="content bigandbold">Name</div>
<div class="nav3 bigandbold">LastName</div>
<div class="sidebar bigandbold">Email</div>
<?php
if(strLen($errDesc) > 0){
echo '<div class="error">'. $errDesc .'</div>';
}
$count=count($checkbox);
while($row = mysql_fetch_array($result)) {
echo '<div class="chekbox"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$rows[id].'"></div>';
echo '<div class="nav4">'. $row['id'] .'</div>';
echo '<div class="content"><a href="update.php?id='.$row['id'].'">'.$row['name'].'</a></div>';
echo '<div class="nav3">' . $row['lastname'] . '</div>';
echo '<div class="sidebar">' . $row['email'] . '</div>';
}
?>
<div class="delete">
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<input name="delete" type="submit" id="delete" value="">
</form>
</div>
<div class="footer">Footer</div>
</div>
</body>
</html>
Warning: Cannot modify header information - headers already sent by (output started at D:\\xampp\\htdocs\\learn\\meti\\idu\\display.php:12) in D:\\xampp\\htdocs\\learn\\meti\\idu\\display.php on line 27
grr… Trying to do too many things in too many languages today
What’s happening is that line (which is the only php was to do a response.write) expects to be the first thing sent, so all the base html stuff at the top of the page is killing it. Take all of the html from the first line down and including the body tag and move it to right above the div class=“container” line (after the php closing tag).
Crap - was doing too much at one time and I missed an obvious problem…
Change the code to this and you should be OK. StarLion was correct - the checkboxes need to be inside the form (which I thought I had fixed - obviously not…)
<?php include('config.php');
// First delete the records necessary
$errDesc = "";
if(isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
if (!$result) { $errDesc .= "<p>Error deleting $del_id";
}
// if successful redirect to delete_multiple.php
// NOTE: If **this** page is delete_multiple.php, then this whole if statement is not needed..
if(strLen($errDesc) == 0){
header("Location: delete_multiple.php");
}
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=stylesheet type=text/css href="style.css"> <title>Untitled Document</title>
<style type="text/css">
.bigandbold { font-size: 20px; font-weight: bold; }
.error { color: red; font-weight: bold; }
</style>
</head>
<body>
<div class="container">
<div class="header"> Header </div>
<div class="nav4 bigandbold">1</div>
<div class="nav4 bigandbold">#</div>
<div class="content bigandbold">Name</div>
<div class="nav3 bigandbold">LastName</div>
<div class="sidebar bigandbold">Email</div>
<div class="delete">
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<?php
// If there's an error, display it....
if(strLen($errDesc) > 0){
echo '<div class="error">'. $errDesc .'</div>';
}
// loop through the table and display the available values....
$result = mysql_query("SELECT * FROM test_mysql ORDER BY name ASC");
$count=mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
echo '<div class="chekbox"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$rows[id].'"></div>';
echo '<div class="nav4">'. $row['id'] .'</div>';
echo '<div class="content"><a href="update.php?id='.$row['id'].'">'.$row['name'].'</a></div>';
echo '<div class="nav3">' . $row['lastname'] . '</div>';
echo '<div class="sidebar">' . $row['email'] . '</div>';
} ?>
<input name="delete" type="submit" id="delete" value="">
</form>
</div>
<div class="footer">Footer</div>
</div>
</body>
</html>
hello
The code you gave me does not work.With code Following After clicking the delete button refresh page. But do not delete rows and rows.,Do you have another solution?
your code:
all code:
display.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=stylesheet type=text/css href="style.css"> <title>Untitled Document</title>
<style type="text/css">
.bigandbold { font-size: 20px; font-weight: bold; }
.error { color: red; font-weight: bold; }
</style>
</head>
<body>
<?php include('config.php');
// First delete the records necessary
$errDesc = "";
if(isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
if (!$result) { $errDesc .= "<p>Error deleting $del_id";
}
}
// if successful redirect to delete_multiple.php
// NOTE: If **this** page is delete_multiple.php, then this whole if statement is not needed..
if(strLen($errDesc) == 0){
echo "<script language=\\"javascript\\">window.location=\\"display.php\\";</script>"; }
}
?>
<div class="container">
<div class="header"> Header </div>
<div class="nav4 bigandbold">1</div>
<div class="nav4 bigandbold">#</div>
<div class="content bigandbold">Name</div>
<div class="nav3 bigandbold">LastName</div>
<div class="sidebar bigandbold">Email</div>
<div class="delete">
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<?php
// If there's an error, display it....
if(strLen($errDesc) > 0){
echo '<div class="error">'. $errDesc .'</div>';
}
// loop through the table and display the available values....
$result = mysql_query("SELECT * FROM test_mysql ORDER BY name ASC");
$count=mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
echo '<div class="chekbox"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$rows[id].'"></div>';
echo '<div class="nav4">'. $row['id'] .'</div>';
echo '<div class="content"><a href="update.php?id='.$row['id'].'">'.$row['name'].'</a></div>';
echo '<div class="nav3">' . $row['lastname'] . '</div>';
echo '<div class="sidebar">' . $row['email'] . '</div>';
} ?>
<input name="delete" type="submit" id="delete" value="">
</form>
</div>
<div class="footer">Footer</div>
</div>
</body>
</html>
config.php
<?php
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'meti1'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
?>
database code:
-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 02, 2010 at 02:20 PM
-- Server version: 5.1.37
-- PHP Version: 5.3.0
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `meti1`
--
-- --------------------------------------------------------
--
-- Table structure for table `test_mysql`
--
CREATE TABLE IF NOT EXISTS `test_mysql` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(65) NOT NULL DEFAULT '',
`lastname` varchar(65) NOT NULL DEFAULT '',
`email` varchar(65) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `test_mysql`
--
INSERT INTO `test_mysql` (`id`, `name`, `lastname`, `email`) VALUES
(1, 'hasan', 'mohamadi', 'meti@zig.com'),
(2, 'hosseifn', 'hgjjhghj', 'bhbh');
Not to mention that the definition in line 48 means nothing for the loop in line 17. You want the number of $checkbox elements there. count($checkbox) , or a foreach.
<?php include('config.php');
$result = mysql_query("SELECT * FROM test_mysql ORDER BY name ASC");
$count=mysql_num_rows($result);
// First delete the records necessary
$errDesc = "";
if(isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
if (!$result) { $errDesc .= "<p>Error deleting $del_id";
}
}
// if successful redirect to delete_multiple.php
// NOTE: If **this** page is delete_multiple.php, then this whole if statement is not needed..
if(strLen($errDesc) == 0){
echo "<script language=\\"javascript\\">window.location=\\"display.php\\";</script>"; }
}
?>