我有一个html表单,带有多个复选框( subjects ),当用户(学生)选择主题时,StudentID存储在一个MySQL表中,以及在单独的列中所作的选择,但在同一个表中。
我的问题是:如果复选框值“等于”某项,那么如何将学生ID存储在新表中,strpos会这样做吗?
例如:
if (strpos($cc,'252000') !== false) {
mysqli_query($dbcon,"INSERT INTO newtable (studentid,ckb)
VALUES ('$studentid','$cc')");
}完整法典:
<?php
$host = 'localhost';
$port = 8889;
$username="root" ;
$password="root" ;
$db_name="db1" ;
$tbl_name="courses" ;
$tbl_name="studentinfo";
$tbl_name="newtable";
$dbcon = mysqli_connect("$host","$username","$password","$db_name") ;
mysqli_set_charset($dbcon, "utf8");
if (!$dbcon) {
die('error connecting to database'); }
$studentid = mysqli_real_escape_string($dbcon, $_GET['studentid']); //echo $studentid;
$name = $_GET['ckb'];
if(isset($_GET['ckb']))
{
foreach ($name as $courses){
$cc=$cc. $courses.',';
}
}
if (strpos($cc,'252000') !== false) {
mysqli_query($dbcon,"INSERT INTO newtable (studentid,ckb)
VALUES ('$studentid','$cc')");
echo "$cc, trtue";
}HTML
<form action="cdb.php" method="get">
<input name="studentid" type="text" id="studentid" maxlength="11"value="Student ID" />
<input type="checkbox" name="ckb[]" value="251000-1"/>
<input type="checkbox" name="ckb[]" value="251000-2"/>发布于 2015-08-29 15:11:25
事实证明,使用此代码实际上可以根据新表和不同表中的复选框对数据进行排序。
if (strpos($cc,'251000') !== false) {
$sql3="INSERT INTO newtable (studentid, ckb)
VALUES ('$studentid', '$cc')";
echo 'true';
}但是,我似乎必须检查sql3语句
if (!mysqli_query($dbcon,$sql3))
{
die('Error: ' . mysqli_error($dbcon));
}我的另一个错误是在sql语句中使用保留词,如表。这个修正和上面添加的代码解决了这个问题。
发布于 2015-08-28 14:00:01
好的,如果你绝对必须忽略所有好的数据库设计实践,试试这个。
与其创建逗号分隔列表并将其放入新表,不如使用serialize()函数将$_GET['ckb']的内容放置到新行中。至少通过这种方式,您可以使用unserialize()取回一个数组,这使得操作数据更容易,即使是,而不是,也使搜索数据库变得更加容易。
可以用json_encode()和json_decode()替换序列化/取消序列化
参考资料:
序列化:http://php.net/manual/en/function.serialize.php 非序列化:http://php.net/manual/en/function.unserialize.php
<?php
$host = 'localhost';
// I assume you moved apache to port 8889.
// so its irrelevant to mysql connection,
// good job you are not actually using this variable anywhere
$port = 8889;
$username="root" ;
$password="root" ;
$db_name="db1" ;
// fix so you have 3 variables and are not overwriting the same one
$tbl_name1="courses" ;
$tbl_name2="studentinfo";
$tbl_name3="newtable";
// remove unnecessary double quotes
$dbcon = mysqli_connect($host,$username,$password,$db_name) ;
// add some error checking that reports the actual error
if ( ! $dbcon ) {
echo 'Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error();
exit;
}
mysqli_set_charset($dbcon, "utf8");
if(isset($_GET['ckb'])) {
$studentid = mysqli_real_escape_string($dbcon, $_GET['studentid']);
$cc = serialize($_GET['ckb']);
$result = mysqli_query($dbcon,"INSERT INTO newtable
(studentid,ckb)
VALUES ('$studentid','$cc')");
if ( ! $result ) {
echo mysqli_error($dbcon);
exit;
}
}
?>发布于 2015-08-28 13:34:43
下面,计算“ckb”复选框的总大小。然后。由于for循环,它将一直运行到总大小。“学生”来自文本框。它将插入到表中,直到循环条件为真。
extract($_POST);
$CKBsize=sizeof($ckb);
for($i=0;$i<$CKBsize;$i++)
{
$CourseName=$ckb[$i];
mysql_query("INSERT INTO newtable SET studentid='$studentid', ckb='$CourseName'");
}https://stackoverflow.com/questions/32271997
复制相似问题