artur shiba artur shiba - 8 days ago 5
PHP Question

Can't get option value from form

I can't get the option value from my form to pass it to my action codes.

Form :

<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>


I wanted to use it to check the class capacity in my class table, if the class is at full capacity it will throw an error otherwise the student data will be successfully added. However, my action codes seems to unable to get the given class input and when I submit the button to add student data it will always throw an error saying that the class is full even though it's not full yet. Below is my action codes, what I have done so far :

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>";
}
}
}
}


Thankyou for the helps.

Answer

Your issue is that mysql_fetch_array() Returns an array ..., not a string/value.

So when you do

if (($student2) >= ($cap ))

It is always returning false (the class if full) as it comparing 2 arrays, ie.

if((array(name, count(name)) >= (array(capacity)))

and not 2 values, ie.

if((count(name)) >= (capacity))

What you need to do is use the column name to get the actual value to evaluate, ie. $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"])) {
Comments