marksie1988 Posted June 1, 2007 Share Posted June 1, 2007 i am new to php and i dont know how i would get some data from a mysql database to display when a person enters a number in a form basically i am creating an order tracking page which will have a form that asks for an order number and the persons postcode, on entering the correct postcode with the correct order number the customer will be shown their orders status. all i dont know is how i would tell the form to check the database and make sure that the postcode and the order number were the same in that row? i havent done much work with forms. Thanks Steve Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/ Share on other sites More sharing options...
piznac Posted June 1, 2007 Share Posted June 1, 2007 I believe you will need two pages. One to have the form where the user enters the number. Then the form action will take you to the next page via a submit button. Then you can use the $_POST[] var to pull your query. Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266468 Share on other sites More sharing options...
marksie1988 Posted June 1, 2007 Author Share Posted June 1, 2007 ok i now have some script that i created but i need it to do an if statement for 2 variables e.g. if($ordernumber + $postcode){ echo $order; } else{ echo "There are no repaits in the database for that postcode and order number."; } do you see what i mean? im not sure how i get the if($ordernumber + $postcode) to work as it errors when i do it like that Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266584 Share on other sites More sharing options...
marksie1988 Posted June 2, 2007 Author Share Posted June 2, 2007 ok so i have looked around and i got how to do the if statement u use && instead of + but my form still returns nothing here is the code <?php $db =mysql_connect ("localhost","dbuser","dbpass"); mysql_select_db("db to use",$db); if($on && $post) { // query DB $sql = "SELECT * FROM repair ORDER BY status postdate DESC WHERE on ='$on' post ='$post'"; $result = mysql_query($sql); if(mysql_num_rows($result) > 0){ while($row=mysql_fetch_array($result)){ echo "Date Submitted: $row[postdate]<br>"; echo "Order Number: $row[on]<br>"; echo "Postcode: $row[postcode]<br><br>"; echo "Status: $row[status]<br><br>"; echo "Comments: $row[comments]<br><br>"; echo "Technician: $row[technician]<br><br>"; echo "Due By: $row[dueby]<br><br>"; } }else{ echo "There are no repaits in the database for that postcode and order number."; } //display data }else{ //display form ?> <form method=post action="<?php echo $PHP_SELF?>"> <p> Order number: <input type="text" name="order number"> </p> <p>Postcode: <input type="text" name="postcode"> </p> <p> <input type="submit"> </p> </form> <?php } echo $sql ?> i have the code uploaded here http://blacklimecomputers.co.uk/track/check.php when i look at the source it seems that the form doesnt know to action to itself any ideas what i can do??? Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266604 Share on other sites More sharing options...
penguin0 Posted June 2, 2007 Share Posted June 2, 2007 Try this instead: <form method="post" action="<?=$_SERVER['PHP_SELF']";?>"> Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266606 Share on other sites More sharing options...
marksie1988 Posted June 2, 2007 Author Share Posted June 2, 2007 ok tried <form method=post action="<?php echo "$_SERVER['PHP_SELF']"; ?>"> didnt work so i tried <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>"> that worked but when i insert test order number and test postcode it doesnt return anything test order number is: 123456789 test postcode is: ls8 1bu i get the following error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/marksie/public_html/blacklime/track/check.php on line 11 There are no repaits in the database for that postcode and order number.SELECT * FROM repair ORDER BY status postdate DESC WHERE on ='123456789' postcode ='ls8 1bu' and the php is now <?php $db =mysql_connect ("localhost","dbuser","dbpass"); mysql_select_db("dbname",$db); $on = $_POST['on']; $post = $_POST['postcode']; if($on && $post) { // query DB $sql = "SELECT * FROM repair ORDER BY status postdate DESC WHERE on ='$on' postcode ='$post'"; $result = mysql_query($sql); if(mysql_num_rows($result) > 0){ while($row=mysql_fetch_array($result)){ echo "Date Submitted: $row[postdate]<br>"; echo "Order Number: $row[on]<br>"; echo "Postcode: $row[postcode]<br><br>"; echo "Status: $row[status]<br><br>"; echo "Comments: $row[comments]<br><br>"; echo "Technician: $row[technician]<br><br>"; echo "Due By: $row[dueby]<br><br>"; } }else{ echo "There are no repaits in the database for that postcode and order number."; } //display data }else{ //display form ?> <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p> Order number: <input type="text" name="on"> </p> <p>Postcode: <input type="text" name="postcode"> </p> <p> <input type="submit"> </p> </form> <?php } echo $sql ?> Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266607 Share on other sites More sharing options...
dbillings Posted June 2, 2007 Share Posted June 2, 2007 Try changing your sql select statement to this $sql = "SELECT * FROM repair ORDER BY status postdate DESC WHERE on ='$on' AND postcode ='$post'"; $result = mysql_query($sql)or die('Error: '.mysql_error()); Link to comment https://forums.phpfreaks.com/topic/53893-solved-form-that-checks-2-fields-if-correct-shows-info/#findComment-266648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.