07960 Posted August 16, 2012 Share Posted August 16, 2012 Hi Im very new to this forum and also to PHP in general - its a summer topic covered at my uni. I am trying to create a simple form for uni, which, when you type in a serial number or licence number, it checks mysql, and if the licence/serial is in the db it displays "product supported" and if its not in the db it displays "product not supported" My script below is not working and automatically displays "product supported" even when its not. Could anyone please help and point me in the right direction ? //code// <?php $serial = $_GET['serial']; $licence = $_GET['licence']; $referer = $_SERVER['HTTP_REFERER']; # if either form field is empty return 1st page if( ( !$serial ) and ( !$licence ) ) { header( "Location:$referer" ); exit(); } #connect to MySQL $conn=@mysql_connect( "localhost", "root", "" ) or die( "Could not connect" ); #select the specified database $rs = @mysql_select_db( "assignment", $conn ) or die( "Could not select database" ); #create the query (select all from table "supported" where values match the inputted data) $sql = "select * from supported where serial=\"$serial\" or licence =( \"$licence\" )"; #execute the query $rs = mysql_query( $sql, $conn ) or die( "Could not execute query" ); #get number of rows that match query $num = mysql_num_rows( $rs ); #if there is a match the query produces a message if( $num != 0 ) { $msg = "<h3>Product is supported</h3>"; } else if ($num != ); { $msg1 = "<h3> Product is NOT supported</h3>"; } // header( "Location:$referer" ); exit(); mysql_close($conn); ?> <html> <head> <title>Product Supported</title> </head> <body style="color:green; background:black;"> <?php echo( $msg ), //getting error relating to this line else echo ( $msg1); ?> </body> </html> //code ends// Additional Details Im Getting the error between this point: <?php echo( $msg ), //getting error relating to this line else echo ( $msg1); ?> I ve tried adding the array from where I set the variable for $msg1 but still it just displays the 1st echo $msg and errors $msg1 Any help is much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/ Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 Are you coding with error reporting turned on? Why is this in brackets? licence =( \"$licence\" ) You have an else without a matching if. Use $msg for both possibilities, drop $msg1, there's no need for it. Simply echo $msg and it will contain whatever it was populated with in your if/else conditional above. Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369920 Share on other sites More sharing options...
07960 Posted August 16, 2012 Author Share Posted August 16, 2012 Hi Yes I have error reporting switched on. I have changed the code as you suggested to not include $msg1. But now it is displaying every product as not supported whether it is or it isnt. Is the issue round this part ? : if( $num != 0 ) { $msg = "<h3>Product is supported</h3>"; $msg = "<h3> Product is NOT supported</h3>"; } And if so, what would I put in its place ? Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369923 Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 if( $num != 0 ) { $msg = "<h3>Product is supported</h3>"; else { $msg = "<h3> Product is NOT supported</h3>"; } Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369931 Share on other sites More sharing options...
07960 Posted August 16, 2012 Author Share Posted August 16, 2012 Hi I figured on adding else like your script, but Im nor sure where the first { is meant to close ? so I did this :- if( $num != 0 ) { $msg = "<h3>Product is supported</h3>"; } else {$msg = "<h3> Product is NOT supported</h3>";} but now its displaying the 1st message on all items ! Where a bouts would I enter the error reporting ? ini_set('display_errors',1); error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369934 Share on other sites More sharing options...
07960 Posted August 16, 2012 Author Share Posted August 16, 2012 It does partially work. If I only fill in one field (as it should be) on the html form, it returns product supported. But If I fill in both fields on the previous html form (serial & licence) then it returns product not supported... Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369939 Share on other sites More sharing options...
Maq Posted August 16, 2012 Share Posted August 16, 2012 07960, please use or tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369959 Share on other sites More sharing options...
Barand Posted August 16, 2012 Share Posted August 16, 2012 try $sql = "select * from supported where (serial='$serial') or (licence = '$licence' )"; Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369961 Share on other sites More sharing options...
07960 Posted August 16, 2012 Author Share Posted August 16, 2012 Hi No it still doesnt work :-S This is the code with the correct code tags :-P This is the code for html form : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Checker</title> </head> <body style="color:green; background:black;"> This form displays whether the hardware or software is supported. <br><br> Please type the HARDWARE serial number or SOFTWARE licence number below.. <br><br> <!--php checkscript needs to be named supported.php --> <form action="supported.php" method="get"> HARDWARE SERIAL NUMBER : <br> <input type="text" name="serial"> <br><br> SOFTWARE LICENCE NUMBER : <br> <input type="text" name="licence"> <br><br> <input type="submit" value="Submit"> </form> </body> </html> and this is the (now edited) code on the php code : <?php $serial = $_GET['serial']; $licence = $_GET['licence']; $referer = $_SERVER['HTTP_REFERER']; # if either form field is empty return 1st page if( ( !$serial ) and ( !$licence ) ) { header( "Location:$referer" ); exit(); } #connect to MySQL $conn=@mysql_connect( "localhost", "root", "" ) or die( "Could not connect" ); #select the specified database $rs = @mysql_select_db( "assignment", $conn ) or die( "Could not select database" ); #create the query (select all from table "supported" where values match the inputted data) //$sql = "select * from supported where serial=\"$serial\" or licence = \"$licence\" "; $sql = "select * from supported where (serial='$serial') or (licence = '$licence' )"; #execute the query $rs = mysql_query( $sql, $conn ) or die( "Could not execute query" ); #get number of rows that match query $num = mysql_num_rows( $rs ); #if there is a match the query produces a message if( $num != 0 ) {$msg = "<h3>Product is supported</h3>"; } else { $msg = "<h3> Product is NOT supported</h3>";} // header( "Location:$referer" ); exit(); mysql_close($conn); ?> <html> <head> <title>Product Supported</title> </head> <body style="color:green; background:black;"> <?php echo( $msg ) ?> </body> </html> Its not throwing back any errors, its just saying everything is supported when it should be either supported or not. Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369967 Share on other sites More sharing options...
Barand Posted August 16, 2012 Share Posted August 16, 2012 I suspect you may have blank input fields matching blank data fields Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369972 Share on other sites More sharing options...
Barand Posted August 16, 2012 Share Posted August 16, 2012 you could try something like this $whereclause = ''; $where = array(); if ($serial) $where[] = "(serial='$serial')"; if ($licence) $where[] = "(licence='$licence')"; if (count($where) > 0) $whereclause = "WHERE" . join(' OR ', $where); $sql = "SELECT * FROM supported $whereclause"; Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369977 Share on other sites More sharing options...
07960 Posted August 17, 2012 Author Share Posted August 17, 2012 I finally resolved this. The query was wrong : $sql = "select * from supported where (serial='$serial') or (licence = '$licence' )"; [/php} Should be [code=php:0] $sql = "select * from supported where (serial='$serial') and (licence = '$licence' )"; Thanks for the support ! Quote Link to comment https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1370193 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.