Jump to content

Adding Error Message if No Record is found


Macman
 Share

Recommended Posts

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 8</title> </head> <body> <?phpif(isset($_GETcustID)) {	$conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn);	$custID = mysql_real_escape_string($_GETcustID)	or die ('Database not found ' . mysql_error() ); 	$sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn);	<?php if (mysql_num_rows($rs)>0) {	?> <table border="1" summary="Order Details"> <tr>	<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>	</tr> 	<?php		while ($row = mysql_fetch_array($rs)) { ?> <tr>		<td><?php echo $roworderNumber?></td>		<td> <?php echo $row orderDate?></td>	<td> <?php echo $row shipped?></td></tr>	<?php } ?></table>	<?php } else {?>	<br><br>No customer found with id  <?php echo $_GETcustID ?>in order database.</p><?php } mysql_close($conn); ?> <?php } ?>

Here is the code i haven written I tried to add a check using Row_num to display an error if no matching ID is found instead of dispalying just table headers so i think i need to move the else statement to before the creation of the table and have the table be created as the else condition though syntactically im not sure it is correct or structured correctly. I had the code reading the records before successfullyand CUSTID is the name of a textbox that posts to the PHP page to display the records Any Help on this would be greatly Appreciated

Edited by macman
Link to comment
Share on other sites

you had to many <?php start tags, and the 'or die ..' is misplaced, Edit: good job i looked again the $_GETcustID should be $_GET['custID']<?phpif(isset($_GETcustID)) { $conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn); $custID = mysql_real_escape_string($_GETcustID) or die ('Database not found ' . mysql_error() ); $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn); <?php

<?phpif(isset($_GET['custID'])) 	{	$conn = mysql_connect("localhost", "twastudent", "prac3"); 	mysql_select_db("warehouse", $conn) or die ('Database not found ' . mysql_error() );	$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0) 		{	?> 		<table border="1" summary="Order Details"> <tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr> 	<?php		while ($row = mysql_fetch_array($rs)) 			{ 	?> 		<tr>		<td><?php echo $roworderNumber?></td>		<td> <?php echo $row orderDate?></td>		<td> 			<?php echo $row shipped; ?>		</td></tr>   	<?php } //end while ?>		</table>	<?php } else { ?> 			<br><br>No customer found with id  <?php echo $_GET['custID'] ?> in order database.</p><?php } mysql_close($conn); ?> <?php } ?>

Edited by dsonesuk
Link to comment
Share on other sites

you had to many <?php start tags, and the 'or die ..' is misplaced, Edit: good job i looked again the $_GETcustID should be $_GET['custID']<?phpif(isset($_GETcustID)) { $conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn); $custID = mysql_real_escape_string($_GETcustID) or die ('Database not found ' . mysql_error() ); $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn); <?php
<?phpif(isset($_GET['custID'])) 	{	$conn = mysql_connect("localhost", "twastudent", "prac3"); 	mysql_select_db("warehouse", $conn) or die ('Database not found ' . mysql_error() );	$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0) 		{	?> 		<table border="1" summary="Order Details"> <tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr> 	<?php		while ($row = mysql_fetch_array($rs)) 			{ 	?> 		<tr>		<td><?php echo $roworderNumber?></td>		<td> <?php echo $row orderDate?></td>		<td> 			<?php echo $row shipped; ?>		</td></tr>   	<?php } //end while ?>		</table>	<?php } else { ?> 			<br><br>No customer found with id  <?php echo $_GET['custID'] ?> in order database.</p><?php } mysql_close($conn); ?> <?php } ?>

Okay thanks for that though it was the section on <?php } else { ?> <br><br>No customer found with id <?php echo $_GET['custID'] ?> in order database.</p> and before the table that i need help with as the code you posted does not create the table or error message that i want to use
Link to comment
Share on other sites

Okay thanks for that though it was the section on <?php } else { ?> <br><br>No customer found with id <?php echo $_GET['custID'] ?> in order database.</p> and before the table that i need help with as the code you posted does not create the table or error message that i want to use
Not sure if I understand you correctly but it seems you are asking why database is not created..am I right? If this is correct this line of code does not create the database..
$conn = mysql_connect("localhost", "twastudent", "prac3");

it only connects you to an existing database(one you have already)..this is an example from W3schools tutorials for database creation

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con)   {   die('Could not connect: ' . mysql_error());   } if (mysql_query("CREATE DATABASE my_db",$con))   {   echo "Database created";   } else   {   echo "Error creating database: " . mysql_error();   } mysql_close($con); ?>

Link to comment
Share on other sites

The Database is fine and created i know this as i have accessed it before i have messed up my code but i want to use num-Rows to create an error message if not match is found ie the PHP file is an action to a form which allows the user to enter in an ID and returns the results where the ID matches ie Orders Basically My code is now not displaying the table and associated records. as the goal of the program is to return the associated records or a suitable error message which is what the echo at the bottom of the page should be the error message though i also will add an anchor tot he message so that the user can enter another ID to try again.Sorry if there was confusion the $ con code is to make a connection to the database on the server i know that thi s part of the code is correct I believe either the num-rows or that ection to be at fault as i had the code displaying correctly earlier

Link to comment
Share on other sites

<?phpif(isset($_GET['custID']))	{	$conn = mysql_connect("localhost", "twastudent", "prac3");	mysql_select_db("warehouse", $conn) or die ('Database not found ' . mysql_error() );	$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0)		{	?>		<table border="1" summary="Order Details"> <tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr>	<?php		while ($row = mysql_fetch_array($rs))			{	?>		<tr>		<td><?php echo $row['orderNumber']; ?></td>		<td> <?php echo $row['orderDate']; ?></td>		<td>			<?php echo $row['shipped']; ?>		</td></tr>  	<?php } //end while ?>		</table>	<?php } else { ?>			<br><br>No customer found with id  <?php echo $_GET['custID']; ?> in order database.</p><?php		} mysql_close($conn); }?>

<?php echo $roworderNumber; ?></td> <td> <?php echo $roworderDate; ?
here ';' was miissing and format of array $row was wrong.
Link to comment
Share on other sites

<?phpif(isset($_GET['custID']))	{	$conn = mysql_connect("localhost", "twastudent", "prac3");	mysql_select_db("warehouse", $conn) or die ('Database not found ' . mysql_error() );	$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0)		{	?>		<table border="1" summary="Order Details"> <tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr>	<?php		while ($row = mysql_fetch_array($rs))			{	?>		<tr>		<td><?php echo $row['orderNumber']; ?></td>		<td> <?php echo $row['orderDate']; ?></td>		<td>			<?php echo $row['shipped']; ?>		</td></tr>  	<?php } //end while ?>		</table>	<?php } else { ?>			<br><br>No customer found with id  <?php echo $_GET['custID']; ?> in order database.</p><?php		} mysql_close($conn); }?>

here ';' was miissing and format of array $row was wrong.

Okay But with the code shown below i am getting an error unexpected T_STRING, expecting ',' or ';' on line 13 which is the first row of the table by my count
?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 8</title> </head> <body> <?phpif(isset($_GET['custID'])) {	$conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn);	$custID = mysql_real_escape_string($_GETcustID)	or die ('Database not found ' . mysql_error() );	 $sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn); ?> <table border="1" summary="Order Details"> <tr>	<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>	</tr>	<?php		while ($row = mysql_fetch_array($rs)) { ?> <tr>		<td><?php echo $row orderNumber;?></td>		<td> <?php echo $row orderDate;?></td>	<td> <?php echo $row shipped; ?></td></tr>	<?php } ?></table>		</body>	</html>	<?php } mysql_close($conn); ?>

Hopefully i sm closer than before as i am getting annoyed with myself

Edited by macman
Link to comment
Share on other sites

Why have you got two while loops? try removing space from die and open bracket 'die (' to 'die('
Right i am getting confused what do you mean two while loops i haven't got two thouh this is a simplified version of the code posted above though i want to use Num-rows as per the top post but my strutcure is way off at the moment I have moved the die to turn it into die(though i don't care about the die statement at the moment I want to turn the above into displaying the records while >0 records are found as matching else dipslay the error which is found at the end of the first post.Sory about the confusion on my part now here is the basis for what i want now i want to add a ceheck for NUm_rows as per the original post
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Prac 3 Task 8</title> </head> <body> <?phpif(isset($_GET['custID'])) {	$conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn);	$custID = mysql_real_escape_string($_GET['custID'])	or die ('Database not found ' . mysql_error() );	$sql = "SELECT orderNumber,orderDate, shipped from orders Where orders.customerID ='$custID' "; $rs = mysql_query($sql,$conn)	or die ('Problem with query' . mysql_error());?> <table border="1" summary="Order Details"> <tr>	<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>	</tr> 		<?php		while ($row = mysql_fetch_array($rs)) { ?> <tr>		<td><?php echo $row["orderNumber"]?></td>		<td> <?php echo $row ["orderDate"]?></td>	<td> <?php echo $row ["shipped"]?></td></tr><?php } mysql_close($conn); ?> </table><?php } ?> </body></html>

This code displays the records i had stuffed it up before :)To clarify the code specifically is if (mysql_num_rows($rs)>0) { // ie if a record is foun return the table of results else return the echo message Can you help me on this please and thanks for your patience

Edited by macman
Link to comment
Share on other sites

your original posting had TWO while loops, now you that have edited the posting you have removed the second while loop. Now your code has almost gone back to the original? birbal gave you the corrected code. Going by birbal code you would just need to make the adjust i suggestedthe 'or die()' should be used for testing connection and database access$conn = mysql_connect("localhost", "twastudent", "prac3"); mysql_select_db("warehouse", $conn);if you are not bothered about the or die, heres an idea, REMOVE IT COMPLETELY. then when you have table and error message displayed, reintroduce it.

Edited by dsonesuk
Link to comment
Share on other sites

though i don't care about the die statement at the moment I want to turn the above into displaying the records while >0 records are found as matching else dipslay the error which is found at the end of the first postGo to the top of the page first Post
you are doing same mistakes. you may want to check the post 6 again.
Link to comment
Share on other sites

you are doing same mistakes. you may want to check the post 6 again.
The code i just posted as a correction works as expected with the die though i am not expecting it to function as i know the database exists basically i just want to implement the num_rows and get that working your code birbal didn't work for me when i tried it maybe i copied it wrong though the code i just added to mey last post displays the record when i search for the ID so basically i want to do a check if the num_rows <=0 echo the error else display the table to avoid printing just the table headers.My apologies if i am getting snarly with people the cod ei put here is right except for the functions i didn't mean to be offensive if i have been Edited by macman
Link to comment
Share on other sites

I suppose you do release, you wont get and table or message appear until 'custID' value is sent through GET.
I figured i would not get a message until after the ID is sent through get as the form is sent to fill the table else it should siplay an error i am also thinking of adding an anchor back to the original form ie please enter a new ID link though it appears instantly for me when i run it though basically the check is meant to happen after the GET anyway as the value sent is checked in the while statement anyway :) so after it is sent notify the user that the data is not found and using the anchor should provide the echoed error and an option to allow them to enter a correct ID etcas it stands if 33432342342 for example was entred i the the Table headers though as it is a non matching id so i believe that the echo should move before the table is created so that it checks the false then returns the table only if a match is foundso basically a num_rows loop that dispalys an echo if num_rows <=0 i think would work though should i make that 1 to exclude the table headers which are on a row I have not done this before Edited by macman
Link to comment
Share on other sites

sorry, you are keep changing the codes... i am confused. post the current code and post what is happening after passing the get data into the script.another thing you can do write a statement to print the echo of mysql_num_rows() to be confirm whats actualy is returning.

Link to comment
Share on other sites

you should have code similar to below

<?phpif(isset($_GET['custID']))	{	$conn = mysql_connect("localhost", "twastudent", "prac3");		/*if (!$conn)		  {		  die('Could not connect: '. mysql_error());		  }		mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );*/		mysql_select_db("warehouse", $conn);			$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped FROM orders WHERE orders.customerID ='$custID'"; 	 $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0)		{	?>		<table border="1" summary="Order Details">		<tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr>	<?php		while ($row =  mysql_fetch_assoc($rs))			{	?>		<tr>		<td><?php echo $row['orderNumber']; ?></td>		<td> <?php echo $row['orderDate']; ?></td>		<td><?php echo $row['shipped']; ?></td>		</tr>  	<?php			 } //end while 	?>		</table><?php 		}	else 		{ ?>			<br><br>No customer found with id  <?php echo $_GET['custID']; ?> in order database.</p><?php		}mysql_close($conn);} ?>

make sure field names used in sql and while loop match exactly field/column names in table, if this works you can remove the /* ...*/ for or die statements, and comment out the mysql_select_db("warehouse", $conn); .

Link to comment
Share on other sites

you should have code similar to below
<?phpif(isset($_GET['custID']))	{	$conn = mysql_connect("localhost", "twastudent", "prac3");		/*if (!$conn)		  {		  die('Could not connect: '. mysql_error());		  }		mysql_select_db("warehouse", $conn) or die('Database not found '. mysql_error() );*/		mysql_select_db("warehouse", $conn);			$custID = mysql_real_escape_string($_GET['custID']);	 $sql = "SELECT orderNumber,orderDate, shipped FROM orders WHERE orders.customerID ='$custID'"; 	 $rs = mysql_query($sql,$conn);	if (mysql_num_rows($rs)>0)		{	?>		<table border="1" summary="Order Details">		<tr>		<th>orderNumber</th>  <th> Order Date</th> <th> Shipped</th>		</tr>	<?php		while ($row =  mysql_fetch_assoc($rs))			{	?>		<tr>		<td><?php echo $row['orderNumber']; ?></td>		<td> <?php echo $row['orderDate']; ?></td>		<td><?php echo $row['shipped']; ?></td>		</tr>  	<?php			 } //end while 	?>		</table><?php 		}	else 		{ ?>			<br><br>No customer found with id  <?php echo $_GET['custID']; ?> in order database.</p><?php		}mysql_close($conn);} ?>

make sure field names used in sql and while loop match exactly field/column names in table, if this works you can remove the /* ...*/ for or die statements, and comment out the mysql_select_db("warehouse", $conn); .

Thanks i will try that very kind of you :) will postback results using the code you spplied i am getting an error at the else statementof PHP Parse error: syntax error, unexpected '}' on line 44 if i remove that i get unexpected else Edited by macman
Link to comment
Share on other sites

you must have extra } somewhere? i just ran exact code, using field names from my database, and it worked fine, list record if id, found, show message if not.
I copied the code you posted it ran fine once i removed an additional <php tag i had I don't know what i did wrong though :) that it works as expected
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...