Delete multiple mysql rows using check boxes in php - php

I've tried asking this question before and I was rudely told I've posted too much code. So this time around I will do my best to only post relevant code.
So, I am trying to delete multiple rows using check boxes. I've looked up sooo many tutorials on it, and most use a for each loop. I haven't really figured out the difference between a while and for each loop. So, this is my front end php code:
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='0' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th><th>Delete</th></tr>";
?>
<form name="update_times" method="post" action="depart_t.php">
<?php
$i=0;
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value='" . $row['id'] . "'>";
echo "<tr>";
echo "<td align='center'><input name='resident[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['r_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='room_n[$i]' style='width: 40px' type='text' value='" . htmlspecialchars($row['room'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='appt_time[$i]' style='width: 55px' type='text' value='" . date("g:i A", strtotime($row['appttime'])) . "' /></td>";
echo "<td align='center'><input name='appt_phone[$i]' style='width: 65px' type='text' value='" . htmlspecialchars($row['apptphone'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='name_l[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['l_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='address[$i]' style='width: 90px' type='text' value='" . htmlspecialchars($row['address'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='city[$i]' style='width: 70px' type='text' value='" . htmlspecialchars($row['city'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='zip[$i]' style='width: 50px' type='text' value='" . $row['zip'] . "' /></td>";
echo "<td align='center'><input name='buscar[$i]' style='width: 30px' type='text' value='" . htmlspecialchars($row['buscar'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='e_name[$i]' style='width: 60px' type='text' value='" . htmlspecialchars($row['escort_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='transfer[$i]' style='width: 40px' type='text' value='" . htmlspecialchars($row['transfer'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='comments[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['comments'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "<td align='center'><input name='delete[$i]' type='checkbox' value='" . $row['id'] . "' /></td>";
echo "</tr>";
++$i;
}
echo "</table>";
?>
<br>
<input type="submit" value="Update" style="float:left; margin:5px" onclick="return confirm('Are you sure you want to proceed?')" /></form>
<form name="print" action="printer_f.php" method="post"><input name="p_friendly" type="submit" value="Printer Friendly View" style="float:left; margin:5px" /></form><form name="delete" action="delete.php" method="post"><input name="delete" type="submit" value="Delete Selected" onclick="return confirm('Are you sure you want to delete these entrys?')" style="float:left; margin:5px" /></form>
This is my delete code:
$size = count($_POST['delete']);
$i=0;
while ($i < $size)
{
$id = $_POST['id'][$i];
$sql = "DELETE FROM appointments WHERE id = $id";
echo $sql;
mysqli_query($transport, $sql) or die('Error: ' .mysqli_error($transport));
++$i;
}
With this code, all that ever happens is, the first row gets deleted no matter which one I select. I've tried using an if statement before it to check if a checkbox is actually checked, but that didn't seem to work either. So I am at a loss. Thank you for the help!

Use this:
$stmt = mysqli_prepare($transport, "DELETE FROM appointments WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
foreach ($_POST['delete'] as $id) {
mysqli_stmt_execute($stmt);
}
The foreach statement is better because the only delete[i] elements that are posted are the ones that are checked, so there will be gaps in the sequence. This code also shows how to use prepared statements instead of variable substitution.
The other problem you have is that the delete checkboxes conflict with this submit button:
<input name="delete" type="submit" value="Delete Selected" onclick="return confirm('Are you sure you want to delete these entrys?')" style="float:left; margin:5px" />
When you use that submit button, it sets $_POST['delete'] to Delete Selected, which overrides all the checkboxes. Give one of them a different name, and change the code that reads from that $_POST field.

Related

mysql update form using php issue keeps updating with old information not new updats

I am totally new to PHP and am working on an address book that contains companies and contacts for said companies. When click submit to update the data it just replaces it with the old data that was originally in there here is the companies html form:
<html>
<head><title>Update Records in MYSQL Database</title>
<style>
body {background-color: powderblue;}
form {border: 5px solid midnightblue;
padding: 40px;}
label {color: darkblue;}
form {border: 5px solid midnightblue;
padding: 40px;
</style>
</head>
<body>
<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');
//Select Query
$sql = "SELECT * FROM Companies";
//Execute the Query
$records = mysqli_query($link, $sql);
?>
<form>
<table>
<tr>
<th>CompanyName</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>ZipCode</th>
<th>PhoneNumber</th>
</tr>
<?php
while($row = mysqli_fetch_array($records)) {
echo "<tr><form action=update.php method='post'></form>";
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
echo "</form></tr>";
}
?>
</table>
</form>
</body>
</html>
and here is the actual updating php sql query that does not work or something any input would help get rid of this headache I have.
<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');
//Update Query
$sql = "UPDATE Companies SET CompanyName= $_POST[companyname1], Address= $_POST[address], City= $_POST[city], State= $_POST[state], ZipCode= $_POST[zipcode], PhoneNumber= $_POST[phone]
WHERE companies_id= $_POST[companies_id]";
//Execute the Query
if(mysqli_query($link, $sql))
header("refresh:1; url=edit.php");
enter code here
else
echo "Not Updated";
?>
First put here condition with WHERE clause $sql = "SELECT * FROM Companies"; that which record from database do you want to update. It seems like you are fetching all the record from database inside form Second you have closed your form tag at the begening of the while loop and keep rest of the input fields without form
Change this
while($row = mysqli_fetch_array($records)) {
echo "<tr><form action=update.php method='post'></form>";
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
echo "</form></tr>";
}
to
?>
<form action=update.php method='post'>
<?php
while($row = mysqli_fetch_array($records)) {
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
}
?>
</form>

PHP form update mysql database returns all database records after editing one record

so I have this code that other Stack members have helped me fine tune and correct some errors, the code all works as it should but there is one small detail, after successfully editing one record and clicking the update button ALL of the existing records that are in the database load into the page. Here is my code below:
<?php
$con = mysql_connect("localhost", "root", "M1q2w3e4r");
if (!$con) {
die("Can not connect: " . mysql_error());
}
mysql_select_db("inventory",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
};
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
$sql = "SELECT * FROM invoice".$where;
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Inv #</th>
<th>From</th>
<th>To</th>
<th>Type</th>
<th>Notes</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action='edit.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='inv_number' value='" . $record['inv_number'] . "'> </td>";
echo "<td>" . "<input type='text' id='from' name='from_date' value='" . $record['from_date'] . "'> </td>";
echo "<td>" . "<input type='text' id='to' name='to_date' value='" . $record['to_date'] . "'> </td>";
echo "<td>" . "<input type='text' name='date_type' value='" . $record['date_type'] . "'> </td>";
echo "<td>" . "<input type='text' name='notes' value='" . $record['notes'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='id' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
I know it has to do with the form action="edit.php", as it refreshes the page the id number in the url is pulled out. So I tried this:
echo "<form action='edit.php?edit=<?php echo $_REQUEST["id"]; ?>' method='post'>";
but this only led to my edit.php to display as a blank page. If anyone can help me figure out how to prevent all the database records from being displayed in the page after clicking the update button it would really help.
I might do this, for example, if I just wanted to show the record that was just updated:
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
$where = ' where id='.$_POST[id];
}
else {
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
}
You could also use REQUEST instead of GET and make a hidden input field with the name "edit" in your form.

Update blob image in Database with UPDATE MySQL using PHP

I am trying to update a record in my database which is a blob type, I can insert using this method really easily but when using UPDATE it doesn't seem to work, only for text. Would someone be able to point me in the right direction?
Here is my code:
<?php
$conn=mysqli_connect("localhost","root","root","plantidentify");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$ufamid = mysqli_real_escape_string($conn, $_POST['ufamilyid']);
$ucommonname = mysqli_real_escape_string($conn, $_POST['ucomname']);
$usynname = mysqli_real_escape_string($conn, $_POST['usynonyms']);
$uspecies = mysqli_real_escape_string($conn, $_POST['uspecies']);
$uprop = mysqli_real_escape_string($conn, $_POST['upropagation']);
$uimageData = mysqli_real_escape_string($conn, (file_get_contents($_FILES['uimage']['tmp_name'])));
$uheight = mysqli_real_escape_string($conn, $_POST['uheight']);
$utreeclass = mysqli_real_escape_string($conn, $_POST['utreeclass']);
$ulifecycle = mysqli_real_escape_string($conn, $_POST['ulifecycle']);
$UpdateQuery = "UPDATE plant
SET FamilyID='$ufamid', CommonName='$ucommonname',
SynonymNames='$usynname', Species='$uspecies',
Propagation='$uprop', Image='$uimageData',
Height='$uheight', TreeClass='$utreeclass',
Lifecycle='$ulifecycle'
WHERE PlantID='$_POST[uplantid]'";
if (!mysqli_query($conn,$UpdateQuery)) {
die('Error: ' . mysqli_error($conn));
}
header("Location: plantedit.php");
mysqli_close($conn);
?>
$uimageData is the variable in which contains the uploaded file from my form in another file. The column in my database where I am trying to update the blob is called "Image".
This might help, here is the form code that is in another .php which the update plant.php gets fired from:
<?php //display databse values
while($plant=mysql_fetch_array($retval)) {
echo"<form action=updateplant.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
echo "<td>" . "<input type=file name=uimage></td>";
echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
echo "</tr>";
echo "</form>";
}//end while
mysql_close($conn);
?>
to upload a file you should mention enctype='multipart/form-data' in form . Hope this will fix your problem.
<?php
//display databse values
while($plant=mysql_fetch_array($retval)) {
echo"<form action='updateplant.php' method='post' enctype='multipart/form-data' >";
echo "<tr>";
echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
echo "<td>" . "<input type=file name=uimage></td>";
echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
echo "</tr>";
echo "</form>";
}//end while
mysql_close($conn);
?>
Looks like you have a typo in the WHERE statement, $_POST[uplantid] should be ". $_POST['uplantid'] ."

How to make sure all content appear on the cell and not just the first word?

I am having a problem displaying content on a table from mysql table in the database to a form in PHP. My problem is that only the first word show on the address field for example.
Please look at my page on: http://www3.londonmet.ac.uk:8008/~iia0014/employeeManager.php
And also the cells are not aligned with the title.
Can anyone help me to solve this?
On my css I have:
table {
table-layout:fixed;
width:180%;
overflow:hidden;
border:1px ;
word-wrap:nowrap;
text-align:left;
}
But even if removing the CSS, just the first word appear.
PHP CODE:
<?php
// Connect to server and select databse.
$con = mysql_connect("$host","$username","$password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("$db_name",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE employees SET
Name='$_POST[name]',
DOB='$_POST[dob]',
Tel='$_POST[tel]',
Address='$_POST[address]',
Department='$_POST[department]',
PayRate='$_POST[payrate]',
Skills='$_POST[skills]',
Gender='$_POST[gender]'
WHERE EmpNo='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM employees WHERE EmpNo='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO employees (EmpNo, Name, DOB, Tel, Address, Department, PayRate, Skills, Gender) VALUES ('$_POST[uempNo]','$_POST
[uname]','$_POST[udob]', '$_POST[utel]','$_POST[uaddress]','$_POST[udepartment]', '$_POST[upayrate]','$_POST[uskills]','$_POST[ugender]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM employees";
$myData = mysql_query($sql,$con);
?>
<table border="1" width="10%">
<?php
echo "<tr>
<th>Number</th>
<th >Employee Name</th>
<th>DOB</th>
<th>Telephone</th>
<th>Address</th>
<th>Department</th>
<th>Pay Rate</th>
<th>Skills</th>
<th>Gender</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=employeeManager.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['EmpNo'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['Name'] . " </td>";
echo "<td>" . "<input type=text name=dob value=" . $record['DOB'] . " </td>";
echo "<td>" . "<input type=text name=tel value=" . $record['Tel'] . " </td>";
echo "<td>" . "<input type=text name=address value=" . $record['Address'] . " </td>";
echo "<td>" . "<input type=text name=department value=" . $record['Department'] . " </td>";
echo "<td>" . "<input type=text name=payrate value=" . $record['PayRate'] . " </td>";
echo "<td>" . "<input type=text name=skills value=" . $record['Skills'] . " </td>";
echo "<td>" . "<input type=text name=gender value=" . $record['Gender'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=employeeManager.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uempNo></td>";
echo "<td><input type=text name=uname></td>";
echo "<td><input type=text name=udob></td>";
echo "<td><input type=text name=utel></td>";
echo "<td><input type=text name=uaddress></td>";
echo "<td><input type=text name=udepartment></td>";
echo "<td><input type=text name=upayrate></td>";
echo "<td><input type=text name=uskills></td>";
echo "<td><input type=text name=ugender></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td></tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
In the below quotes you were missing quotes and the tags were not closed proprely!
while($record = mysql_fetch_array($myData)){
echo "<form action='employeeManager.php' method='post'>";
echo "<tr>";
echo "<td><input type='hidden' name=hidden value='" . $record['EmpNo'] . "'> </td>";
echo "<td><input type='text' name='name' value='" . $record['Name'] . "'> </td>";
echo "<td><input type='text' name='dob' value='" . $record['DOB'] . "'> </td>";
echo "<td><input type='text' name='tel' value='" . $record['Tel'] . "'> </td>";
echo "<td><input type='text' name='address' value='" . $record['Address'] . "'> </td>";
echo "<td><input type='text' name='department' value='" . $record['Department'] . " </td>";
echo "<td><input type='text' name='payrate' value='" . $record['PayRate'] . "'> </td>";
echo "<td><input type='text' name='skills' value='" . $record['Skills'] . "'> </td>";
echo "<td><input type='text' name='gender' value='" . $record['Gender'] . "'> </td>";
echo "<td><input type='submit' name='update' value='update'> </td>";
echo "<td><input type='submit' name='delete' value='delete'> </td>";
echo "</tr>";
echo "</form>";
}
For better understanding of your mistake, notice your code:
echo "<td><input type=text></td>";
It should be like that:
echo "<td><input type='text'></td>";
It works, its just that you don't close the tag, which results in:
<input type=text name=address value=one two thre
The browser reads it as value=one and "two" and "three" like separate arguments.
What you need is a quote like this:
<input type=text name=address value="one two three"/>

Reading 0 and 1 from a database and retrieving in a checkbox

I am trying to create a members table, using a PHPmyadmin database, and in the database, there is a 0 & 1 field, and I want to transform the 0 and 1 in a checkbox, for each member. Is an editable table, so the table might be in a form. Here is what I did:
<?php
include('../../config.php');
$result2 = mysql_query("SELECT * FROM acars_users");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Dados Pessoais</th>
<th>Origem</th>
<th>Dados de Redes</th>
<th width='220px'>Base</th>
<th>Patente</th>
<th>Horas</th>
<th>É Ativo?</th>
<th>Dinheiro</th>
<th>Senha</th>
<th>Opções</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td width='300px'>" . "<form action=editar.php method=POST><input name=username size=7 type=text value=".$row['username']." /></br><input name=nome size=10 type=text value=".$row['nome']." /><input name=sobrenome size=20 type=text value=".$row['sobrenome']." /></br><input name=email size=25 type=text value=".$row['email']." />" . "</td>";
echo "<td>" . "<input name=datanascimento size=10 type=text value=".$row['datanascimento']." /></br><input name=pais size=10 type=text value=".$row['pais']." /></br><input name=cidade size=30 type=text value=".$row['cidade']." />" . "</td>";
echo "<td>" . "IVAO: <input name=idivao size=8 type=text value=".$row['idivao']." /></br>VATSIM: <input name=idvatsim size=8 type=text value=".$row['idvatsim']." />" . "</td>";
echo "<td>" . "<input name=base size=8 type=text value=".$row['base']." />" . "</td>";
echo "<td>" . "<input name=rank size=10 type=text value=".$row['rank']." /></br>Admin <input name=admin size=1 type=checkbox value=".$row['admin']." /></br>DOV <input name=dov size=1 type=checkbox value=".$row['dov']." /></br>Checador <input name=checador size=1 type=checkbox value=".$row['checador']." />" . "</td>";
echo "<td>" . "<input name=horas size=6 type=text value=".$row['horas']." />" . "</td>";
echo "<td>" . "<input type=checkbox name='isactive[]' value=".$row['isactive']." />" . "</td>";
echo "<td>" . "<input name=dinheiro size=10 type=text value=".$row['dinheiro']." />" . "</td>";
echo "<td>" . "<input name=password size=10 type=password value=".$row['password']." />" . "</td>";
echo "<td>" . "<input name=edit_id value=".$row['id']." type=hidden><input type=submit value=Editar ></form><form action=deletarexist.php method=POST><input name=delete_id value=".$row['id']." type=hidden><input type=submit value=Demitir></form>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>
$chkd=$row['isactive']?'checked="checked"':''; // 0: false; 1: true
echo "<td><input type='checkbox' name='isactive[]' value='{$row['isactive']}' $chkd></td>";
(Although, you should look into using a templatingg system (such as smarty) rather than echoing html fragments. It seems at bit weird in the start, but as soon as you get used to it, you'll never look back-)
I guess you want the checkbox checked, and in that case you need to add the checked="checked" attribute to the checkbox.
So what you would do would be something like this:
if($row['isactive'] == 1){
echo "<td>" . "<input type='checkbox' name='isactive[]' value=".$row['isactive']. checked='checked' />" . "</td>";
} else {
echo "<td>" . "<input type='checkbox' name='isactive[]' value=".$row['isactive']." />" . "</td>";
}
For the value you probably should have something to identify the checkbox, like a memberid depending on what you are using it for.

Categories