psquillace Posted April 10, 2008 Share Posted April 10, 2008 Hello all, I have a form that when filled out, writes to a mysql database. One of these form fields is called Company_Name and I wanted to add and if else statement in my php code so that if Company_Name is filled in, in the form, it writes something else in a different part of the db. For example, if someone fills in Company_Name I have it capturing the company name the person fills out in the db. At the same time, I need an if else to write to a different part of the db if it is filled in or not. So far I have this but I do not think I am doing it right, Can someone tell me if this syntax is right or not? $query .= mysql_escape_string($First_Name) . "', '"; $query .= mysql_escape_string($Last_Name) . "', '"; $query .= mysql_escape_string($Company_Name) . "', '"; if ($_POST($Company_Name) { $query .= "ORG', '"; //Write ORG if Filled In } else { $query .= "IND', '"; //Write IND if Not } $query .= mysql_escape_string($Address_Line_1) . "', '"; $query .= mysql_escape_string($City) . "', '"; $query .= mysql_escape_string($State) . "', '"; $query .= mysql_escape_string($Postal_Code) . "', '"; $query .= mysql_escape_string($Country) . "', '"; $query .= mysql_escape_string($Custom_Field_1) . "', '"; $query .= mysql_escape_string($Custom_Field_2) . "', '"; Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 that doesnt' look like any query i have ever seen cause you didn't show anything also process all your variables outside the query and make 1 nice query string instead of that ugly connotation method Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 well, I only posted the part of the query that I inserted in there to show what I was trying to do. Here is the whole query so you can see what I did //Define the Query $queryx = "SELECT email FROM customer_table WHERE email='$email' LIMIT 1"; // Check if the email exists in the table $resultx = mysql_query($queryx); if (mysql_num_rows($resultx) > 0) { $message = '<p><font color="red">Please use a different email address.</font></p>'; } else { $query = "INSERT INTO customer_table "; // Insert into table 'customer_table' $query .= "(First_Name, Last_Name, Company_Name, Address_Line_1, City, State, Postal_Code, Country, Custom_Field_1, Custom_Field_2, Custom_Field_3, Optin, email, List_Array, Parent, Date) "; $query .= "VALUES ('"; $query .= mysql_escape_string($First_Name) . "', '"; $query .= mysql_escape_string($Last_Name) . "', '"; $query .= mysql_escape_string($Company_Name) . "', '"; if ($_POST($Company_Name) { $query .= "ORG', '"; //Write ORG if Filled In } else { $query .= "IND', '"; //Write IND if Not } $query .= mysql_escape_string($Address_Line_1) . "', '"; $query .= mysql_escape_string($City) . "', '"; $query .= mysql_escape_string($State) . "', '"; $query .= mysql_escape_string($Postal_Code) . "', '"; $query .= mysql_escape_string($Country) . "', '"; $query .= mysql_escape_string($Custom_Field_1) . "', '"; $query .= mysql_escape_string($Custom_Field_2) . "', '"; $query .= mysql_escape_string($Custom_Field_3) . "', '"; if ($Optin) { $query .= "1', '"; // Write a 1 if opted in } else { $query .= "0', '"; // Write a 0 if opted out } $query .= mysql_escape_string($email) . "', '"; // email is a unique field; the same email address will not be written to the table $query .= mysql_escape_string($trial) . "', '"; $query .= mysql_escape_string($parentsite) . "', '"; $query .= mysql_escape_string($today) . "');"; //Execute the Query $result = mysql_query($query) or die(mysql_error()); // Run the query. if ($result) { $message = '<p><font color="red">Your request has been added to our records.</font></p>'; } else { $message = '<p><font color="red">There was a problem writing your request to our records.</font></p>'; // $message .= $query; for displaying the query for testing purposes } } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 are u using registered_globals on??? Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 Not that I am aware of, No. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 Not that I am aware of, No. and the vars you are using come from where? Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 when you say vars I am assuming you mean the variables? Sorry, I am a little new to php and mysql. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 yes u don't define $First_name and so forth Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 well, those are defined in another part of the script. I thought it would easier to just show the area I was having trouble with. Should I post the whole page here? It is quite lenthy. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 no your methodologly seems primitive and i'm trying to see if are doing something that is counter active for a "good" db strucutre Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 well, like I said, I am new to this so expect more primitive entries. LOL. I just did not know how to get an entry in another part of my db when the exception was a form field filled in. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 if your goal is to store data that is multiple types such as Client Type you should store them in mysql as ENUM or Integers Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 not as TEXT as I have it in my db right now. I need to know if the format that I laid out in my script is correct or did I write it incorrectly. thanks for your help, Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 not as TEXT as I have it in my db right now. I need to know if the format that I laid out in my script is correct or did I write it incorrectly. thanks for your help, did u even try it or are u wanting to be the great cranack here and predict what will happen? Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 LOL.... No I will give it a whirl now and see. I was getting a white screen with no errors with that code that I put in there so I was not sure what I was doing wrong with that. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 white screen + no error = you are supressing errors and going to a fatal error check your error reporting config. There are errors you just can't see them Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 well, I changed the code a bit to read this instead $query .= mysql_escape_string($Company_Name) . "', '"; if (isset($_POST($Company_Name))) { $query .= "ORG', '"; //Write ORG if Filled In } else { $query .= "IND', '"; //Write IND if Not } but I still get the white screen. I have this code before anything else in my php script ini_set ('display_errors', 'On'); error_reporting (E_ALL); so I don't know why I would get no errors at all. Any thoughts on this, Quote Link to comment Share on other sites More sharing options...
psquillace Posted April 10, 2008 Author Share Posted April 10, 2008 OK, I got it to go into the db with this code here $query .= mysql_escape_string($Company_Name) . "', '"; if (isset($_POST['$Company_Name'])) { $query .= "ORG', '"; //Write ORG if Filled In } else { $query .= "IND', '"; //Write IND if Not } I was using () when I should have used [], sheesh, I guess it is all in the learning process. Ok, but I have a new issue I am hoping someone here can help. It is not going into the right field in the db. It is just pushing all the other information forward and mis-alligning it all. what do I have to put in my code to get it to query into the right field is my question? Thanks for any help on this, Paul Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.