Nazirul Posted April 13, 2009 Share Posted April 13, 2009 Hello this is my code.. i can't use the if-else statement.. any idea? $query = "SELECT * FROM $table WHERE student_number LIKE '".$_POST['txtSearch']."'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $Student_Number = $row['Student_Number']; $Student_Name = $row['Student_Name']; $Qualification = $row['Qualification']; $Status = $row['Status']; $kirim = $_POST['txtSearch']; if ($kirim == $Student_Number) { echo "You are success"; } else { echo("You are failed!!!"); } } i want to display a msg if the student success or not... is it the correct way for it? thanks Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/ Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Hmm...what? Is this code not working or something? Is the message not being displayed correctly? Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808731 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 2 things: 1) Why can't you use if-else? 2) If you're going to use LIKE in your SQL statement, surround the value with %: $query = "SELECT * FROM $table WHERE student_number LIKE '%".$_POST['txtSearch']."%'"; If this is a school assignment and you can't use if-else, you're probably looking for the ternary operator: http://us.php.net/operators.comparison Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808733 Share on other sites More sharing options...
dadamssg Posted April 13, 2009 Share Posted April 13, 2009 the parenthesis around the You are Failed!! part may have something to do with it Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808736 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 the parenthesis around the You are Failed!! part may have something to do with it That is perfectly legit. Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808743 Share on other sites More sharing options...
Nazirul Posted April 13, 2009 Author Share Posted April 13, 2009 the code is actually working ... it is just try-n-error code... but the problem is... i want to display an error when the student number did not match in the table... how thats gonna be... i've spend a few hours for that... (im newbie anyway, ) Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808755 Share on other sites More sharing options...
ober Posted April 13, 2009 Share Posted April 13, 2009 Is it not doing that right now? What you have looks ok to me. Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808761 Share on other sites More sharing options...
Nazirul Posted April 13, 2009 Author Share Posted April 13, 2009 if ($kirim == $Student_Number) { echo "You are success"; } [b] else { echo("You are failed!!!"); }[/b] i have a problem with the 'else' statement... when the student did not match like in the table, it display nothing... that is " echo("You are failed!!!"); " did not works... ??? Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808770 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Well, it's got to display one of the two lol. Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808784 Share on other sites More sharing options...
Nazirul Posted April 13, 2009 Author Share Posted April 13, 2009 Well, it's got to display one of the two lol. lol... its confusing me... it is either one must work... i've tried ... if ($kirim != $Student_Number) { echo "You are failed!!!"; } else { echo("You are success"); } it just does not output the correct echo when the student number was wrong... no luck... Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808802 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 So what does it actually display when they do not match? Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808831 Share on other sites More sharing options...
Nazirul Posted April 13, 2009 Author Share Posted April 13, 2009 an empty page...why is that happen? is there any simple way for searching... that will display success or not success... thanks Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808868 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 You need to turn on your errors I believe. display_errors(E_ALL); may enlighten you... Quote Link to comment https://forums.phpfreaks.com/topic/153878-simple-php-searching-plz-take-a-look/#findComment-808871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.