Can't get option value from form

artur shiba

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.

Sean

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"])) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Form select option, can't get the value and display text right

From Dev

Jquery can't get the value from input form

From Dev

Can't get data attribute value from radio button but can from select option

From Dev

PHP: can't get form value to the class

From Dev

Can't get form value with PHP

From Dev

I can't get value from select option to MySQL (Spring MVC)

From Dev

Can't get value from multidimensional array

From Dev

can't get hash value from hashref

From Dev

Can't get integer value from EditText

From Dev

Can't get value from SELECT

From Dev

Can't get value from Gridview

From Dev

Can't get value from map in js

From Dev

can't get string value from edittext

From Dev

Can't get Table Value from HTML

From Dev

Can't get value from multidimensional array

From Dev

Can't get value from an array (c)

From Dev

can't get value from html tag

From Dev

Can't get value from CharField in Django

From Dev

Can't get value from JSONObject

From Dev

QCommandLineOption can't get option

From Dev

Can't get POST value when submitting form

From Dev

Angular 2 rc4: can not get the value from form

From Dev

can't pass value from form to php page

From Dev

Can't get confirmation from AJAX form submit

From Dev

Get radio value from <form>

From Dev

Can't get single values from tuple taken from Option[Double]

From Dev

Why select option change event can't have this and event.target to get selected value?

From Dev

How can I get the value from combobox in window form application form

From Dev

Get selected option value from specific select