1

I have a query that outputs the resulting data into a table. I have two fields in that table that are vital for this if statement: Max Capacity and Current Capacity. These are from our database, from the same table.

I want to write an if statement:

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

How do I do this? How do I specify the max_c and current_c from the db?

That section of code:

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
  • are you asking how do the db query? or? Commented Apr 29, 2012 at 21:11
  • 1
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process . Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented Apr 30, 2012 at 2:51

2 Answers 2

0

Here is the insert code:

    //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");
    }
Sign up to request clarification or add additional context in comments.

Comments

-1

You can do it using the following code:

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

14 Comments

thanks i will try this as soon as i get home again i need to go and buy something from the store for my mom.
oi can you explain the //mysql insert code? do i have to do it like $insert=mysql_query (.....) ?
if i show you what i've done so far can you help me? i'm having trouble :(
because i am getting current capacity as an output by doing a count on how many particular session IDs exist.
i have a query that finds a session for a course at a particular time. i want to implement an if else statement where if there is room left in a session at the time the user enters, an insert query will fire if there is room. if not, it will just say its full. the max enrollment is pre determined, and the current enrollment is found by doing a count.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.