I have a method which insert a record into a table as shown below:
public static function AddCategory($categoryName)
{
$sql = 'INSERT INTO category(name)
开发者_如何学JAVA VALUES(' . $category_name .')';
$parameters = array('category_name' => $categoryName);
return DB::Execute($sql,$parameters);
}
Can anyone tell me if the SQL syntax is correct? Im not being able to insert any record..I can't find my mistake. Thanks
public static function AddCategory($categoryName) {
$sql = 'INSERT INTO category (name)
VALUES (:category_name)';
$parameters = array('category_name' => $categoryName);
return DB::Execute($sql,$parameters);
}
From my brief observation this line is redundant:
$parameters = array('category_name' => $categoryName);
You have already specified the inserted value inside query itself. You need only to execute it now.
I don't exactly understand what you want to do, a valid SQL INSERT sintax is:
INSERT INTO table1 VALUES (value1, value2, ...)
so i suppose your code should be changed into something like:
public static function AddCategory($categoryName)
{
//I don't know the real name of the TABLE you want to update, so you have to replace CATEGORIES_TABLE with your table name
$sql = 'INSERT INTO CATEGORIES_TABLE VALUES(' . $categoryName .')';
return DB::Execute($sql);
}
I don't know what DB::Execute does, I suppose it executes a query.
FYI a good way to test this is to add the following line before exectuing the query:
echo "<br>" . $sql . "</br>";
Then you take the result printed by PHP and run it on MySQL query browser, it will tell you more accurate details about why the query fails.
精彩评论