I want to pull 2 fields from a MySQL database and them use them in 2 hidden form fields along with allowing the user to input data into additional form fields. I am using the following code to do so:
$yesterday = date('d/m/Y',strtotime("-1 days"));
$sql="SELECT acct_backup_servers.server_desc, acct_customer.customer from acct_backup_servers, acct_customer
where acct_backup_servers.cust_act=acct_customer.acct_no ";
$result=mysql_query($sql);
echo '<form action="http://www.bimotech.co.uk/admin/insert_backup_rec/index.php" method="post">
<table>
<tr>
<th width="20%" align="left" valign="top">Server</th>
<th width="20%" align="left" valign="top">Customer</th>
<th align="left" valign="top">Date</th>
<th width="25%" align="left" valign="top">Status</th>
<th width="30%" align="left" valign="top">Notes</th>
<th align="left" valign="top"></th>
</tr>';
while($rows=mysql_fetch_array($result)){
$server_notes = $rows['server_desc'];
echo "
<input type='hidden' name='server' value='".$rows['server_desc']."'>
<input type='hidden' name='customer' value='".$rows['customer']."'>
<tr>
<td valign='top'>".$rows['server_desc']."</td>
<td valign='top'>".$rows['customer']."</td>
<td valign='top'><input type='text' name='date' size='10' id='datepicker' value='".$yesterday."'></td>
<td valign='top'>
<select name='status'>";
$sql2 = mysql_query("SELECT cat_desc FROM acct_backup_category");
while ($row = mysql_fetch_array($sql2)){
echo "<option value='category'>" . $row['cat_desc'] . "</option>";
}
echo "</select></td>
<td valign='top'><textarea name='notes'></textarea></td>
<td valign='top'><input type='checkbox' name='completed'></td>
</tr>";
}
echo "</table>
</br>
<input type='submit' class='btn' value='Submit'>
</form>";
mysql_close();
I have setup the page that this form post to and echo'd the results but all I am receiving is the last record. Is there a way that I can get all the results for each row and post them in one go to the database?
EDIT: PHP Echo below.
$backup_date = $_POST['date'];
$server_desc = $_POST['server'];
$customer = $_POST['customer'];
echo $backup_date;
echo $server_desc;
echo $customer;
I am very much a PHP newbie so appreciate anyones help on this.
Further EDIT:
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
foreach($customer as $key => $value) {
$date = $backup_date[$key];
$server = $server_desc[$key];
$customer_id = $value;
$backup_code = $status[$key];
$notes_field = $notes[$key];
$been_checked = $checked[$key];
mysql_query("INSERT INTO acct_backups (backup_date, server_code, cust_act, backup_code, notes, checked) VALUES ($date, $server, $customer_id, $backup_code, $notes_field, $been_checked)");
}
mysql_close();
Thanks,
John
Make hidden input as an array.
Like,
<input type='hidden' name='server[]' value='".$rows['server_desc']."'>
<input type='hidden' name='customer[]' value='".$rows['customer']."'>
Then print,
print_r($backup_date);
print_r($server_desc);
print_r($customer);
Sample Code:
/* MySQL Conncection goes here*/
foreach($customer as $key => $value) {
$customer_id = $value;
$date = $backup_date[$key];
$server = $server_desc[$key];
mysql_query("INSERT INTO table_name VALUES($customer_id, $date, $server)");
}
Related
Look like everything is working fine with this code but in fact fails to update the database, Data are displayed correctly while fetching data but when i press update Button the data disappear but no update has been executed. It look fine to me but seems i am wrong.
This is a project for my professor so i don't care for the SQL injection and others.
<html>
<head>
<link rel="stylesheet" type="text/css" href="btnstyle.css">
<title>Managament System</title>
</head>
<body>
<h1>TU Chemnitz Student managament system</h1>
<br>
ADD Person
Edit Person
Manage Boards
Manage Departments
Search N&S
Triple Search
Membership
<br>
<br>
<?php
// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$querys = "SELECT * FROM tblperson";
// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());
echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
?>
<?php
echo '<tr>';
echo '<td>'. $row['personid'].'</td>';
echo '<td>'. $row['personname'].'</td>';
echo '<td>'. $row['personsurname'].'</td>';
echo '<td>'. $row['persondepartment'].'</td>';
echo '<td>'. $row['personboard'].'</td>';
echo '<td>'. $row['martinumber'].'</td>';
echo '<td>'. $row['personregdate'].'</td>';
echo '<td>'.' EDIT '.'</td>';
}
?>
</body>
</html>
and this is the edit file which seems to problematic.
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid'];
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
<form action="edit20.php" method="POST">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="newpersonname" value="<?php echo $row[1];?>" maxlength="30" size="13"></td>
</tr>
<tr>
<td>Last Name</td>
<td> <input type="text" name="personsurname" value="<?php echo $row[2];?>" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Department</td>
<td>
<select name='persondepartment'>
<option>Production</option>
<option>Sales</option>
</select>
</td>
</tr>
<tr>
<td>Board</td>
<td>
<select name='personboard'>
<option>Evaluation</option>
<option>Executive</option>
<option>Research</option>
</select>
</td>
</tr>
<tr>
<td>Marticulation Number</td>
<td> <input type="text" name="martinumber" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Date of Registration</td>
<td><input type="date" name="personregdate" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=" Update"></td>
</tr>
</table>
</form>
You are looking for personid when the Update button is pressed on the form in edit20.php but that value has never been set so it will be empty and the update will fail.
After
<form action="edit20.php" method="POST">
add:
<input type="hidden" name="personid" value="<?php echo $personid; ?>">
On edit page seem your confusing the same variable with different values. If you state $personid variable to contain the edit value from get, then just re-use the variable don't assign new value. On this line you assign new value :
$personid = $_POST['personid'];
Don't assign new value since it has the initial value already to use just set the variable global for usage
$personid = $_GET['edit'];
Or else create a hidden element and pass edit value into it.
Please add name attribute for your update button
<td colspan="2"><input type="submit" name="update" value=" Update"></td>
and chk whether the update button set or reset as in the place of
if(isset($_POST['newpersonname'])) // change text 'newpersonname' as 'update'
You use a variable that doesn't excist:
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid']; // this doesn't excist
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
$personid = $_POST['personid']; doesn't excist in your code. Its simply a piece of code you put in there to probably proces, but forgot to define the variable in the code. Place the following in your form.
<input type="hidden" name="personid" value="<?php echo $_GET['edit']; ?>">
You only use this just once because you send the form back after proces to your home, hence it wont be used anymore. You can also use the avariable you defined as $personid; on that position.
If that fails, something maybe wrong in your query. Try to echo out the query (remove qucikly the meta command) by simply just do echo $sql after you do the sql query. 9 out of 10 times, it's a typo.
I am trying to update multiple rows on submit of a form (in particular this one is the "hours" field.
I have it working but only one of the value updates vs all of them.
There is the possibility of having different values for each update.
The form code:
$query2 = "select * FROM work_hours WHERE formid = $formid ";
$result = $mysqli->query( $query2 );
$num_results = $result->num_rows;
if( $num_results > 0){
echo " <table border='0' align='center'>
<tr>
<td colspan='2' align='center'>
<strong> Time Away Break Down</strong>
</td>
</tr>
<tr>
<td align='center'>Date</td>
<td align='left'>Hours</td>
</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo " <tr>
<td class='hidden_sm' align='center'>
<input type='text' name='id' size='10' value='$id' class='dept' readonly style='width:30px;'>
<input type='text' name='date' size='40' value='$date' class='dept' readonly> <input type='text' name='end_date' size='40' value='$end_date' class='dept' readonly>
</td>
<td class='hidden_sm' align='left' >
<input type='text' name='hours' size='10' style='width:30px;' value='$hours' class='dept' >
</td>
</tr>
";
}
echo "<tr>
<td colspan='2' align='center'>
<input type='submit' name='Submit' value='Submit Request'>
</td>
</tr>
</form>
</table>";//end table
Submit Code:
$id = $_POST['id'];
$formid = $_POST['formid'];
$hours = $_POST['hours'];
include 'connect-db.php';
$stmt = $mysqli->prepare("UPDATE work_hours SET hours = ? WHERE formid = ?");
$stmt->bind_param('si',
$_POST['hours'],
$_POST['formid']);
$stmt->execute();
if ( $stmt ) {
echo "<p align='center'>Thank you, this request has been approved.<BR>You will be redirected in 5 seconds</p>";
} else {
echo "Error, you status cannot be updated. <BR> Please contact your system administrator.";
}
$stmt->close();
?>
Could anyone point me in the right direction to have all values update on submit, as I have had zero luck.
As well I do understand the need to prevent SQL Injections, and that I am working, so no need to remind me.
Thanks in advance!
Looks like you'll want to use a CASE statement as explained here:
How does MySQL CASE work?
Use a loop to build the statement and you're better off using the id as the identifier instead of formid, since the id is the unique value and you could have different results in the form.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
sample form in html
<form action="search.php" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="Search" />
</form>
When I am trying to search it displays entire row with all fields. That's good. but not capture that details when i click on Edit.
search.php file:
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
echo '<table border=1 width=999>
<tr>
<th valign=top>ID</th>
<th valign=top>Name</th>
<th valign=top>Telephone</th>
<th valign=top>E-mail</th>
<th valign=top>Country Visiting for</th>
<th valign=top>Visa Category</th>
<th valign=top>Other Category</th>
<th valign=top>Passport No</th>
<th valign=top>Remarks</th>
<th valign=top>Date(Created)</th>
<th valign=top>Updated Remarks</th>
<th valign=top>Updated Date</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['Telephone'].'</td>
<td>'.$row['E_mail'].'</td>
<td>'.$row['country'].'</td>
<td>'.$row['visa_categeory'].'</td>
<td>'.$row['other_category'].'</td>
<td>'.$row['passport_no'].'</td>
<td>'.$row['remarks'].'</td>
<td>'.$row['date'].'</td>
<td>'.$row['updated_remarks'].'</td>
<td>'.$row['updated_date'].'</td>
</tr>';
}
echo '
</table>';
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div>Edit</div>
and my update.php file looking like this.
<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="customer_details"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=intval($_GET['id']);
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table align="center" style="margin-left:100px" width="600" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="left"><strong>ID</strong></td><td align="left">
<input name="id" disabled type="text" id="id" value="<?php echo $rows['id']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Name</strong></td><td align="left">
<input name="name" disabled type="text" id="name" value="<?php echo $rows['name']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Telephone</strong></td><td align="left">
<input name="Telephone" disabled type="text" id="Telephone" value="<?php echo $rows['Telephone']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Country Visiting for</strong></td><td align="left">
<input name="country" disabled type="text" id="country" value="<?php echo $rows['country']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Visa Category</strong></td><td align="left">
<input name="visa_categeory" disabled type="text" id="visa_categeory" value="<?php echo $rows['visa_categeory']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Other Category</strong></td><td align="left">
<input name="other_category" disabled type="text" id="other_category" value="<?php echo $rows['other_category']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Remarks</strong></td><td>
<textarea name="remarks" id="remarks" value="<?php echo $rows['remarks']; ?>" size="20"><?php echo $rows['remarks']; ?></textarea>
</td></tr><tr>
<td align="left"><strong>Date</strong></td><td>
<input name="date" type="text" id="date" value="<?php echo $rows['date']; ?>" size="20">
</td></tr>
<tr>
<td align="left"><strong>Modified Remarks</strong></td><td>
<textarea name="updated_remarks" id="updated_remarks" value="<?php echo $rows['updated_remarks']; ?>" size="20"><?php echo $rows['updated_remarks']; ?></textarea> </td></tr>
<tr>
<td align="left"><strong>Modified Date</strong></td><td>
<input name="updated_date" type="text" id="updated_date" value="<?php echo $rows['updated_date']; ?>" size="20">
</td></tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="left">
<input type="submit" name="Submit" value="Update">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
and finally update_ac.php looking like this.
<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="customer_details"; // Table name
$id=$_POST['id'];
$name=$_POST['name'];
$remarks=$_POST['remarks'];
$date=$_POST['date'];
$updated_remarks=$_POST['updated_remarks'];
$updated_date=$_POST['updated_date'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET id='$id', name='$name', remarks='$remarks', date='$date', updated_remarks='$updated_remarks', updated_date='$updated_date' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
header('Location: list_records.php');
}
else {
echo "ERROR";
}
?>
Now I am Explaining this.
When I am trying to search with id it displays entire row(all fields).
But When I click on Edit in that file it doesn't capture that row details(that fields) I am asking How to capture that row details(all fields).
Thanks in Advance.
I figured out whats wrong with your code. Actually you are not passing any specific ID to your update.php page. You need to this part Edit inside your while loop and then pass id of each row inside the link. Update your update.php code like below, i think it will work:
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
//$output = "";
echo '<table border=1 width=999>
<tr>
<th valign=top>ID</th>
<th valign=top>Name</th>
<th valign=top>Telephone</th>
<th valign=top>E-mail</th>
<th valign=top>Country Visiting for</th>
<th valign=top>Visa Category</th>
<th valign=top>Other Category</th>
<th valign=top>Passport No</th>
<th valign=top>Remarks</th>
<th valign=top>Date(Created)</th>
<th valign=top>Updated Remarks</th>
<th valign=top>Updated Date</th>
<th valign=top>Action</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['Telephone'].'</td>
<td>'.$row['E_mail'].'</td>
<td>'.$row['country'].'</td>
<td>'.$row['visa_categeory'].'</td>
<td>'.$row['other_category'].'</td>
<td>'.$row['passport_no'].'</td>
<td>'.$row['remarks'].'</td>
<td>'.$row['date'].'</td>
<td>'.$row['updated_remarks'].'</td>
<td>'.$row['updated_date'].'</td>
<td>Edit</td>
</tr>';
}
echo '
</table>';
//echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div></div>
The proble you are facing is because the $id variable you have used in the search.php for HREF to update.php doesnt exist. place the DB value fetched in that variable first.
Also, its better to reuse the code using $_SERVER['PHP_SELF'] rather than all different files.
I have populated a drop down list using values from a database in my web page. I use MySQL database and PHP for coding and HTML for design. Now, if I select a value from the drop down list and submit it, I want the selected value to be stored in a new variable. I will transfer the variable again to a new web page.
I tried to do this using the $_GET and $_REQUEST codes, but none of them work. Please suggest me the solutions.
Here is the code:
PHP and HTML code for Drop Down List:
<?php
$yearval=$_GET['year'];
$classval=$_GET['class'];
$semesterval=$_GET['semester'];
mysql_connect("localhost","root",""); // (host, username, password)
mysql_select_db("digint") or die("Unable to select database"); //select db
?>
<table width="500" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>SELECT YOUR SUBJECT AND ENTER THE SUBJECT ACCESS CODE</td>
<table width="400" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form method="post" action="accesscheck.php">
<tr>
<td>Select Subject</td>
<td>
<?php
//Subject selection
$result = mysql_query("select DISTINCT subject from accessindex where semester=$semesterval");
echo '<select id="subject" name="subject"><OPTION>';
echo "Select a subject</OPTION>";
while ($row = mysql_fetch_array($result)){
$subject = $row["subject"];
echo "<OPTION value=\"$subject\">$subject</OPTION>";
}
echo '</SELECT>';
?>
</td>
</tr>
<tr>
<td width="78">AccessKey</td>
<td width="294"><input name="myaccesskey" type="password" id="myaccesskey"></td>
</tr>
<td>
<input type="submit" name="AccessCheck" value="Submit">
</td>
</tr>
</body>
</html>
PHP code for using the selected value:
<?php
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myaccesskey=$_POST['myaccesskey'];
$subjectval=$_POST['subject'];
$myaccesskey = stripslashes($myaccesskey);
$myaccesskey = mysql_real_escape_string($myaccesskey);
$subjectval = stripslashes($subjectval);
$subjectval = mysql_real_escape_string($subjectval);
$sql="SELECT accesskey FROM accessindex WHERE accesskey='$myaccesskey' and subject=$subjectval";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
header("location:marksentry.php");
}
else {
echo "Wrong Subject Access Key";
}
After submit you can get value of select drop down using method you used GET or POST
$select_val = $_POST['subject'];
Where $_POST['subject'] is selected value of dropdown and subject is a name of dropdown
Try this
<?php
//Subject selection
$result = mysql_query("select DISTINCT subject from accessindex where semester=$semesterval");
echo '<form method="POST" action="new_webpage_action.php">';
echo '<select id="subject" name="subject"><OPTION>';
echo "Select a subject</OPTION>";
while ($row = mysql_fetch_array($result)){
$subject = $row["subject"];
echo "<OPTION value=\"$subject\">$subject</OPTION>";
}
echo '</SELECT>';
echo '<input type="submit" name="submit" value="submit"/>';
echo '</form>';
?>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="toybox"; // Database name
$tbl_name="Emp"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// echo $count;
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['EmpId']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['EmpId']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['Email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo " Record have been deleted";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Rather than doing a query for each iteration of your delete-loop, I would build all of the indexes up into a string, and use something like the following:
DELETE FROM tableName
WHERE id IN (1,2,12,53)
Also, your submit button won't come through as $delete, but instead $_POST["delete"]. And with your connection:
mysql_connect("$host", "$username", "$password")
You really ought not use variables like strings (generally) - this should be written as:
mysql_connect($host, $username, $password)
Furthermore, you've got a few more problems in and around your delete-logic. For instance, I pointed out already that your <input type='submit' name='delete' /> button will be known as $_POST["delete"] once it is registered on the server. Likewise, your checkboxes, having an id value of checkbox[] will simply be known as $_POST["checkbox"] on the server.
Also, your $count variable, used in your delete-logic, is based on the earlier query that selected all of the records to show them. It does not reflect the number of checkboxes to be deleted, it reflects the number of records that were shown. As such, your for loop should not be based on it:
for ($i = 0; $i < count($_POST["checkbox"]); $i++)
{
// delete $_POST["checkbox"][$i];
}
And again, I would suggest you build a string of values and run a single query instead of multiple.
Working Code .. Consider Point 1 2 and 3
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("funconnect") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM members")
or die(mysql_error());
$count=mysql_num_rows($result);
echo "<form name='sendmail' method='post' action='memberList.php'><table border='1'>";
echo "<tr> <th>Select</th> <th>Name</th> </tr>";
// keeps getting the next row until there are no more to get
$countSn = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
$chkname = "checkbox".$countSn; #Point 1- Create defferent name for checkboxex like checkbox0, checkbox1
echo "<tr><td><input type='checkbox' name=".$chkname." value=".$row['m_id']." /></td>";
echo "</td><td>";
echo $row['m_name'];
echo "</td></tr>";
$countSn++;
}
echo '<tr><td colspan=2><input name="delete" type="submit" id="delete" value="Delete"></td></tr></table></form>';
$delete=$_POST['delete'];
$checkbox=$_POST['checkbox'];
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$cname = "checkbox".$i;# Point 2- Create check box name like checkbox0, checkbox1
$checkbox=$_POST[$cname]; #Point 3 - Retrieve data against name
echo $i."===".$checkbox."<br />";
//echo $del_id;
//$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
//$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
//echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>