Page 1 of 1

This is too simple not to work

Posted: Sun Feb 07, 2010 11:43 am
by fried
I have a flash form that sends 2 pieces of information Email address and Nationality. This is sent to Email.php, where the following code is used:-

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
</body>
<?php
    include 'loginphone.php';
    include  'clean function.php';
    
//connect
 
    $connection= mysql_connect($host,$user,$pass);
    if (!$connection){
        die ("Could not connect : ". mysql_error());
    }
 
//select
 
    $db_select=mysql_select_db($data);
    if (!$db_select){
        die ("could not select database". mysql_error());
    }
 
//clean
    
        $identite= $_POST["identite"];
        $mail= $_POST["mail"]);
        
    
    
//insert
 
$query = "INSERT INTO newletter VALUES (NULL,'".$mail."','".$identite."')";
$result = mysql_query($query) or die(mysql_error());
    
?>
 
</html>
This succesfully enters the NULL value into the database, however the $mail and $identite variable appear as blank in the database, I'm not sure what this means or exactly where the problem lies.

Here is my database structure:
Table structure for table newletter
Field
Type
Null
Default
Comments
MIME type
Id_client
int(11)
No



E-mail
varchar(40)
Yes
NULL


Identite
varchar(10)
Yes
NULL

Thanks in advance, it's been a while since I did any programming so I probably missed something obvious.

Re: This is too simple not to work

Posted: Sun Feb 07, 2010 12:10 pm
by limitdesigns
Check out proper syntax for the INSERT statement. It should be:

Code: Select all

 
"INSERT INTO table (column1, column2, column2) VALUES('$column1', '$column2', '$column3')"
 

Re: This is too simple not to work

Posted: Sun Feb 07, 2010 2:45 pm
by fried
I changed the code to this

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
</body>
<?php
    error_reporting(E_ERROR|E_WARNING);
    include 'loginphone.php';
    include  'clean function.php';
    
//connect
 
    $connection= mysql_connect($host,$user,$pass);
    if (!$connection){
        die ("Could not connect : ". mysql_error());
    }
 
//select
 
    $db_select=mysql_select_db($data);
    if (!$db_select){
        die ("could not select database". mysql_error());
    }
 
//clean
    if (isset ($_POST['identite'])){
        $identite= $_POST['identite'];
    }
    if (isset ($_POST['mail'])){
        $mail= $_POST['mail']);
    }
        
    
    
//insert
 
$query = "INSERT INTO `newletter`(`Id_client`,`E-mail`,`Identite`) VALUES (NULL,'".$mail."','".$identite."')";
$result = mysql_query($query) or die(mysql_error());
    
?>
 
</html>
Which works on another application but now it doesn't even enter the NULL. Can't see where I'm going. Thanks for your help.

Re: This is too simple not to work

Posted: Sat Feb 13, 2010 9:56 pm
by brentwientjes
Seems like id_client is an int and e-mail, identite are varchar so:

$query = "INSERT INTO `newletter`(`Id_client`,`E-mail`,`Identite`) VALUES (NULL,'".$mail."','".$identite."')";

should be:

$query = "INSERT INTO newletter (Id_client, E-mail, Identite) VALUES (NULL, \"$mail\", \"$identite"\)";

This is at least how all my sql code is (and works).

Good luck.

Re: This is too simple not to work

Posted: Sat Feb 13, 2010 10:31 pm
by requinix
limitdesigns wrote:Check out proper syntax for the INSERT statement. It should be:

Code: Select all

 
"INSERT INTO table (column1, column2, column2) VALUES('$column1', '$column2', '$column3')"
 
OP's original syntax was fine. The list of columns is not necessary if the VALUES list has a value for each one in the right order.
brentwientjes wrote:Seems like id_client is an int and e-mail, identite are varchar so:

$query = "INSERT INTO `newletter`(`Id_client`,`E-mail`,`Identite`) VALUES (NULL,'".$mail."','".$identite."')";

should be:

$query = "INSERT INTO newletter (Id_client, E-mail, Identite) VALUES (NULL, \"$mail\", \"$identite"\)";

This is at least how all my sql code is (and works).

Good luck.
Unless $mail or $identite have unquoted apostrophes, changing the quote style won't make a difference.
And removing the backticks actually breaks the query.
fried wrote:Which works on another application but now it doesn't even enter the NULL. Can't see where I'm going.
Id_client cannot be null. Says so right in the table definition.

Re: This is too simple not to work

Posted: Sun Feb 14, 2010 2:33 pm
by brentwientjes
:D Thanks tasairis for the correction to my suggestion. I have about 40K lines of php code with about 2-3 K lines of sql over the past 5 years running over 3 differrent hosting services. They all work correctly with the suggestion I gave. They always fail if I have a var and forget the \" around the variable. I tried a more basic test and found your correction also works. The books I learned from did not use your suggested syntax but I see in this forum all the posted I have read today and yesterday (about 30 posts) (I just joined yesterday) use this syntax. I wonder if there is any performance difference in php excecution or if the interpreter just accepts multiple syntax?

Again, thanks for the update. In my attempt to help someone else, I also learned something new.