Jump to content

if ($_POST( into mysql db question


psquillace

Recommended Posts

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) . "', '";

Link to comment
https://forums.phpfreaks.com/topic/100524-if-_post-into-mysql-db-question/
Share on other sites

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

}

}

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?

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,

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.