I am getting this error here is my code
if(isset($_POST['submit']))
{
$projTit=mysql_escape_string($_POST['projecttitle']);
$projCat=mysql_escape_string($_POST['projectcategory']);
$budget=intval(mysql_escape_string($_POST['budget']));
$description=mysql_escape_string($_POST['editor1']);
$query=sprintf("insert into projects value('%s','%s','%s',%d)",
$projTit,$description,$projCat,$budget);
if (!mysql_query($query)){
die('Error: ' . mysql_error());
}
echo '<p class="record">Your Record has been Added<p>';
}
?>
I have tried to write %d in ''
but still not working.
INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3)
) then you must specify values for all columns : - Interrobang 2012-04-04 06:55
Your table has five columns. However, you are not providing a value for the Project_Id
column. This gives you the error you mentioned.
I understand that you're not providing a value since it's likely a PRIMARY KEY
that autoincrements. To tell MySQL that you are intentionally not passing a value for that column, you should add NULL
as the first value.
value(NULL, '%s','%s','%s',%d)
However, you really should specifically name which columns you are inserting into in case you add a new column at a future date.
INSERT INTO projects (col1, col2, col3, col4) value ('%s','%s','%s',%d)
You should really specify the column names or your app will break if you add a column later, even if it has a useable default value:
INSERT INTO projects
(Project_Title, Project_Description, Project_Category, Project_Budget)
VALUES
(...)
Especially since you have an auto_increment
ID column it makes sense; otherwise you'd always have to specify it as NULL
in your query.
The easiest (but also ugliest) fix would thus be:
$query=sprintf("insert into projects value (null, '%s','%s','%s',%d)",
$projTit,$description,$projCat,$budget);
The good fix would be this:
$query = sprintf("
INSERT INTO projects
(Project_Title, Project_Description, Project_Category, Project_Budget)
VALUES
('%s', '%s', '%s', %d)
", $projTit,$description,$projCat,$budget);
By the way, consider using PDO - then you can use something similar to a format string but don't have to deal with escaping.
try naming the columns you want to fill like
insert into projects (col1, col2, col3) values (...)