我无法从表单中获取选项值以将其传递给我的操作代码。
表格:
<select required name ="class" class="form-control">
<option value="NULL">-select class-</option>
<?php
$link2 = query("select * from td_class");
while ($list = mysql_fetch_array($link2)) {
?>
<option value="<?php echo $list['class'] ?>"><?php echo $list['class']; ?></option>
<?php } ?>
<option value="Alumni">Alumni</option>
</select>
我想用它来检查我的类表中的类容量,如果类是满容量则会引发错误,否则将成功添加学生数据。但是,我的动作代码似乎无法获取给定的类输入,当我提交按钮添加学生数据时,它总是会抛出一个错误,表示该类已满,即使它尚未完整。以下是我的行动代码,到目前为止我所做的:
if ($hal == 'managestudent') {
$name = $_POST['name'];
$image = $_FILES['image'];
$class = $_POST['class'];
$id = $_GET['id'];
if ($aksi == 'addstudent') {
$student = query("select name, count(name) from td_student where class='$class' ");
$student2 = mysql_fetch_array($student);
$capacity = query("select capacity from td_class where class='$class'");
$cap = mysql_fetch_array($capacity);
if (($student2) >= ($cap )) {
echo"<script> alert ('the class is full'); "
. "history.back();" . "</script>";
} else {
if (empty($image['tmp_name'])) {
echo "Without image<br>";
$sql = query("insert into td_student (name,class) values ('$name','$class)");
} else {
echo "With image<br>";
$namafile = uploadBerkas($image);
$sql = query("insert into td_student (name,class,image) values ('$name','$class,'$namafile')");
}
if ($sql == TRUE) {
echo "SQL True<br>";
echo"<script> alert('student data is added'); "
. "window.location='datasiswa.php';"
. "</script>";
} else {
echo "Sql Error " . mysql_error() . " ";
echo"<script> alert ('Error'); "
. "history.back();" . "</script>";
}
}
}
}
谢谢你的帮助。
答案 0 :(得分:0)
您的问题是mysql_fetch_array()
返回数组... ,而不是字符串/值。
所以当你这样做时
if (($student2) >= ($cap ))
在比较2个数组时,总是返回false(类如果已满),即
if((array(name, count(name)) >= (array(capacity)))
而不是2个值,即
if((count(name)) >= (capacity))
您需要做的是使用列名来获取要评估的实际值,即。 $student2["count(name)"]
/ $cap["capacity"]
$student = query("select name, count(name) from td_student where class='$class' ");
$student2 = mysql_fetch_array($student);
$capacity = query("select capacity from td_class where class='$class'");
$cap = mysql_fetch_array($capacity);
if (($student2["count(name)"]) >= ($cap["capacity"])) {