This is too simple not to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fried
Forum Newbie
Posts: 16
Joined: Tue Sep 08, 2009 5:43 am

This is too simple not to work

Post 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.
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: This is too simple not to work

Post 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')"
 
fried
Forum Newbie
Posts: 16
Joined: Tue Sep 08, 2009 5:43 am

Re: This is too simple not to work

Post 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.
brentwientjes
Forum Newbie
Posts: 8
Joined: Sat Feb 13, 2010 3:29 am

Re: This is too simple not to work

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: This is too simple not to work

Post 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.
brentwientjes
Forum Newbie
Posts: 8
Joined: Sat Feb 13, 2010 3:29 am

Re: This is too simple not to work

Post 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.
Post Reply