Page 1 of 1

WHy cant I insert

Posted: Mon Feb 10, 2003 5:47 pm
by cbompart
I am a newbie to PHP. WHy can't I insert I am running on version 4.2.3 of PHP a sample of the code is listed. Any assistance is appreciated

if ($submit) {

// process form

$dbh=mysql_connect ("localhost", "xxx", "yyyy");
mysql_select_db ("affusi2_Comments",$db);
$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES ('$BY','$EMAIL','MESSAGE')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";


} else{

// display form

?>
<form method="$post" action="<?php echo $PHP_SELF?>">
BY:<input type="Text" name="BY"><br>
EMAIL:<input type="Text" name="EMAIL"><br>
MESSAGE:<input type="Text" name="MESSAGE"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>

Posted: Mon Feb 10, 2003 5:50 pm
by Zoram
is that the exact code?

Posted: Mon Feb 10, 2003 6:02 pm
by Zoram
If that is the entire code, try this :

Code: Select all

if ($_POST&#1111;'submit'] == 'Enter Information') &#123; 

// process form 

$dbh=mysql_connect ("localhost", "xxx", "yyyy"); 
mysql_select_db ("affusi2_Comments",$db); 
$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES ('".$_POST&#1111;'BY']."','".$_POST&#1111;'EMAIL'].','".$_POST&#1111;'MESSAGE']."')"; 
$result = mysql_query($sql); 
echo "Thank you! Information entered.\n"; 


&#125; else&#123; 

// display form 

?> 
<form method="post" action="<?php echo $PHP_SELF?>"> 
BY:<input type="Text" name="BY"><br> 
EMAIL:<input type="Text" name="EMAIL"><br> 
MESSAGE:<input type="Text" name="MESSAGE"><br> 
<input type="Submit" name="submit" value="Enter information"> 
</form> 
<?php 
&#125; // end if 
?>

Parse error: parse error in /home/affusi2/public_html/testms

Posted: Mon Feb 10, 2003 10:09 pm
by cbompart
thanks.. I tried your option..completed code below:

<html>

<body bgcolor="#000000" text="#FFFFFF">
<font face="Arial, Helvetica, sans-serif" size="1">
<?php
if ($_POST['submit'] == 'Enter Information') {

// process form

$dbh=mysql_connect ("localhost", "affusi2", "fossil");
mysql_select_db ("affusi2_Comments");
$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES ('".$_POST['BY']."','".$_POST['EMAIL'].','".$_POST['MESSAGE']."')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";

} else{

// display form

?>
<form method="post" action="<?php echo $PHP_SELF?>">
BY:<input type="Text" name="BY"><br>
EMAIL:<input type="Text" name="EMAIL"><br>
MESSAGE:<input type="Text" name="MESSAGE"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if

?>
</font>
</body>
</html>

but i get this error;

Parse error: parse error in /home/affusi2/public_html/testmsql8.php3 on line 12

Posted: Tue Feb 11, 2003 2:42 am
by twigletmac
Try changing the following lines:

Code: Select all

$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES ('".$_POST['BY']."','".$_POST['EMAIL'].','".$_POST['MESSAGE']."')"; 
$result = mysql_query($sql);
to

Code: Select all

$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES('".$_POST['BY']."','".$_POST['EMAIL']."','".$_POST['MESSAGE']."')"; 
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
There was a missing double quote within the SQL statement causing the parse error - notice how using the syntax highlighting on the original code $_POST['MESSAGE'] looks different to $_POST['EMAIL'] and $_POST['BY']. The or die() statement with the call to mysql_error() will help if there is something wrong with the SQL statement.

Mac

Still does not work

Posted: Tue Feb 11, 2003 10:06 am
by cbompart
Hi,

Thanks for you input. It did remove the error, but the insert is still not done. could it be because I am not populating the othertwo fields in the database. One is an auto incremental field called "ID" and the other is date and time stamp on record insert.

Thanks again

Posted: Wed Feb 12, 2003 3:15 pm
by volka
try

Code: Select all

$result = mysql_query($sql) or die($sql. ' :'. mysql_error());
what's the error message it displays?

Posted: Wed Feb 12, 2003 3:44 pm
by cbompart
WHere do I locate the error

nothing shows up

Posted: Wed Feb 12, 2003 3:50 pm
by cbompart
Nothing shows up when I add the code to trap the error.

I have an error

Posted: Wed Feb 12, 2003 4:11 pm
by cbompart
INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES('','','') :You have an error in your SQL syntax near 'BY,EMAIL,MESSAGE) VALUES('','','')' at line 1

Posted: Wed Feb 12, 2003 5:17 pm
by volka
ah ;)
BY is a keyword in mysql, e.g. GROUP BY groupId. If you want to use it as fieldname in a query you have to quote it. Try

Code: Select all

$sql = "INSERT INTO Comment_person (`BY`,`EMAIL`,`MESSAGE`) VALUES ('".$_POST['BY']."','".$_POST['EMAIL'].','".$_POST['MESSAGE']."')";

Posted: Thu Feb 13, 2003 2:14 am
by twigletmac
This is probably of interest:
http://www.mysql.com/doc/en/Reserved_words.html

Mac