使用数据库中的值时使用if语句

时间:2012-04-29 21:05:30

标签: php html sql if-statement

我有一个查询将结果数据输出到表中。我在该表中有两个对此if语句至关重要的字段:Max CapacityCurrent Capacity。这些来自我们的数据库,来自同一张表。

我想写一个if声明:

if (Max_C == Current_C){
   echo "Sorry, course is full.";
}
else{
   $insert = mysql_query ("insert into blahblahblah... ");
}

我该怎么做?如何从db中指定max_ccurrent_c

代码部分:

if($courseandtime) {  $max_e = $courseandtime['MaxEnrollment']; $current_e = $courseandtime['COUNT(REGISTERED.SID)']; } 

    if ($current_e < $max_e) {
        echo "
       <form action='register-exec.php' method='post'>
       <label>Time: $_POST[Time]</label><br>
       <input type='hidden' value='$Time' /><br><br>
       <label>Student ID:</label><br>
       <input type='text' name='SUCID' id='SUCID' /><br><br>
       <label>CID: $_POST[CID]</label><br>
       <input type='hidden' name='CID' id='CID' /><br><br>
           <label>SID: $row['SID']</label><br>
       <input type='hidden' name='SID' id='SID' /><br><br>
       <button type='submit'>Register</button>
       </form>
       ";
    }
    else {
        echo 'Sorry, were full!';
    }

2 个答案:

答案 0 :(得分:1)

这是插入代码:

    //Create query
    $qry="INSERT INTO table (column-name, another-column) VALUES ('$variable', '$valueyouwanttoinsert')";
    $result=mysql_query($qry);
    if ($result) {
    echo "Success!";
    }
    else {
    die("Query Failed");
    }

答案 1 :(得分:0)

您可以使用以下代码执行此操作:

//Create query
$qry="SELECT * FROM table WHERE column='$variable'";
$result=mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    $course = mysql_fetch_assoc($result);
        $max_c = $course['max_c'];
        $current_c = $course['current_c'];
}
else {
    die("Query failed");
}
    if ($current_c == $max_c){
        echo "Sorry, the course is full!";
    }
    else {
        //MySQL Insert Code here
    }