Jump to content

Trying to access a database.


Dtp1337

Recommended Posts

I've been working at this provided that the user/pass are removed. I need to validate a form processing it to where it displays the city and state in the MySQL database. Can anyone help me piece this in order. I think I have 2 copies of what I have but I do not understand how to get it to say if the city is entered and the state is not, process city and request state. Likewise for state and when both are input, do both, Otherwise error and redo. Also its using PDO passing.Heres the 2 horrible ideas I've had so far. Can anyone please help me piece this thing together correctly.

Lab12.php

Edited by Dtp1337
Link to comment
Share on other sites

Your second code isn't going to work because you're trying to run queries before you've connected. It's also not going to work to use isset to check the variables, they will always be set. You're either setting them to a value from $_GET or an empty string, either way they get set. If you want to check if they're empty you can either use the empty function or just compare them against an empty string. I'm not quite sure what your requirements are, but you may need a structure like this to pick the query to run:

if ($city != '' && $state != ''){  // both are given}elseif ($city != ''){  // only city is given}elseif ($state != ''){  // only state is given}else{  // neither are given}

  • Like 1
Link to comment
Share on other sites

I think you're missing the point. The if/else structure needs to run a query or do some other work, all you're doing is setting various variables. I wouldn't put all of the strlen checking in that if structure, you should use the previous code you were using to get the values from $_GET and validate them. After that code runs then $city and $state will either be set to the value from $_GET, or an empty string. Then you use the if structure I posted to do something different based on what they filled out. If they gave values for both, then run a query that searches or both. If they only gave a value for state, then run a query that only uses state. If they only give a value for city, then run a query that only uses city. If they didn't give values for either then show an error message. Your code is still always running the same query regardless of which ones they filled in. There needs to be more than one query that it decides to run based on what they entered.

  • Like 1
Link to comment
Share on other sites

That's closer, yeah. You can move the 3 while loops out of the if structure though. Those loops are all the same, so you can move them outside the if structure and run the same loop regardless of which query you ran (you'll need a true/false variable that you set in the else in case there was an error and you don't want to run the while loop). Other than that, the header redirect seems odd. It seems like you're redirecting if they filled out values. Also, instead of "Y" and "y" or "N" and "n", most people use true and false for that kind of thing. The other thing is the validation part of your code. The value of $errors will always be the value when you validated the state, whatever you set for $errors when you validated the city gets overridden when you validate the state.

Link to comment
Share on other sites

Exit stops the entire script. Just have a variable that you set to true, in the else block if there was an error then set that variable to false, and after the if/else structure check if that variable is true to run the while loop. It will be true unless it hit the else and you never ran a query. For the validation, I'm not sure you need to set all of those variables. First you just need to get the values:

$city1= $_GET["City1"];if (strlen($city1)<=0 or strlen($city1)>=20) {  $city1error = '*';  $city1='';}$state1= $_GET["State1"];if (strlen($state1)<=2 or strlen($state1)>=20) {  $state1error = '*';  $state1='';}

For the other variables, if you want to know if there was an error then you can set that variable in the else block (the same variable you can use to determine whether or not to run the while loop). Also, it looks like you're checking the wrong variables in the if/else structure.

  • Like 1
Link to comment
Share on other sites

Notice: Undefined index: City1 in F:\xampp\htdocs\Lab12.php on line 16 Notice: Undefined index: State1 in F:\xampp\htdocs\Lab12.php on line 17Can't connect to MySQL Server. SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. Fixed the variables lol, and also corrected a couple of otther things I caught in the header and stuff. Does the first if{} if else{} work as for setting those check variables? As for the while loop where would I need to put that variable check?

Lab12.php

Edited by Dtp1337
Link to comment
Share on other sites

The notices mean there was nothing in $_GET. You can use isset to check for that first. $city1 = isset($_GET["City1"]) ? $_GET['City1'] : ''; That's a shorthand if/else to set to to the value from $_GET if it's there, or else an empty string. The database error just means that the database refused your connection. You put the part where you get the variables in an if/else structure. That means it's only going to validate $state1 if $city1 had a value. If $city1 is blank it will not validate $state1. Maybe that's the way you want it to work, but just so you're aware.

  • Like 1
Link to comment
Share on other sites

On the database connection... I used other previous working programs I wrote and now I can't seem to access the database. They are now providing that I can't use them either... Does that mean my account holding my user/pass is removed? Nevermind I'm down to a problem with my loop and I guess those variables you mentioned.

Lab12.php

Edited by Dtp1337
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
×
×
  • Create New...