2012-05-21 6 views
0

Ive는 2 개의 배열을 비교하는 함수를 가지고 있습니다. Array diff는 첫 번째 인수가 배열이 아니라는 경고를 보내는 중입니다.이 함수를 작성하는 데 더 좋은 방법이 열려 있습니다. 나는 이것에 날개를 달기 만했다. 감사!Array 배열을 찾지 못함

function changeLog(){ 
include('../includes/conn.inc.php'); 
//select object and make an array with each current value 
    $stmt = $mysql->prepare ("SELECT * FROM table WHERE id=?"); 
    $stmt->bind_param("i", $_POST['id']); 
    $OK = $stmt->execute(); 
    $stmt->bind_result(
     $id, 
     $name, 
     $created, 
     $edited, 
     $owner 
    ); 
while($row = $stmt->fetch()) { 
    $array1 = array(
     'name' => $name, 
     'owner' => $owner 
     );} 
    $stmt->close(); 

//$array1 now holds current values for each field 
//now grab the post values from the update and stick them into array2 

     $name= $_POST['name']; 
     $owner= $owner; 

    $array2 = array(
     'name' => $name, 
     'owner' => $owner 
     ); 

//$array2 now holds post values for each field in the update 
//check the arrays and spit out the differences 

    $result = array_diff($array1, $array2); 
//strip the values and just output the array keys 

$dbInput =(array_keys($result)); 

     foreach($dbInput as $i){ 
      $owner= 'use'.$_SESSION['i']; 
      $sql = 'INSERT INTO history (id, created, edited, owner, parent, body) 
        VALUES (NULL,NOW(),NOW(),?,?,?)'; 
      $stmt = $mysql->stmt_init(); 
      if ($stmt->prepare($sql)) { 
       $stmt->bind_param('sss', $owner, $_POST['id'], $i); 
       $OK = $stmt->execute();} 
       $stmt->close(); 

     } 
}// end changeLog 
+0

를? 새 행을 삽입하고 기존 행을 업데이트하거나, 무엇을해야합니까? – Julian

+0

변경된 필드의 이름과 변경 한 사람의 사용자 개체 요소가있는 별도의 테이블에 행을 삽입합니다. 해당 부분은 문제가 없습니다. 이 함수는 실제로 잘 동작하지만, 배열 diff와 배열 키는 첫 번째 인수에 대한 배열을 얻지 못하고 그 이유를 알 수 없다는 경고를 던집니다. – Severian

+0

while 루프가 0 번 실행되면 array1이 할당되지 않습니다. – Julian

답변

0

는 더이 같은 비트 코딩 시도하고 그것은 당신에게 아무것도 알 수 있는지 : 당신은 실제로 여기에 일을하려고 무엇

if($row = $stmt->fetch()) { 
    $array1 = array('name' => $name, 'owner' => $owner); 
    //$array1 now holds current values for each field 
    //now grab the post values from the update and stick them into array2 
    $name= $_POST['name']; 
    $owner= $owner; 
    $array2 = array('name' => $name,   'owner' => $owner   ); 
    //$array2 now holds post values for each field in the update 
    //check the arrays and spit out the differences 
    $result = array_diff($array1, $array2); 
// ... 
} else { 
     echo "no result returned"; 
} 
+0

덕분에 젠장, 몇 가지 시행 착오 후 내가 뭔가 잘못 생각 ID에 대한 게시물 배열과 생각합니다. – Severian

+0

그건 쿼리를 실패하게 만들 수 있습니다. – Julian

관련 문제