1

This is probably a dumb question but if i am inserting data using MySQLi and prepared statements.

Is it possible to put in one line different conditions?

For example if text are Active change to Inactive and if text are Inactive change to Active. Is it possible to do in only one line?

$Status = ($_POST['status'] == "Inactive" ? "Inactive" : "Active");
Qirel
  • 25,449
  • 7
  • 45
  • 62
Guif If
  • 535
  • 2
  • 7
  • 18
  • Are you doing anything else in that query, or just toggling the active/inactive? Show your query, please. – Qirel Jul 05 '19 at 20:57

2 Answers2

1

If all you're doing is toggling the status between Active and Inactive, then you can do it in a single query, using CASE WHEN.

UPDATE myTable
SET status = (CASE WHEN status = 'Inactive' THEN 'Active' 
                   ELSE 'Inactive' 
              END)

WHERE id = ?
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

Both of the following should be legal:

$Status = ($_POST['status'] == "Inactive" ? "Inactive" : "Active");
$stmt->bind_param("s", $Status);

 

$stmt->bind_param("s", ($_POST['status'] == "Inactive" ? "Inactive" : "Active"));

WARNING : this 2nd version yields the "Only variables should be passed by reference" strict standards warning. Consider your situation carefully when choosing this approach.


... But note that your expression,

($_POST['status'] == "Inactive" ? "Inactive" : "Active")

will yield Inactive when $_POST['status'] is already Inactive, which is the opposite of the desired behavior you described in your question. I believe you want:

($_POST['status'] == "Inactive" ? "Active" : "Inactive")

EDIT 1

Regarding your comment question, "but, not's possible to put something else for example: if ($_POST['status'] == 'Inactive'){ $Status = ($_POST['status'] == "Inactive" ? "Inactive" : "Active"); else ...", something like that is legal, but doesn't make too much sense. That literally is the same thing as:

if ($_POST['status'] == 'Inactive') {
    if ($_POST['status'] == "Inactive") {
        $Status = "Inactive";
    } else {
        $Status = "Active";
    } else {
        ...
}
landru27
  • 1,654
  • 12
  • 20
  • Can I put this with if else statement for example in the same line? – Guif If Jul 05 '19 at 22:04
  • @GuifIf : if I understand your question correctly, no : PHP will not evaluate an `if ... else` statement down to a value; `bind_param()` (like all functions) is expecting a set of values for its parameter list, so you can only use variables that contain a value or expressions that evaluate to a value – landru27 Jul 05 '19 at 22:08
  • but, not's possible to put something else for example: ```if ($_POST['status'] == 'Inactive'){ $Status = ($_POST['status'] == "Inactive" ? "Inactive" : "Active"); else.```.. – Guif If Jul 05 '19 at 22:13
  • Note that your second snippet would yield warnings - "*Only variables should be passed by reference*" - https://stackoverflow.com/questions/23843383/mysqli-strict-standards-only-variables-should-be-passed-by-reference?rq=1 – Qirel Jul 05 '19 at 22:15
  • @GuifIf : the ternary operator (`condition ? true-result : false-result`) is already a kind of `if ... else` statement; it doesn't make much sense to combine an `if ... else` with a ternary evaluation the way that you have; I will edit my answer to illustrate ... – landru27 Jul 05 '19 at 22:21
  • @Qirel : thanks! I've added a cautionary note to that effect – landru27 Jul 05 '19 at 22:40