Jump to content

PHP and MYSQL connection with Redirect


Macman
 Share

Recommended Posts

Okay I know that i need to move the $conn out of the if loop and i can see that i have closed it incorrectly/at the wrong point

<?php error_reporting(E_ALL);require_once("nocache.php"); // Stop information being cached	   if (isset($_POST["Submit"])) // Check form was submitted  {  $conn = mysql_connect("localhost", "****", "****");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact,customerCar.rego FROM customer,customerCar where customer.familyName = $Family OR customer.contact = $Phone OR customerCar.rego = $Rego";     $rs = mysql_query($sql,$conn);  if(!$rs)  echo mysql_error($conn);  $Family = $_POST["Family"];  $Phone = $_POST["Phone"];  $Rego  = $_POST ["Vehicle_Rego"];  }?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action=""><label for = "Family"> Customer Family Name </label> <input type = "text" name = "Family" id = "Family"></input><label for = "Phone" > Customer Phone Number </label> <input type = "text" name = "Phone" id = "Phone"> </input><label for = "Vehicle_Rego" > Vehicle Registration Number </label> <input type = "text" name = "Vehicle_Rego" id = "Vehicle_Rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?php 		while ($row = mysql_fetch_array($rs)) { ?> <tr>	<td> <?php echo $row ["Family"];?> </td>	<td> <?php echo $row ["Phone"];?>	</td>	<td> <?php echo $row ["Vehicle_Rego"]; } ?>		 </td>	</tr>[indent][/indent]<?php  mysql_close($conn);?> </table></body></html>

Basically I am Getting An Error $rs undefined which I want to make it search and produce a list of matches with some additional fields i have yet to add though i can figure that out I would like to have "Family" Create A hyperlink which matches the customer name to a number and redirects to a listing of booking information Ideally.Currently my two errors in this are the mysql close and the $rs which i believe is due to me closing the loop early Any help to solve this is greatly Appreciated

Link to comment
Share on other sites

the problem is not the location of $conn. Why else would you want to make a connection unless the form has been submitted first? The problem is that your query is trying to reference variables that don't exist yet, i.e. when you assign the respective values of POST to $Family, $Phone, and $Rego. Those assignments should happen before you assign your query to $sql since in your query those are the variables you are trying to reference.

Link to comment
Share on other sites

Ok i thought as much is there anything else that you can see wrong with it So you are telling me to Set the values ie place the Query After the Form Element as it contains the input fields the pge has to submit to itself to produce a list of matching records which i believe the $row would produce Uisng my Above code where would i place the $SQL exactly i think i may have a few other errors there as wellThanks for your Speedy Response

Edited by macman
Link to comment
Share on other sites

no. this has nothing to with the form elements themselves, only how you are getting their values. you are making a query, right? using the values of the form inputs?

$sql = "SELECT customer.familyName,customer.contact,customerCar.rego FROM customer,customerCar where customer.familyName = $Family OR customer.contact = $Phone OR customerCar.rego = $Rego";

at this point in your code, those variables that aren't defined yet. (i.e. $Family, $Phone, $Rego).You define them after the fact

$Family = $_POST["Family"];$Phone = $_POST["Phone"];$Rego  = $_POST["Vehicle_Rego"];

if you want to use $Family, $Phone, and $Rego in the query with the values they are given upon the form being submitted, then you need assign them the values from POST before you use them in the query. i.e.

if(isset($_POST['submit'])){  $Family = $_POST["Family"];  $Phone = $_POST["Phone"];  $Rego  = $_POST["Vehicle_Rego"];  $conn = mysql_connect("localhost", "****", "****");   mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact,customerCar.rego FROM customer,customerCar where customer.familyName = $Family OR customer.contact =     $Phone OR customerCar.rego = $Rego";   .  .

Edited by thescientist
Link to comment
Share on other sites

Yes i am making a query i know that i had to define them it was a matter of where to put the $SQL as you have shown the $Family etc are in the if isset it was more a case of where to put the $SQL but i can see now that it is to go after the $ variables Thanks i will try that and post back any errors

<?php error_reporting(E_ALL);require_once("nocache.php"); // Stop information being cachedif(isset($_POST['submit'])){  $Family = $_POST["familyName"];  $Phone = $_POST["contact"];  $Rego  = $_POST["rego"]; $conn = mysql_connect("localhost", "*****", "****");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact, FROM customer where customer.familyName = $Family OR customer.contact =    $Phone'"    ?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action="<?php echo $_SERVER["PHP_SELF"];?>"><label for = "familyName"> Customer Family Name </label> <input type = "text" name = "familyName" id = "familyName"></input><label for = "contact" > Customer Phone Number </label> <input type = "text" name = "contact" id = "contact"> </input><label for = "rego" > Vehicle Registration Number </label> <input type = "text" name = "rego" id = "rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?php 		while ($row = mysql_fetch_array($rs)) { ?> <tr>	<td> <?php echo $row ["familyName"];?> </td>	<td> <?php echo $row ["contact"];?>	</td>		</tr><?php  mysql_close($conn); }?> </table></body></html>

I also think that the while if isset could contain the bottom echo statements as it should not have anything there except for the form and when submit is clicked and the value is posted to itself then the query should run which i think i have it in the wrong order correct because we should be checking if the value entered matches the database while i have it as database matches value entered as you can see it may not make a difference though.so i think it should be where $family = customer.familyNameYour help is greatly appreciated

Edited by macman
Link to comment
Share on other sites

You are missing to call mysql_query() which will execute the $sql.put the mysql_close() outside the while loop. if its in the loop connection will be closed on the first iteration.

Link to comment
Share on other sites

Thanks for that i will try it Have i got it this time

<?php error_reporting(E_ALL);require_once("nocache.php"); // Stop information being cachedif(isset($_POST['submit'])){  $Family = $_POST["familyName"];  $Phone = $_POST["contact"];  $Rego  = $_POST["rego"]; $conn = mysql_connect("localhost", "twastudent", "prac3");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact, FROM customer where customer.familyName = $Family OR customer.contact =    $Phone'";  $rs = mysql_query($sql,$conn)    ?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action="<?php echo $_SERVER["PHP_SELF"];?>"><label for = "familyName"> Customer Family Name </label> <input type = "text" name = "familyName" id = "familyName"></input><label for = "contact" > Customer Phone Number </label> <input type = "text" name = "contact" id = "contact"> </input><label for = "rego" > Vehicle Registration Number </label> <input type = "text" name = "rego" id = "rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?php 		while ($row = mysql_fetch_array($rs)) { ?> <tr>	<td> <?php echo $row ["familyName"];?> </td>	<td> <?php echo $row ["contact"];?>	</td>		</tr><?}?></table><?php  mysql_close($conn); ?> </body></html>

I thought i had something silly there :)using what i posted results in PHP Parse error: syntax error, unexpected $end in Page3.php on line 50 which i think i s caused by me not closing the loop correctly i would assume thanks for your help

Edited by macman
Link to comment
Share on other sites

$rs = mysql_query($sql,$conn);missed semicolon here.

Link to comment
Share on other sites

$rs = mysql_query($sql,$conn);missed semicolon here.
 <?php error_reporting(E_ALL);require_once("nocache.php"); // Stop information being cachedif(isset($_POST['submit'])){  $Family = $_POST["familyName"];  $Phone = $_POST["contact"];  $Rego  = $_POST["rego"]; $conn = mysql_connect("localhost", "twastudent", "prac3");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact, FROM customer where customer.familyName = $Family OR customer.contact =    $Phone'";  $rs = mysql_query($sql,$conn); ?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action="<?php echo $_SERVER["PHP_SELF"];?>"><label for = "familyName"> Customer Family Name </label> <input type = "text" name = "familyName" id = "familyName"></input><label for = "contact" > Customer Phone Number </label> <input type = "text" name = "contact" id = "contact"> </input><label for = "rego" > Vehicle Registration Number </label> <input type = "text" name = "rego" id = "rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?php 		while ($row = mysql_fetch_array($rs)) { ?> <tr>	<td> <?php echo $row ["familyName"];?> </td>	<td> <?php echo $row ["contact"];?>	</td>		</tr><?php }?><?php  mysql_close($conn); }?> </table></body></html>

With this i get a blank page with no form no errors displayed so i cannot enter the data and the previous set i get the unexpected $end at the final line of the code which i think is caused by the loop IF (isset) I am stressing a bit about it

Link to comment
Share on other sites

your query has two quotes at the end, a single and a double. should only be a double. you could try making the end a bit easier on yourself to code.

<table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?php while ($row = mysql_fetch_array($rs)){  echo "<tr>";  echo "<td>" . $row["familyName"] . "</td>";  echo "<td>" . $row["contact"] . "</td>";  echo "</tr>";};mysql_close($conn); ?> </table>

Link to comment
Share on other sites

Okay iw ill do that and see are there anymore errors there i am struggling with this i will need to add more selections to this but it should be enough to test at leastIf it helps you i can post table contents for one table and a table structure i tried that and am getting unexpected $end on line 50otherwise this code

 <?phperror_reporting(E_ALL);require_once("nocache.php"); // Stop information being cachedif(isset($_POST['submit'])){  $Family = $_POST["familyName"];  $Phone = $_POST["contact"];  $Rego  = $_POST["rego"];$conn = mysql_connect("localhost", "twastudent", "prac3");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact, FROM customer where customer.familyName = $Family OR customer.contact =    $Phone";  $rs = mysql_query($sql,$conn);?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action="<?php echo $_SERVER["PHP_SELF"];?>"><label for = "familyName"> Customer Family Name </label> <input type = "text" name = "familyName" id = "familyName"></input><label for = "contact" > Customer Phone Number </label> <input type = "text" name = "contact" id = "contact"> </input><label for = "rego" > Vehicle Registration Number </label> <input type = "text" name = "rego" id = "rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?phpwhile ($row = mysql_fetch_array($rs)){  echo "<tr>";  echo "<td>" . $row["familyName"] . "</td>";  echo "<td>" . $row["contact"] . "</td>";  echo "</tr>";};mysql_close($conn);?></table>

Document

Edited by macman
Link to comment
Share on other sites

so what is happening now, still a blank page?you should be testing for connection errors, like in this example from the tutorialshttp://www.w3schools.com/php/php_mysql_connect.asp

$rs = mysql_query($sql,$conn);if(!$rs){  echo "there was a problem with the query";};

Edited by thescientist
Link to comment
Share on other sites

so what is happening now, still a blank page?you should be testing for connection errors, like in this example from the tutorialshttp://www.w3schools.com/php/php_mysql_connect.asp
$rs = mysql_query($sql,$conn);if(!$rs){  echo "there was a problem with the query";};

Okay with the code in the most recent post the error is the unexpects $end the code in the 2nd last post produced the blank page and still does i have added an error check in there on both versions of my pageusing the code you posted i am getting PHP Parse error: syntax error, unexpected $end in Page3.php on line 53 on the one above your last postand still a blank page on the version 2 above.at one stgae i at least had the html form showing now none of it is and i want to have it hsow then enter the value into the associated field target itself and produce the table which will need extra items added in the query though i only want to compare to the three which match the inputsThe Name should go to another page "Hyperlink" i think the get method to create a url which would run and list bookingNUM,CustNUM etc fro the tables which you can see in the document i linked to hopefully Thanks for your help Edited by macman
Link to comment
Share on other sites

From your post I couldn't really make out exactly what you tried or what wasn't working. I'm not sure where $end is coming from? can you please show your updated code after making any changes, and please indicate which lines the errors messages are indicating, by commenting them in the code? It would also be helpful to stick with one version of the code. If you want to keep a backup of your original somewhere that's fine, but it will be a lot easier for people helping you if you keep the focus to one version of the code at a time.

Edited by thescientist
Link to comment
Share on other sites

Okay i will stick with 1 version for the time being then to claify for you the objectivesare the page is to act as a simplified search facility allowing the staff member to search for a customer booking based upon the customer’s family name, or phone number, or vehicle registration number. The staff member will need to enter the detail in a form. When the form’s submit button is clicked the page must submit If the input passes validation which is che the page should generate a list of possible service bookings for the customer. The staff member will then be able to choose one of these bookings from the list. The details of the chosen booking will then be displayed on the Booking Detail page.To me if a match is found on the criertia generate a list of reords ie the table which is echoed though if i select the customer which matches it is to redirect to another page which will show service history from the service table as custNum is a filed in each table ideally i am thinking maybe using a get to append the Custnum to the url and produce the additional selection items where $Custnum_$GET ["custNum"] = service.Custnum or something similar.using the code below

<?phperror_reporting(E_ALL);require_once("nocache.php"); // Stop information being cachedif(isset($_POST['submit'])){  $Family = $_POST["familyName"];  $Phone = $_POST["contact"];  $Rego  = $_POST["rego"];$conn = mysql_connect("localhost", "twastudent", "prac3");  mysql_select_db('autoservice', $conn); // Logins to Database // Connect to database  $sql = "SELECT customer.familyName,customer.contact, FROM customer where customer.familyName = $Family OR customer.contact =    $Phone"; $rs = mysql_query($sql,$conn);if(!$rs){  echo "there was a problem with the query";};?><html><head><title> Find Booking Form </title></head><body><form id = "CustomerBooking" method = "post" action="<?php echo $_SERVER["PHP_SELF"];?>"><label for = "familyName"> Customer Family Name </label> <input type = "text" name = "familyName" id = "familyName"></input><label for = "contact" > Customer Phone Number </label> <input type = "text" name = "contact" id = "contact"> </input><label for = "rego" > Vehicle Registration Number </label> <input type = "text" name = "rego" id = "rego"></input><label for = "Submit">Submit </label> <input type = "submit" id = "Submit"> </input><label for = "reset" > Reset </label> <input type = "reset" id = "reset"> </input></form><table border = '1'><th> Customer Family Name </th> <th> Customer First Name </th> <th> Customer Suburb </th> <th> Customer Phone</th> <th> Vehicle Registraion Number </th> <th> Date Of Service Booking </th><?phpwhile ($row = mysql_fetch_array($rs)){  echo "<tr>";  echo "<td>" . $row["familyName"] . "</td>";  echo "<td>" . $row["contact"] . "</td>";  echo "</tr>";};mysql_close($conn);?></table></html>  // Otherwise the Last Line According to my Texteditor

The error is PHP Parse error: syntax error, unexpected $end Page 6.php on line 55 Which i would think is caused by my loop not being closed correctly Which also means the form is not displaying at all which is the prupose of the page to take the user input from the form if it matches an entry in the database ie familyname matches family name in the customer database return a list of records where the value matches.The list of bookings that is displayed on this page should show the customer’s family name, first name, suburb, phone number, vehicle registration number, date of service booking. This list should be displayed in alphabetic order of the family name and then by the date of service booking. Given that the staff member will need to choose one of these bookings from the list they will need to be displayed as input devices in an appropriate form or as a series of hypertext links. annoyingly for me i had the form displaying in my original though my queries were not correct and i had a looping issue there i can post it if it helps.Thanks agin for your help i will have to sleep as it is late if you can solve it great hopefully the documents i have supplied give an indication of what i am looking for

Edited by macman
Link to comment
Share on other sites

is page6.php what you are showing us? Which one is line 55? It's hard to tell for us since we aren't viewing it through your text editor which probably has line numbers. Are you putting the $ before end, because if not, then $end would appear to be a variable....It's still also good practice to check for connection errors on the mysql_select_db line like in that link I posted.

Link to comment
Share on other sites

is page6.php what you are showing us? Which one is line 55? It's hard to tell for us since we aren't viewing it through your text editor which probably has line numbers. Are you putting the $ before end, because if not, then $end would appear to be a variable....It's still also good practice to check for connection errors on the mysql_select_db line like in that link I posted.
Line 55 is the Last one ie the </html> tag it is the version of the code that i posted I can add back in the some more error checking i have the code for that.and i am not adding it it is the error produced in the browser it is a copy and paste of that $end is not a variable as it is never defined from what i can tell it is caused when a php loop is not closed correctly thanks for yourreply Edited by macman
Link to comment
Share on other sites

Okay I ma closer i moved the elemnets around ie i moved the form and am getting the error message that the query is wrong error message there was a problem with the query PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in page6.php at the where loop which i think would be caused due to the query ailing an not producing a record set i will change the query a little and see

Link to comment
Share on other sites

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in page6.php at the where loop which i think would be caused due to the query ailing an not producing a record set i will change the query a little and see
that means there's something wrong with your query, which means there's probably something wrong with your connection. Which is why I've been pushing you to add error handling to that part of the script with the links I keep posting. If your connection and query were correct you would be getting a resource. If it fails, you will get a return value of FALSE, which is what the error message is telling you. (FALSE is a boolean).
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...