2015-05-18 108 views
0

我需要根據所選單選按鈕從數據庫中選擇一個表格。我認爲這將工作的方式是,1)通過確定在if語句中選擇了哪個單選按鈕併爲其分配一個變量;以及2)將該變量分配給PDO中用於連接的$ dbname(表的變量)。我在嘗試中沒有取得成功,所以我需要知道如何從if語句中獲取變量並在PDO中使用它(或者告訴我更好的解決方案)。使用單選按鈕從數據庫中選擇表格

示例HTML(有三個單選按鈕):

<div id="radio_buttons"> 
        <label for="p_book">Book</label> 
        <input type="radio" name="p_radio" id="p_book" value="p_book"> 

        <label for="p_article">Article</label> 
        <input type="radio" name="p_radio" id="p_article" value="p_article"> 

        <label for="journal">Journal</label> 
        <input type="radio" name="p_radio" id="p_journal" value="p_journal"> 


       </div> <!-- end radiobuttons --> 

如何定位在PHP單選按鈕:

if (isset($_POST['edify_button'])) { 

     if(isset($_POST['p_radio'])) { 
      $radio_values = $_POST['p_radio']; 
      if ($radio_values == 'p_book') { 
       $dbname = 'books'; //this is the name of the table 
       return $dbname; 

      }elseif ($radio_values == 'p_article') { 
       $dbname = 'articles'; 
       return $dbname; 


      }elseif ($radio_values == 'p_journal') { 
       $dbname = 'journals'; 
       return $dbname; 

      } 
     }else{ 
       echo 'unchecked'; 
    } 
    } 

然後我想,不知何故,得到該變量來計算在PDO中:

function connect($dbname, $servername) { 


     //$dbname is supposed to be retrieved from the if-statement 

     $servername = "localhost"; 
     $username = "admin"; 
     $password = "password"; 

    try { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 

我也試着把if語句放在connect()中,以防connect()沒有從if語句訪問變量:沒有成功。

我在網上搜索了一個像瘋子這樣的信息,但沒有運氣。如果你知道一個涵蓋這些東西的教程,請發佈它。

+0

你不能在'if'語句中使用'return' ,它只是從函數返回一個值。你如何調用你的'connect()'函數? –

+1

爲什麼在函數中設置'$ servername'時,它是一個參數? – Barmar

+0

周杰倫:if語句最初是一個函數,但是返回(但thx的信息,沒有意識到這一點)。 connect()從具有require_once的html文件調用:'filename.php';和一個我創建的變量,它指的是連接($ connect =「connect」;然後調用它:$ connect();)Barman:這是因爲我曾經使用這些參數從php文件中調用函數,像這樣:$ connect(name,server); - 我知道有錯誤,但你們會怎麼做?問題是,從字符串變量(表名)中檢索值並將其添加到PDO中。 –

回答

0

我解決了這個問題(它不是面向對象的,它仍然是一個有點原始)是通過實現3個步驟的方式:

1)創建一個函數if語句來檢查的單選按鈕值和分配值賦給與表名對應的變量; 2)創建一個函數的變量,並在PDO中調用它(見下文);

3)在PDO函數內添加一個簡單的if語句來檢查是否沒有單選按鈕。

第一個函數:

function table_name() { 
    if (isset($_POST['edify_button'])) { 

     if(isset($_POST['p_radio'])) { 
      $radio_values = $_POST['p_radio']; 
      if ($radio_values == 'p_book') { 
       $table_name = 'books'; 
       return $table_name; 

      }elseif ($radio_values == 'p_article') { 
       $table_name = 'articles'; 
       return $table_name; 

      }elseif ($radio_values == 'p_journal') { 
       $table_name = 'journals'; 
       return $table_name; 
      } 
     }else{ 
       $table_name = false; 
     } 
    } 
} 

然後一些變量:

$table_name = "table_name"; 
$function_connect = "connect"; 
$function_connect(); //initialize the function; 

最後的PDO功能:

function connect() { 

     $dbname = 'citation'; 
     $servername = "localhost"; 
     $username = "admin"; 
     $password = "password"; 
     $table_name = table_name(); 


    try { 

     //if table is false, connection is refused; 
     if (table_name() == false) { 
      echo 'Pick a category'; 
     }else{ 

     //if table is not false, connection is executed depending on the radio button (which corresponds to a table); 

     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
     $conn->exec("SET NAMES 'utf8'"); 
     // set the PDO error mode to exception 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     echo "Connected successfully" . '<br>'; 

     //$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"); 

      $stmt = $conn->prepare("INSERT INTO $table_name //this is where the table is picked 
相關問題