基于复选框值,将0或1插入MySQL [英] Insert 0 or 1 into MySQL based on checkbox value
本文介绍了基于复选框值,将0或1插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用php复选框将0
或1
插入到mysql数据库。但是,它无法修改或插入mysql数据库。
I would like to insert the "0"
or "1"
into mysql database by using php checkbox. However, it is unable to modify or insert on mysql database.
我的编码 edit.php
:
<?php
switch($_GET['action']) {
case "Modify":
/*************************** case "modify" **********************/
$colname_RsStaff = "1";
if (isset($_GET['staffid'])) {
$colname_RsStaff = (get_magic_quotes_gpc())
? $_GET['staffid'] : addslashes($_GET['staffid']);
}
mysql_select_db($database_conn, $conn);
$query_RsStaff = "SELECT * FROM `tbl_staff`
WHERE `tbl_staff`.`staffid` = '".$_GET['staffid']."'";
$RsStaff = mysql_query($query_RsStaff, $conn) or die(mysql_error());
$row_RsStaff = mysql_fetch_assoc($RsStaff);
$totalRows_RsStaff = mysql_num_rows($RsStaff);
$subagent = $row_RsStaff['subagent'];
$subum = $row_RsStaff['subum'];
$subssm = $row_RsStaff['subssm'];
$subtutor = $row_RsStaff['subtutor'];
break;
/* ****************Add**************** */
default:
$subagent = "";
$subum = "";
$subssm = "";
$subtutor = "";
break;
}
?>
<form name="staff" method="post" onsubmit="return validateForm()" action="commit.php?action=<?php echo $_GET['action'] ?>">
<tr>
<td height="12" align="right" bgcolor="#CCCCCC"><h5> </h5></td>
<td height="12" align="left" bgcolor="#dfdfdf"> <input type="checkbox" name="subagent[]" value="<?php echo $subagent; ?>" /> Agent <input type="checkbox" name="subum[]" value="<?php echo $subum; ?>" /> UM </td>
<td rowspan="2" align="left" bgcolor="#dfdfdf">
<h6> * Selected one or more</h6></td>
</tr>
<tr>
<td height="12" align="right" bgcolor="#CCCCCC"><h5> </h5></td>
<td height="12" align="left" bgcolor="#dfdfdf"> <input type="checkbox" name="subssm[]" value="<?php echo $subssm; ?>" /> SSM <input type="checkbox" name="subtutor[]" value="<?php echo $subtutor; ?>" /> Tutor </td>
</tr>
<input type="submit" name="Submit" value="<?php echo $_GET['action'] ?>" />
张贴到 commit.php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
switch ($_GET['action']) {
case "Create":
$staffid = $_POST['staffid'];
mysql_select_db($database_conn, $conn);
$result ="SELECT * FROM tbl_staff WHERE staffid = '".$_POST['staffid']."'";
$Staff = mysql_query($result, $conn) or die(mysql_error());
$totalRows_Staff = mysql_num_rows($Staff);
$insertSQL = sprintf("INSERT INTO tbl_staff (subagent, subum, subssm, subtutor) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['subagent'], "text"),
GetSQLValueString($_POST['subum'], "text"),
GetSQLValueString($_POST['subssm'], "text"),
GetSQLValueString($_POST['subtutor'], "text"),
mysql_select_db($database_conn, $conn);
$Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
}
$insertGoTo = "tr_staff.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
break;
/************************* update *************************/
case "Modify":
$updateSQL = sprintf("UPDATE tbl_staff SET subagent=%s, subum=%s, subssm=%s, subtutor=%s WHERE staffid=%s",
GetSQLValueString($_POST['subagent'], "text"),
GetSQLValueString($_POST['subum'], "text"),
GetSQLValueString($_POST['subssm'], "text"),
GetSQLValueString($_POST['subtutor'], "text"),
GetSQLValueString($_POST['staffid'], "int"));
mysql_select_db($database_conn, $conn);
$Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());
$insertGoTo = "tr_staff.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
//}
break;
}
?>
任何建议都将非常感激。
Any advice would be super appreciated.
推荐答案
这里是一个小技巧:
当使用复选框时,只需在复选框之前添加一个隐藏字段:
Here is a little trick: When working with checkboxes, just add a hidden field before the checkbox:
<input type="hidden" name="checkbox" value="0" />
<input type="checkbox" name="checkbox" value="1" />
然后您可以使用
<?php
$checkboxValue = (isset($_POST['checkbox'])) ? intval($_POST['checkbox']) : 0; // returns 0 or 1
?>
并保存一些代码。
在您的代码中,我将替换
<input type="checkbox" name="subssm[]" value="<?php echo $subssm; ?>" />
<input type="hidden" name="subssm" value="0" />
<input type="checkbox" name="subssm" <?php if($subssm == "1") echo "checked='checked'"; ?> value="1" />
这篇关于基于复选框值,将0或1插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文