Hi, I'm having trouble with writing this statement as an INSERT function instead of UPDATE.

$sql = " UPDATE tbl_category 
	SET " . ($cat_parent_id == 0 ? "sub_cat_name = '$name'," : "cat_name = '$name',") . 
	"cat_description = '$description', cat_image = $catImage
	WHERE cat_id = $catId";

Sorry if this is a noob question I'm still quite new with MySQL.
Thank you for any help.

Hi, I'm having trouble with writing this statement as an INSERT function instead of UPDATE.

$sql = " UPDATE tbl_category 
	SET " . ($cat_parent_id == 0 ? "sub_cat_name = '$name'," : "cat_name = '$name',") . 
	"cat_description = '$description', cat_image = $catImage
	WHERE cat_id = $catId";

Sorry if this is a noob question I'm still quite new with MySQL.
Thank you for any help.

Could we see the syntax? Right off the top of my head, I know it goes INSERT INTO table VALUES (field1, field2, field3, etc...) after connecting to server. What error are you getting from php?

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 ')' at line 2

That's all I get.

. ($cat_parent_id == 0 ? "sub_cat_name = '$name'," : "cat_name = '$name',") .

That is the part that I cant get to work in it. I'm not sure how I needs to be written inside of the SQL statement.

$sql = " UPDATE tbl_category
SET " . ($cat_parent_id == 0 ? "sub_cat_name = '$name'," : "cat_name = '$name',") .
"cat_description = '$description', cat_image = $catImage
WHERE cat_id = $catId";

insert into tablename (`$cat_parent_id`, `sub_cat_name`, `cat_name`, soo on..) values ('0', '$name', '$name')
. ($cat_parent_id == 0 ? "sub_cat_name = '$name'," : "cat_name = '$name',") .

That is the part that I cant get to work in it. I'm not sure how I needs to be written inside of the SQL statement.

Try:

(($cat_parent_id == 0) ? "sub_cat_name = '$name'," : "cat_name = '$name',")

If it still doesn't work, echo the query after you built it, so maybe you can determine where it goes wrong.

what are those : and ? marks doing in there?

what are those : and ? marks doing in there?

That's called the ternary operator.

$somevar = (condition) ? (true case) : (false case);
// so
$test = true;
$somevar = $test ? "Hello" : "Goodbye";

// is the same as
$test = true;
if ($test) {
  $somevar = "Hello";
} else {
  $somevar = "Goodbye";
}

the first post doesnt make sense and doesnt go along what the other people areposting. the op is asking for help with making an update an insert those are two diferent thigns you people are posting update not insert

i have no idea if this can be done in php but try using an anonymous block and then test the input variables if not create a stored procedure in the database that accepts whatever you are trying to test. i know this can be done with asp so i'm assuming it can be done php as well.

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.