Dtp1337 Posted November 27, 2012 Share Posted November 27, 2012 (edited) 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 November 27, 2012 by Dtp1337 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 27, 2012 Share Posted November 27, 2012 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} 1 Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 Just displaying the City name selected, State selected, and the database for selection of both. Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) I restarted from an example. Is that a little better towards what you described? I also uploaded it to the first post. Edited November 27, 2012 by Dtp1337 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 27, 2012 Share Posted November 27, 2012 I don't see the if/else structure I suggested. It sounds like you want to run one of three possible queries based on what they entered, or else show an error. Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 Yes and where exactly do I need that If statement? I had problems with it in the 1st 2 files I submitted. Of which I did switch them around cause it didn't make sense to me. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 27, 2012 Share Posted November 27, 2012 The if statement checks $city and $state, so put if after you've gotten the values for those. Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 Ok then in the loop would I place my loop there? and additionally for each else if? Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 I'm also getting a unexpected else on line 24? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 27, 2012 Share Posted November 27, 2012 You can run the query inside each if block, and display the results of the query in a single loop after the if structure. I'm also getting a unexpected else on line 24?Sounds like a missing if statement or mixed up brackets. Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 27, 2012 Author Share Posted November 27, 2012 I still don't think im getting it right... Lab12.php Link to comment Share on other sites More sharing options...
justsomeguy Posted November 27, 2012 Share Posted November 27, 2012 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. 1 Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 28, 2012 Author Share Posted November 28, 2012 Is this closer to what your talking about? Lab12.php Link to comment Share on other sites More sharing options...
justsomeguy Posted November 28, 2012 Share Posted November 28, 2012 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 More sharing options...
Dtp1337 Posted November 28, 2012 Author Share Posted November 28, 2012 Could I use "exit" to prevent the loop? And I should place the validation into a If { } elseif stucture? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 28, 2012 Share Posted November 28, 2012 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. 1 Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) 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 November 28, 2012 by Dtp1337 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 28, 2012 Share Posted November 28, 2012 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. 1 Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 29, 2012 Author Share Posted November 29, 2012 (edited) 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 November 29, 2012 by Dtp1337 Link to comment Share on other sites More sharing options...
Dtp1337 Posted November 29, 2012 Author Share Posted November 29, 2012 Still getting an error to my display loop... Lab12.php Link to comment Share on other sites More sharing options...
justsomeguy Posted November 29, 2012 Share Posted November 29, 2012 What error? When you are comparing values in an if statement you need to use == or === to make a comparison, when you use = you are setting a value. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now