Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当参数为空时,设置自己的值php mysql_Php_Mysql - Fatal编程技术网

当参数为空时,设置自己的值php mysql

当参数为空时,设置自己的值php mysql,php,mysql,Php,Mysql,我有一个查询,它将数据插入到我的表中: $stmt = $db->prepare("INSERT INTO reservations (name, start, end,room_id, status, paid, customer, name_ship, equipment, port, ETA, ETD, service_id, service_classification, job, active) VALUES (:name, :start, :en

我有一个查询,它将数据插入到我的表中:
$stmt = $db->prepare("INSERT INTO reservations 
     (name, start, end,room_id, status, paid, customer, name_ship, equipment, port,
         ETA, ETD, service_id, service_classification, job, active) 
VALUES (:name, :start, :end, :room,  :status, 0, :customer, :name_ship, :equipment, :port, 
        :ETA, :ETD, :service_id, :service_classification, :job, 1)");
 $stmt->bindParam(':start', $_POST['start']);
 $stmt->bindParam(':end', $_POST['end']);
 $stmt->bindParam(':name', $_POST['name']);
 $stmt->bindParam(':room', $_POST['room']);
 $stmt->bindParam(':status', $_POST['status']);
 $stmt->bindParam(':customer', $_POST['customer']);
 $stmt->bindParam(':name_ship', $_POST['name_ship']);
 $stmt->bindParam(':equipment', $_POST['equipment']);
 $stmt->bindParam(':port', $_POST['port']);
 $stmt->bindParam(':ETA', $_POST['ETA']);
 $stmt->bindParam(':ETD', $_POST['ETD']);
 $stmt->bindParam(':service_id', $_POST['service_id']);
 $stmt->bindParam(':service_classification', $_POST['service_classification']);
 $stmt->bindParam(':job', $_POST['job']);
 $stmt->execute();

class Result {}

 $response = new Result();
 $response->result = 'OK';
 $response->message = 'Created with id: '.$db->lastInsertId();
 $response->id = $db->lastInsertId();

 header('Content-Type: application/json');
 echo json_encode($response);
?>
我想chceck if:port有一些值,如果没有,则设置静态值。 所以可能在执行之前就应该这样做
if(:port==“”){port='9999';}else{:port}或if(empty(:port){}else{}
我说得对吗?
谢谢

这是您要检查的值:

$_POST['port']
您可以使用条件运算符使其在线计算到所需的值。像这样简单的操作可能就是您想要的:

isset($_POST['port']) ? $_POST['port'] : '9999'
或者,您可以根据需要检查的范围添加更多条件。例如:

(isset($_POST['port']) && !empty($_POST['port'])) ? $_POST['port'] : '9999'
但总体结构是一样的,基本上:

condition ? value_if_true : value_if_false

整个语句的计算结果为结果值,因此可以在线使用。

只需使用三元运算符
?:

$stmt->bindParam(':port',空($\u POST['port'])?'9999':$\u POST['port'])


empty()
如果在
$\u POST
中未设置“port”元素,或者设置为false或0或空字符串,则为true。

@Jens如果insert语句提供显式值,则数据库级默认值将被覆盖。如果我想进行比较,如果:port值大于1000,则保存到Shippyard else to port("港口",("港口")>1000元("港口"):"港口"("船厂);;是否正确?
w=x?y:z
if(x){w=y;}else{w=z;}
的一种快捷方式,因此您可以使用z的每个布尔值。谢谢..我将使用它;)