[英]Using if statement when using values from the database
我有一個查詢,將結果數據輸出到表中。 該表中有兩個字段對於此if
語句至關重要: Max Capacity
和Current Capacity
。 這些來自我們的數據庫,來自同一表。
我想寫一個if
語句:
if (Max_C == Current_C){
echo "Sorry, course is full.";
}
else{
$insert = mysql_query ("insert into blahblahblah... ");
}
我該怎么做呢? 如何從數據庫中指定max_c
和current_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!';
}
這是插入代碼:
//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");
}
您可以使用以下代碼進行操作:
//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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.