|
|
|
PHP Multiple Checkbox จากบทเรียนเรื่อง PHP Multiple Checkbox ผมได้บันทึกข้อมูลลงไปใน mysql แล้ว |
|
|
|
|
|
|
|
ผมไม่ทราบว่าโครงสร้างตารางเป็นยังไง ลองดูผมเขียนซึ่ง multiple checkbox จะเก็บไว้ในโครงสร้าง varchar แบบง่ายๆ นี้ละกันครับ
<?php
/*
CREATE TABLE `tb` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`color` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
*/
mysql_connect('localhost','root','12345');
mysql_select_db('test');
if (isset($_GET['addAction'])){
$color = array_map('mysql_real_escape_string',$_POST['color']);
$color = str_replace(',','',$color);
$s = 'INSERT INTO `tb`(color) VALUES(\''.implode(',',$color).'\')';
mysql_query($s);
}else if (isset($_GET['editAction'])){
$color = array_map('mysql_real_escape_string',$_POST['color']);
$color = str_replace(',','',$color);
$s = 'UPDATE `tb` SET color=\''.implode(',',$color).'\' WHERE id='.(int)$_GET['id'];
mysql_query($s);
}else if (isset($_GET['editForm'])){
$s = 'SELECT * FROM `tb` WHERE id='.(int)$_GET['id'];
$r = mysql_query($s);
if (mysql_num_rows($r)){
$row = mysql_fetch_assoc($r);
$color = htmlspecialchars($row['color']);
$color = explode(',',$color);
}else{
$color = array();
}
?>
EDIT ID:<?php echo$_GET['id'];?>
<form method="post" action="?editAction&id=<?php echo (int)$_GET['id'] ?>">
<input type="checkbox" name="color[]" value="red" <?php echo in_array('red',$color)?'checked="checked"':''; ?> />
<input type="checkbox" name="color[]" value="green" <?php echo in_array('green',$color)?'checked="checked"':''; ?> />
<input type="checkbox" name="color[]" value="blue" <?php echo in_array('blue',$color)?'checked="checked"':''; ?> />
<input type="submit" value="submit" />
</form>
<?php }else if (isset($_GET['addForm'])){ //addForm ?>
ADD:
<form method="post" action="?addAction">
<input type="checkbox" name="color[]" value="red" />
<input type="checkbox" name="color[]" value="green" />
<input type="checkbox" name="color[]" value="blue" />
<input type="submit" value="submit" />
</form>
<?php } ?>
<?php //display list
if (mysql_error()) echo htmlspecialchars(mysql_error());
$r = mysql_query('SELECT * FROM `tb`');
while($row = mysql_fetch_assoc($r)):
$row = array_map('htmlspecialchars',$row);
?>
<a href="?editForm&id=<?php echo $row['id'];?>">
record <?php echo $row['id'];?>
</a><br />
<?php endwhile; ?>
<a href="?addForm">[Add New Record]</a>
reference
|
|
|
|
|
Date :
2009-05-11 17:39:21 |
By :
num |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณ numมาตอบแทนแล้ว เย้ เย้ แก้แล้วค่ะ error หาย แต่ ตารางแสดงสินค้าไม่แสดงค่ะ
|
|
|
|
|
Date :
2009-05-11 18:38:18 |
By :
awakia |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบผิด ขอโทษนะคะ
|
|
|
|
|
Date :
2009-05-11 18:39:10 |
By :
awakia |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$query_rsS = "SELECT * FROM i_special";
$rsS = mysql_query($query_rsS, $inventory) or die(mysql_error());
$row_rsS = mysql_fetch_assoc($rsS);
$totalRows_rsS = mysql_num_rows($rsS);
?>
<body>
<form method="get">
<? do {?>
<input type="checkbox" name="specS[]" value="<?=$row_rsS['code'];?>"><? echo $row_rsS['mdescT'];?>
<? }while ($row_rsS = mysql_fetch_assoc($rsS));?>
<input name="Save" type="submit">
</form>
</body>
<?
$specS= $specS[0]." ".$specS[1]." ".$specS[2]." ".$specS[3]." ".$specS[4]." ".$specS[5]." ".$specS[6]." ".$specS[7];
echo $specS;
?>
</html>
<?php
mysql_free_result($rsS);
?>
|
|
|
|
|
Date :
2009-06-10 13:57:26 |
By :
Backer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|