我需要修改这个源PHP脚本代码。当我使用switch-case修改时,当我给出text1
时,我得到了text1的错误值$query = "INSERT INTO mytable( text_name, text_image, text_detail ) VALUES ( :name, :image, :detail ) ";
$query_params = array(
':name' => $_POST['text_name'],
':image' => $_POST['text_image'],
':detail' => $_POST['text_detail ']
);
To(类似)这个
switch("text1" || "tex2" || "text3"){
case "text1":
$query = "INSERT INTO comments ( text_name, text_image, text1_detail)
VALUES ( :name, :image, :detail) ";
//Update query
$query_params = array(
':name' => $_POST['text_name'],
':image' => $_POST['text_image'],
':detail' => $_POST['text1_detail']
);
break;
case "tex2":
$query = "INSERT INTO comments ( text_name,text_image, text2_detail )
VALUES ( :text_id, :image, :detail) ";
//Update query
$query_params = array(
':name' => $_POST['text_name'],
':image' => $_POST['text_image'],
':detail' => $_POST['text2_detail ']
);
break;
case "text3":
$query = "INSERT INTO comments ( text_name, text_image, text3_detail)
VALUES ( :text, :image, :detail) ";
//Update query
$query_params = array(
':name' => $_POST['text_name'],
':image' => $_POST['text_image'],
':detail' => $_POST['text3_detail'],
);
break;
default: echo "Something is wrong fuckingshit!! Give me at least text1 or text2 or text3 !!!";
}
但是当我给text1时,我得到的错误没有text1的值,有什么问题?有什么建议吗?
答案 0 :(得分:0)
switch
语句接受变量,并根据case
中的每个值对其进行评估:
switch ($yourVariable) {
case "text1": # Equivilent to if ($yourVariable == "text1")
# do stuff
break;
case "text2":
# do different stuff
break;
case "text3":
# etc.
break;
}