需要帮助修改php脚本与switch-case

时间:2014-02-25 04:54:22

标签: php switch-statement

我需要修改这个源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的值,有什么问题?有什么建议吗?

1 个答案:

答案 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;
}