2017-02-21 20 views
-1

TABLE.PHP - 表正確顯示。我剛在這裏發佈了重要的代碼。將數組中的td值傳遞給函數類,然後插入數據庫

$ipn="192.168.196."; 
echo "<form method ='post' action='class_prdsbmt.php'>"; 
echo "<table border='5'>"; 
echo "<td>"; 
echo "<table name='' border='3'>"; 
for($a1=51;$a1<68;$a1++){ 
    for($a2=52;$a2<70;$a2++){ 
     echo "<tr>";    
     echo "<td><input type='hidden' value='".$ipn.$a1."' name='td_a[]' />$ipn$a1</td>"; 
     echo "<td><input type='hidden' value='".$ipn.$a2."' name='td_a[]' />$ipn$a2</td>"; 
     echo "</tr>"; 
     $a1+=2; 
     $a2+=1; 
    } 
    echo "</table>"; 
    echo "</td>"; 
} 

echo "<td><input type='submit' value='PRINT' name='btnss' /></td>"; 

class_prdsbmt.php

include("class_prdtrans.php"); 
$pcs = $_POST['td_a']; 

for ($i = 0; $i < ; $i++) { 
    $arr=$pcs[$i]; 
    $printval = new PrintVal($arr,"TESTING ONLY"); 
    $crud = new CRUD_process(); 
} 

<?php 
if(isset($_POST['btnss'])){ 
    $isSuccess=0; 
    $strCol = "pcip,pcname"; 
    $strVal = "'".$printval->getIp()."','". $printval- >getPcname()."'"; 
    $isSuccess = $crud->saveRecord("machine",$strCol,$strVal); 
}else{ 
    "NOT SAVED! GO BACK!!!"; 
} 

?> 

class_prdtrans.php

define('DB_SERVER','localhost'); 
define('DB_USER','user'); 
define('DB_PASSWORD',''); 
define('DB_NAME','db'); 

class PrintVal{ 
    var $_ipmac; 

    function __construct($ipmac,$namemac){ 
     $this->_ipmac = $ipmac; 
    } 

    function getIp(){ 
     return $this->_ipmac; 
    } 

} 


//crud operation 
class CRUD_process{ 
    var $con; 

    function __construct(){ 
     $this->con = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME); 
    } 

    function saveRecord($record,$columns,$values){ 
     $result = false; 

     switch($record){ 
     case "machine": 
      $result = $this->con->query("INSERT INTO tbl (".$columns.") VALUES(".$values.");"); 
      break; 
     default: 
     } 
     return $result; 
    } 

    function getRecord($record,$values){ 
     $result = false; 
     $val = explode(",",$values); 

     switch($record){ 
     case "machine": 
      $result = $this->con->query("SELECT * FROM tbl WHERE pcip =".$val[0]." AND pcname = ".$val[1].";"); 
      break; 
     default: 
     } 
     return $result; 
    } 

} 

嘗試呼應INSERT查詢和輸出是錯誤的。第一個字段(pcip)值爲空。以下是SQL輸出。

INSERT INTO tbl (pcip,pcname) VALUES('','TESTING ONLY'); 

請幫我先到了一種更好的方式或簡單的方式接入的所有數組值「td_a []」從表中插入我的數據庫,一旦我按一下按鈕。

+0

因此,做一些基本的調試以找出你在那個地方使用的變量爲什麼是空的......並且還要閱讀SQL注入以及你需要做些什麼 – CBroe

+0

@Brian Tompsett - 湯萊恩Ok會這樣做,謝謝 – Ronix

回答

0
在class_prdsbmt.php

for ($i = 0; $i < ; $i++) { 
    $arr=$pcs[$i]; 
    $printval = new PrintVal($arr,"TESTING ONLY"); 
    $crud = new CRUD_process(); 
} 

$ I < ????

,你也白白造成了太多的OBJ CRUD。將它移動到if(isset條件低於

+0

Ooppss。它應該是$ i Ronix

+0

另外,其他的創建obj是以後檢索數據。 – Ronix

+0

我知道了。我把它放在if條件內。這裏就是我所做的。 如果(isset($ _ POST [「btnsbmt」] )){ \t \t $ isSuccess = 0; \t \t $ pcs = $ _POST [「td_a」]; \t \t //柱 \t \t 爲($ I = 0; $ I <計數($ PCS); $ I ++){$ 主機=新主機($個[$ i]於 「PHTESTXX」); $ crud = new CRUD_process(); \t \t $ strCol =「pcip,pcname」; \t \t $ strVal =「'」。$ hosts-> getInputip()。「','」。 $ hosts-> getInputnamepc() 「'」。 \t \t //調用crud過程函數執行查詢使用參數 \t \t $ isSuccess = $ crud-> saveRecord(「comp」,$ strCol,$ strVal); } \t} 謝謝。 – Ronix