 |
ถามเรื่อง checkbox แล้ว update แต่ละ record ยังไงครับ ขอบคุณครับ |
|
 |
|
|
 |
 |
|
สอบถามเรื่อง checkbox ครับ
****** ปัญหาตอนนี้คือ เมื่อผม เลือก checkbox อีเมล record ใด record หนึ่ง มัน update ให้ทั้ง record เลยครับ******
***** ความต้องการ*****
เมื่อผมเลือก checkbox อีเมล record แรก ให้มัน update เฉพาะที่ผมติ๊ด checkbox นั้น ครับ
ส่วน อีเมล ใน record อื่นๆ จะไม่ update ไปด้วย
โค้ด ตาราง
Code (PHP)
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "db_erp";
$dbcon = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $dbcon);
mysql_query("SET NAMES UTF8");
$sql = "SELECT * FROM employees"; // เลือกข้อมูลจากตารางเฉพาะฟิล id,firstname,lastname
$result = mysql_query($sql, $dbcon); //ส่งคำสั่งเลือกข้อมูลให้ทำงาน
?>
<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<form name="form1" action="index.php?r=Config/Admin" method="post">
<table class="table table-striped table-advance table-hover">
<h4><i class="fa fa-angle-right"></i> ตั้งค่าส่วน User</h4>
<hr>
<thead>
<tr>
<th><i class="fa fa-bullhorn"></i> ชื่ออีเมล์</th>
<th><i class="fa fa-user"></i> สถานะ User</th>
<th>เข้าระบบได้</th>
<th>เข้าระบบไม่ได้</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="#"><?php echo $row['employee_email']; ?></a></td>
<td class="hidden-phone"><?php echo $row['employee_status']; ?></td>
<td>
<input type="checkbox" name="chkDel[]" value ="active"/>
</td>
<td><span class="label label-info label-mini"></span>
<input type="checkbox" name="chkDel[]" value ="block" /> </td>
<td>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
mysql_close($dbcon);
?>
<input type="submit" name="Submit" value="SAVE" />
</form>
โค้ด update
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","") or die ("ไม่สามารถติดต่อฐานข้อมุลได้");
$objDB = mysql_select_db("db_erp");
$strSQL = "SELECT * FROM employees";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
for ($i=0;$i<count($_POST["chkDel"]); $i++)
{
if($_POST["chkDel"][$i] != "")
{
$strSQL = "UPDATE employees SET employee_status ='".$_POST["chkDel"][$i]."'";
// $strSQL .= "WHERE id = '".$_POST['chkDel'][$i]."' " ;
// $strSQL .= "WHERE id = '".$_GET["chkDel"][$i]. "' ";
$objQuery = mysql_query($strSQL) or die ("ไม่สามารถค้นหาได้ [".$strSQL."]");
}
}
mysql_close($objConnect);
?>
Tag : PHP, MySQL, JavaScript, Ajax, jQuery
|
|
 |
 |
 |
 |
Date :
2014-04-30 09:30:52 |
By :
yokvoice |
View :
922 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้จาก
Code (PHP)
<input type="checkbox" name="chkDel[]" value ="active"/>
</td>
<td><span class="label label-info label-mini"></span>
<input type="checkbox" name="chkDel[]" value ="block" /> </td>
เป็น
Code (PHP)
<input type="checkbox" name="chkDel[<?php echo urlencode($row['employee_email']); ?>]" value ="active"/>
</td>
<td><span class="label label-info label-mini"></span>
<input type="checkbox" name="chkDel[<?php echo urlencode($row['employee_email']); ?>]" value ="block" /> </td>
ส่วนอัพเดตเดี๋ยวเขียนดูก่อน
|
 |
 |
 |
 |
Date :
2014-04-30 09:41:43 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","") or die ("ไม่สามารถติดต่อฐานข้อมุลได้");
$objDB = mysql_select_db("db_erp");
//get checked user that want to active
$emails = $active = $verify = array();
foreach ($_POST['chkDel'] as $email => $status) {
$email = urlencode($email);
if($status==="active") $active[$email] = true;
else $active[$email] = false;
$email = '\''.mysql_real_escape_string($email).'\'';
$emails[] = $email;
}
$strSQL = sprintf(
"SELECT `employee_email` AS 'email' FROM `employees` WHERE `employee_email` IN (%s)",
implode(',', $emails)
);
unset($emails); //for performance
//var_dump($strSQL); //uncomment if you want to see SQl command
$objQuery = mysql_query($strSQL) or die ("Error Query [{$strSQL}]");
if(!empty($objQuery)){
while ($objResult = mysql_fetch_assoc($objQuery)) {
if(!empty($active[$objResult['email']])){ //verify that this mail active and exist
$email = '\''.mysql_real_escape_string($email).'\'';
$verify[] = $email;
unset($emails[$email]);
}
}
}
unset($active); //for performance
mysql_free_result($objQuery); //for performance
mysql_query(
sprintf(
"UPDATE `employees` SET `employee_status` = 'active' WHERE `employee_email` IN (%s)",
implode(',', $verify)
)
) OR die("Can't update active user");
mysql_query(
sprintf(
"UPDATE `employees` SET `employee_status` = 'block' WHERE `employee_email` IN (%s)",
implode(',', $emails)
)
) OR die("Can't update blocked user");
mysql_close($objConnect);
?>
|
 |
 |
 |
 |
Date :
2014-04-30 10:10:53 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Invalid argument supplied for foreach()
error ตรง foreach ครับ
|
 |
 |
 |
 |
Date :
2014-04-30 10:25:46 |
By :
yokvoice |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้แล้วครับ ขอบคุณ สำหรับ โค้คแนวทางครับผม
|
 |
 |
 |
 |
Date :
2014-04-30 14:07:01 |
By :
yokvoice |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|