Question:
can anyone please help me on PHP and MySQL?
persiankillers
2009-07-05 17:24:43 UTC
can anyone please help me on PHP and MySQL?

Have the following form with php coding, trying to store the data from the form into MySQL database, but it doesn't store it , any idea please help me,
appreciate it.
thanks

######################################...

please leave me your email so i can post you the whole coding, please please help me..

thanks
Four answers:
anonymous
2009-07-05 23:23:28 UTC
Hm....



















I think u can get POST raw with:

foreach($_POST as $key=>$value) $$key=$value;



And get POST parameter with isset():

if(isset($name) && isset(email) && isset(ALL_POST_ACCEPTED)){

... save database here ...

}
just "JR"
2009-07-06 02:20:06 UTC
Lot of mistakes in that code! (AND in the answers above as well!)

"

if(!empty($_POST['name']) and !empty($_POST['email']) and !empty($_POST['hotel']) and

!empty($_POST['interpreter_gender']) and !empty($_POST['date_request']) and !empty($_POST['comment']))

"

Get the $_POST variables into php variables with the same name (you will re-use them later:

$name = $_POST['name'];

$email = $_POST['email'];

$hotel = $_POST['hotel'];

$interpreter_gender = $_POST['interpreter_gender'];

$date_request = $_POST['date_request'];

$comment = $_POST['comment'];

... ADD here the OTHER $_POST values you will use in the next table (like "first-name" etc...)



Your test for empty: DON'T use "empty()".

if ( ($name != "") && ($hotel != "") && .... )

{

// now you can insert... OOPS! Your syntax is wrong.

// in Php, variables enclosed in SINGLE quotes are NOT parsed!

// in SQL, tables and field names must be eclosed in REVERSE SINGLE QUOTES. Values must be enclosed in SINGLE QUOTES.



$sql = "INSERT INTO `translator` (`name`, `email`, `hotel`, `interpreter_gender`, `date_request`, `comment`)

VALUES ('".$name."', '".$email."', '".$hotel."', '".$interpreter_gender."', '".$date_request."', '".$comment."')";

mysql_query($sql) or die (mysql_error());

}

(You also have a logic error: IF fields are not empty, $sql = "something;, then you are out of the IF, and do your mysql_query!

IF any field is empty, you do NOT define $sql, BUT you STILL call the query!!!)

NEXT: you record the booking:

"

$sql = "INSERT INTO booking ('first-name', 'surname', 'gender', 'birthday', 'country-city', 'contact-phone-number', 'email-address', 'select-hotel', 'check-in-date', 'check-out-date', 'no-of-night', 'no-of-room', 'no-of-adult'. 'no-of-children', 'occupancy', 'comment') VALUES ('$firstname', '$surname', '$gender', '$birthday', '$countrycity', '$contactphonenumber', '$emailaddress', '$selecthotel', '$checkindate', '$checkoutdate', '$noofroom', '$noofadult', '$noofchildren', '$occupancy', '$comment')";

"

1. replace your single quotes the way explained above

`tablename` - `fieldname` - ' " . $value . " '

2. Make sure you have extracted your edata from the $_POST (your code does not show that).

3. Rename your table fields: do NOT use "-", use "_"

'first-name' => first_name. This will avoid you to have confusions or parse error, later on: the "-" can be taken as a MINUS:

"update `table` set `score`=`score`-".CONSTANT." where ... "

Here, score is decremented by one. If you forgot your quotes, this would look like

score = score-constant => parse error!

Last note:

You store the value "$comment" in both tables, but I believe they MIGHT be two different variables!

If they are, call them comment1 and comment2 in your form.

Good luck!
jef.r.sun
2009-07-05 17:59:02 UTC
I see a couple of big problems here.



First of all you are using variables in your query that you most likely didn't set in the first place. Examples: $name, $email, $hotel, ...

By looking at the if clause above the first query, I can tell that you are referencing POST variables and trying to use those in your query.

Unless you have register_globals enabled in your PHP configuration (either in the php.ini or via .htaccess if it is allowed) the variable names I mentioned above (not the $_POST ones) are not set and therefore have no value in them.

You can check if register_globals is activated by calling phpinfo();.



HOWEVER (second problem), if register_globals should be active (and generally when using variables in an SQL query), it is *still* STRONGLY deprecated to use variables just like that without checking for their values. I would *strongly* recommend at least escaping their values instead if you shouldn't feel the need to sanitize them properly (i.e. check if numeric variables indeed hold valid numeric values in predefined ranges, check string lengths, check if a multiple choice value really is one of the given options etc.).

After you have sanitized them, you can include them in the MySQL query.

PHP 4.3.0 and higher has a very nice function for that: mysql_real_escape_string ().



On a side note, register_globals should default to being disabled. For future reference: never assume it is enabled - that "feature" will be removed in PHP 6.0 anyway.
anonymous
2009-07-05 17:32:11 UTC
No need to post the whole thing, just post the lines that connect to the database and insert or update the record.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...