2015-01-26 93 views
0

我喜歡用jQuery,php和Ajax使用json_encode和Ajax Post更新我的數據庫,但我不明白如何使用數組$ json_encode更新數據庫for每個循環。 ¿那麼,我如何使用$ json_encode更新我的數據庫?

這是我在控制檯響應時,點擊保存按鈕順序:

{ 
    "success": true, 
    "parent_id": [ 
     "0,17,17,17,17,17,17,17,17,17,17,17,0,0,0" 
    ], 
    "order_page": [ 
     "3,4,5,6,7,8,9,10,11,12,13,14,0,1,2" 
    ], 
    "id_page": [ 
     "17,8,9,2,3,4,5,6,7,11,10,1,16,14,15" 
    ] 
} 

這是我的職位AJAX的php事件saveorder.php

<?php 
    header('Content-type: text/javascript'); 
    $json = array('success'=>false, 
        'parent_id' =>array($_POST['parent_id']), 
        'order_page' => array($_POST['order_page']), 
        'id_page'=>array($_POST['id_page']) 
    ); 
if(isset($_POST['parent_id'],$_POST['order_page'],$_POST['id_page'])){ 
    $json = array('success'=>true, 
        'parent_id' =>array($_POST['parent_id']), 
        'order_page' => array($_POST['order_page']), 
        'id_page'=>array($_POST['id_page']) 
      ); 
    echo json_encode($json);  
    $con = $this->connect(); 
    $Ordersaved=json_decode($json); 
    foreach($Ordersaved as $key){ 
     mysqli_query($con, "UPDATE pages SET parent_id='$parent_id' Order_Page=$order_page ID_Page=$id_page WHERE Section_ID=0 "); 
    } 

} 
elseif(is_null($_POST['parent_id'],$_POST['order_page'],$_POST['id_page'])){ 
    echo "Las variables son nulas"; 
} 
function connect(){ 
    return mysqli_connect("localhost", "root","root","db"); 
} 
?> 

這是我的db:

CREATE TABLE `pages` (
 
    `ID_Page` int(11) NOT NULL, 
 
    `TitlePage` varchar(255) NOT NULL, 
 
    `SectionPage` varchar(255) NOT NULL, 
 
    `CategoryPage` varchar(255) NOT NULL, 
 
    `NamePage` varchar(255) NOT NULL, 
 
    `BodyPage` varchar(9000) NOT NULL, 
 
    `DescriptionPage` varchar(255) NOT NULL, 
 
    `parent_id` int(11) unsigned NOT NULL DEFAULT '0', 
 
    `Section_ID` int(11) NOT NULL DEFAULT '0', 
 
    `Category_ID` int(11) NOT NULL DEFAULT '0', 
 
    `Order_Page` int(11) DEFAULT '0' 
 
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT ;

回答

1

不用解釋,我覺得你可以看出其中的區別:

$json = array('success'=>false, 
        'parent_id' => $_POST['parent_id'], 
        'order_page' => $_POST['order_page'], 
        'id_page'=>$_POST['id_page'] 
    ); 
if(isset($_POST['parent_id'],$_POST['order_page'],$_POST['id_page'])){ 
    $json = array('success'=>true, 
        'parent_id' => $_POST['parent_id'], 
        'order_page' => $_POST['order_page'], 
        'id_page'=> $_POST['id_page']) 
      ); 
    echo json_encode($json);  
    $con = $this->connect(); 
    // HERE 
    mysqli_query($con, "UPDATE pages SET parent_id=".$json['parent_id'].", Order_Page=".$json['order_page'].", ID_Page=".$json['id_page']." WHERE Section_ID=0 ") 

    // $Ordersaved=json_decode($json); 
    //foreach($Ordersaved as $key){ 
     // mysqli_query($con, "UPDATE pages SET parent_id='$parent_id' Order_Page=$order_page ID_Page=$id_page WHERE Section_ID=0 "); 
    //} 
+0

這不是在mysqli的數據保存。但感謝您的atention。 – Leo1234562014 2015-03-01 23:16:02

+0

你是什麼意思? – 2015-03-01 23:26:45

+0

爲了將數據保存到數據庫中,代碼不起作用 – Leo1234562014 2015-03-04 01:08:25