WHy cant I insert
Moderator: General Moderators
WHy cant I insert
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
?>
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
?>
If that is the entire code, try this :
Code: Select all
if ($_POSTї'submit'] == 'Enter Information') {
// process form
$dbh=mysql_connect ("localhost", "xxx", "yyyy");
mysql_select_db ("affusi2_Comments",$db);
$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
?>Parse error: parse error in /home/affusi2/public_html/testms
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
<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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try changing the following lines:
to
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
Code: Select all
$sql = "INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES ('".$_POST['BY']."','".$_POST['EMAIL'].','".$_POST['MESSAGE']."')";
$result = mysql_query($sql);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>');Mac
Still does not work
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
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
trywhat's the error message it displays?
Code: Select all
$result = mysql_query($sql) or die($sql. ' :'. mysql_error());nothing shows up
Nothing shows up when I add the code to trap the error.
I have an error
INSERT INTO Comment_person (BY,EMAIL,MESSAGE) VALUES('','','') :You have an error in your SQL syntax near 'BY,EMAIL,MESSAGE) VALUES('','','')' at line 1
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
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']."')";- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK