i am trying to connect a form to mysql database by using php code,but once a click submit button to submit the form this is the error am getting.

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by) VALUES ('','','')' at line 1


please help to troubleshoot this.

Can I see the full codes?

<?php
session_start();
$con = mysql_connect("localhost","root","");
if (!$con)
{
die ('could not connect: '.mysql_error());
}

mysql_select_db("scanad_kenya", $con);

$topic= $_POST;
$bodyn= $_POST;
$by= $_POST;


$sql="INSERT INTO news_feed(topic,bodyn,by) VALUES ('$topic','$bodyn','$by')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "your comment sucessfully added";
header("location:homenews.php");
mysql_close($con)
?>

the above is the code that is connecting to the database

Try this

<?php
session_start();
$con = mysql_connect("localhost","root","");
if (!$con)
{
die ('could not connect: '.mysql_error());
}

mysql_select_db("scanad_kenya", $con);

$topic= $_POST['topic'];
$bodyn= $_POST['bodyn'];
$by= $_POST['by'];


$sql="INSERT INTO news_feed VALUES ('$topic','$bodyn','$by')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "your comment sucessfully added";
header("location:homenews.php");
mysql_close($con)
?>

thanks alot well the previous error is gone but the following error results.

Error: Column count doesn't match value count at row 1

maybe you should check your field on the table

thanks alot for your concern the error was in the naming of columns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.