2016-11-05 131 views
1

我的表類有這些列:存儲數據庫值到變量

 
idcategory 
categorySubject 
users_idusers 

我有一個簡單的單選按鈕和文本框的形式。 我有類別全選語句,並需要獲得idcategory存儲到一個變量($ getCatId),所以我可以用這個語句:

$sql="INSERT INTO topic(subject, topicDate, users_idusers,  category_idcategory, category_users_idusers) VALUES('($_POST[topic])', '$date', '$_SESSION[userid]', '$getCatId', '$_SESSION[userid]');"; 

是什麼,以獲得最佳的方式和存儲類別ID?

if($_SERVER['REQUEST_METHOD'] != 'POST') //show form if not posted 
    { 

     $sql = "SELECT * FROM category;"; 

    $result = mysqli_query($conn,$sql); 

     ?> 
    <form method="post" action="createTopic.php"> 
     Choose a category: 
     </br> 
     </br> 

     <?php 
     while ($row = mysqli_fetch_assoc($result)) { 
      echo "<div class= 'choice'><input type='radio' name='category' value='". $row['idcategory'] . "'>" . $row['categorySubject'] ."</div></br>"; 

     } 

       echo 'Topic: <input type="text" name="topic" minlength="3" required> 
      </br></br> 
      <input type="submit" value="Add Topic" required> 
      </form>'; 

      } 


    if ($_POST){ 
     if(!isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == false) 
{ 

    echo 'You must be signed in to contribute'; 
} 
     else{ 
     $sql="INSERT INTO topic(subject, topicDate, users_idusers, category_idcategory, category_users_idusers) VALUES('($_POST[topic])', '$date', '$_SESSION[userid]', '$getCatId', '$_SESSION[userid]');"; 

      $result = mysqli_query($conn,$sql); 
      echo "Added!"; 
+1

** WARNING **:當使用'mysqli'你應該使用[參數化查詢(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **絕不**將'$ _POST'或'$ _GET'數據直接放入查詢中,如果有人試圖利用您的錯誤,這會非常有害。 – tadman

回答

0

如果我沒有理解這個問題,你有你$getCatId$_POST['category'](類別的ID),你的情況(發送表單後)

+0

是的,在按下提交按鈕之後,用戶從來自數據庫的單選按鈕中進行選擇。我不知道它是否重要,或者我在哪裏放置代碼,但我已經有一個查詢。我不確定是否可以在同一部分放置另一個 – Mei

0

你應該做的第一件事是保護自己SQL injection通過參數化您的查詢Bobby Tables來拜訪您。

bobby tables

你也可以考慮使用PDO,因爲我已經證明如下因爲它是一個統一的API是有很多不同的數據庫管理系統的工作原理,所以這會導致你奇妙移植的代碼。 Here's an annotated working example on Github

<?php 

// returns an intance of PDO 
// https://github.com/jpuck/qdbp 
$pdo = require __DIR__.'/mei_DV59j8_A.pdo.php'; 

// dummy signin 
session_start(); 
$_SESSION['signedIn'] = true; 
$_SESSION['userid'] = 42; 

//show form if not posted 
if($_SERVER['REQUEST_METHOD'] != 'POST'){ 

    $sql = "SELECT * FROM category;"; 

    // run query 
    $result = $pdo->query($sql); 

    ?> 

    <form method="post" action="createTopic.php"> 
    Choose a category: 
    </br> 
    </br> 

    <?php 
    // get results 
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) { 
     echo " 
      <div class= 'choice'> 
       <input type='radio' name='category' value='$row[idcategory]'/> 
       $row[categorySubject] 
      </div> 
      </br> 
     "; 
    } 

    echo ' 
     Topic: <input type="text" name="topic" minlength="3" required> 
     </br></br> 
     <input type="submit" value="Add Topic" required> 
    </form> 
    '; 

} 


if ($_POST){ 
    if(!isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == false){ 
     echo 'You must be signed in to contribute'; 
    } else { 
     // simulate your date input 
     $date = date("Y-m-d"); 

     // bind parameters 
     $sql = " 
      INSERT INTO topic (
       subject, topicDate, users_idusers, category_idcategory, category_users_idusers 
      ) VALUES(
       :subject, :topicDate, :users_idusers, :category_idcategory, :category_users_idusers 
      ); 
     "; 

     // prepare and execute 
     $statement = $pdo->prepare($sql); 
     $statement->execute([ 
      'subject' => "($_POST[topic])", 
      'topicDate' => $date, 
      'users_idusers' => $_SESSION['userid'], 
      // to answer your question, here's your variable 
      'category_idcategory' => $_POST['category'], 
      'category_users_idusers' => $_SESSION['userid'], 
     ]); 

     echo "Added!"; 
    } 
}