|
|
|
รับค่าสังกัดจาก checkbox ต้องการ insert แบบ วนลูป แต่มัน insert แค่สังกัดละ 1 record ตอน select มันมาครบทุก record ครับ จากโค๊ดนี้ต้องแก้ตรงไหนครับ |
|
|
|
|
|
|
|
Code (PHP)
$chk = $_POST['chk'];
//echo"$choice</br>";
$sql8="select max(id_refer) as maxid_refer from message";
$cn8=mysql_db_query($dbname, $sql8);
while($rs=mysql_fetch_array($cn8)){
$maxid_refer = $rs[maxid_refer];}
$id_refer = $maxid_refer+1;
//echo"id_refer=$id_refer<br>";
for($i=0;$i<count($_POST['chk']);$i++)
{
if(trim($_POST['chk'][$i]) != "")
{
$sql2="select * from staff where id_staff = '$id_staff'";
$cn2=mysql_db_query($dbname, $sql2);
$num2=mysql_num_rows($cn2);
while($rs2=mysql_fetch_array($cn2)){
$id_staff = $rs2[id_staff];
$position = $rs2[position];}
//echo"รหัสพนักงาน$id_staff</br>";
//echo"ตำแหน่ง$position</br>";
$sql3="select max(id_message) as maxid_message from message";
$cn3=mysql_db_query($dbname, $sql3);
while($rs=mysql_fetch_array($cn3)){
$maxid_message = $rs[maxid_message];}
$id_message = $maxid_message+1;
$sql4="select * from member where belong = '".$_POST['chk'][$i]."'";
//echo"sql4=$sql4</br>";
$cn4=mysql_db_query($dbname, $sql4);
$num4=mysql_num_rows($cn4);
while($rs4=mysql_fetch_array($cn4)){
$id_member = $rs4[id_member];
$belong = $rs4[belong];
//echo"idสมาชิก:$id_member</br>";
//echo"สังกัด:$belong</br>";}
$id_refer;
$sql5 = "insert into message values('$id_message','$id_refer','$id_staff','$id_member','$head_message','$description_message','$last_update','1')";
//echo"$sql5</br>";
mysql_db_query($dbname,$sql5) or die("error=$sql5");
}
}
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2010-08-11 14:13:09 |
By :
benze240 |
View :
1571 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ ผมจะเข้าไปอ่านดูครับ
|
|
|
|
|
Date :
2010-08-13 03:34:54 |
By :
benze240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (form.php)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<script language="JavaScript">
function onSave()
{
if(confirm('Do you want to save ?')==true)
{
return true;
}
else
{
return false;
}
}
</script>
<form name="frmMain" action="save.php" method="post" OnSubmit="return onSave();">
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Select </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
<td align="center"><input type="checkbox" name="chkEmail[]" value="<?=$objResult["Email"];?>"></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnSave" value="Save">
</form>
</body>
</html>
Code (save.php)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=0;$i<count($_POST["chkEmail"]);$i++)
{
if($_POST["chkEmail"][$i] != "")
{
$strSQL = "INSERT INTO table_name (Mail) VALUES ('".$_POST["chkEmail"][$i]."')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
}
}
echo "Record Insert.";
mysql_close($objConnect);
?>
</body>
</html>
Go to : PHP Multiple Checkbox
|
|
|
|
|
Date :
2011-08-11 22:10:09 |
By :
thaicreate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|