WHy cant I insert

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
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

WHy cant I insert

Post 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
?>
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

is that the exact code?
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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 
?>
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

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

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

Still does not work

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$result = mysql_query($sql) or die($sql. ' :'. mysql_error());
what's the error message it displays?
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

Post by cbompart »

WHere do I locate the error
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

nothing shows up

Post by cbompart »

Nothing shows up when I add the code to trap the error.
cbompart
Forum Newbie
Posts: 9
Joined: Mon Feb 10, 2003 5:47 pm

I have an error

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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']."')";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This is probably of interest:
http://www.mysql.com/doc/en/Reserved_words.html

Mac
Post Reply