Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369920
Share on other sites

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 ?

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369923
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369934
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369967
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1369977
Share on other sites

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 !

Link to comment
https://forums.phpfreaks.com/topic/267183-simple-php-query/#findComment-1370193
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.