2017-05-08 10 views
-2

私はテーブルから受け取ったIDごとに別のクエリを実行したい、解決するのを助けてください。例えばMySQLのPHP条件ベースのクエリ

$ == 5ファイル名を指定して実行クエリA idcat場合とそうでない$ == 4 idcat実行クエリBここで

//include connection file 
    include_once("connection.php"); 

    $db = new dbObj(); 
    $connString = $db->getConnstring(); 

    $params = $_REQUEST; 

    $action = isset($params['action']) != '' ? $params['action'] : ''; 
    $empCls = new FillEmpty($connString); 



    function insertFillEmpty($params) { 
     $data = array(); 

     $catintid = "SELECT categoryinternalID FROM inhandemptystock"; 
     $results = mysqli_query($this->conn, $catintid); 
     $raw = mysql_fetch_array($results); 
     $idcat = $raw["categoryinternalID"]; 
     if ($idcat == "5") { 

     $sqll = "INSERT INTO `testing` (goog) VALUES('" . $params["enteredBy"] . "'); "; 
     echo $result = mysqli_query($this->conn, $sqll) or die("error to insert employee data"); 

} else { 
      echo '<script language="javascript">'; 
      echo 'alert("Something is wrong")'; 
      echo '</script>'; 
     } 


    } 
+1

あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃に対して脆弱です。 [** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https://secure.php.net/)を使用してください。 manual/en/pdo.prepared-statements.php)は、[** this post **](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql - インジェクション - イン - php)。 –

+0

あなたはmysqlとmysqliを混在させています。 mysql_ *関数を使わないでください。それらはv5.5(2013年6月)から廃止され、v7.0(2015年12月)以降削除されました。代わりに[** mysqli _ ***](https://secure.php.net/manual/en/book.mysqli.php)または[** PDO **](https://secure.php.net/** ** prepared statements **](https://secure.php.net/manual/en/pdo.prepare.php)と[**バインドされたパラメータ** ](https://secure.php.net/manual/en/pdostatement.bindparam.php)。 –

+1

あなたはあなたが何をしているのか正確に説明していませんか?ここで何が問題なの? – David

答えて

0

は、あなたが希望する結果

<?php 
$result = ""; 
$mysqli = new mysqli('localhost', 'root', '', 'auto') or die($mysqli->error); 
$query = $mysqli->query("SELECT categoryinternalID FROM inhandemptystock") or die($mysqli->error); 
while($rows = $query->fetch_array(MYSQL_ASSOC)){ 
    $id = $rows['categoryinternalID']; 
    if($id == 5){ 
     do something 
    }else{ 
     do something 
    } 
} 
を達成できるかの簡単な例であれば
+0

あなたの助言に感謝し、私は間違いなくPDOを使用します。 コードを試してみましょう。 – neohacksus