0

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.

Edited by specialk423: n/a

6
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by baki100
0

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?

Edited by lukethedrifter: n/a

0

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.

0
. ($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.

-1

$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')
0
. ($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.

Edited by pritaeas: n/a

0

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";
}

Edited by ShawnCplus: n/a

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.