2014-07-14 3 views
2

저는 Wordpress/WooCommerce를 사용하고 있습니다. 고객은 사용자가 무언가를 사기 위해 계정을 만들어야하지만, 여전히 고객 주소를 저장하기를 원하지 않으므로 사용자가 나중에 사이트로 돌아올 때 자동으로 채워집니다.데이터베이스가 업데이트되지 않음 (woocommerce 통합)

데이터베이스 (텍스트 상자에 일반 텍스트)에 메모를 저장 한 학교 프로젝트가 있습니다. 그것은 이미 데이터베이스에있는 "메모"를 가져 와서 메모를 업데이트하는 옵션과 함께 보여줍니다. 이것은 데이터베이스 항목을 갱신합니다.

나는이 코드를 woocommerce와 함께 사용하기 위해 조정하려고합니다. 나는 테이블의 이름 외에 많이 바뀌지 않았다.

데이터베이스가 제대로 연결되고 요청한대로 테이블의 내용을 가져옵니다. 내 문제는입니다. 데이터베이스에서 "업데이트"를 클릭 할 때 항목을 업데이트하지 않습니다. 나는 이것이 woocommerce 내에서 사용하려고 시도하고 있기 때문에 이것이 확실한 지 모르며 장바구니가 문제를 일으키고 있습니까? 하지만 그것은 실질적으로 제 작업 학교 프로젝트와 같은 코드입니다.


당신은 (당신이 장바구니에 무언가를 추가하면)이 여기서 일하는 볼 수 있습니다http://www.onelifehealthycuisine.com/mobile/checkout/

을 현재, 난 그냥 '한 번 I를 작동 할 수, 그것은 주소를 업데이트하려고 노력하고있어 업데이트 할 다른 셀을 추가합니다.


데이터베이스

이름 : tbl_stored_address

| ID | ip_address | 주소 | 도시 | 지방 | 우편 |

시험 항목 :이, 96.48.1.29, 123 가짜 거리, 밴쿠버, BC 주, V3C 5R6


function dbConnect(){ 
    $db_host = 'xxx.xxx.com'; 
    $db_un = 'xxxx'; 
    $db_pass = 'xxxx'; 
    $db_name = 'xxxx'; 
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8",$db_un,$db_pass); 

    mysql_connect($db_host, $db_un, $db_pass) or die(mysql_error()); 
    mysql_select_db($db_name) or die(mysql_error());  
} 
dbConnect(); 

function getAddys(){ 
    $user_id = '96.48.1.29'; //get user IP 
    $query = "SELECT tbl_stored_address.address, tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address ='".$user_id."';"; 
    $result = mysql_query($query) or die(mysql_error()); 
    //saves number of rows returned 
    $rowCount = mysql_num_rows($result); 
    echo "<form enctype='multipart/form-data' action='form-checkout.php' method='post'>"; 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    { 
     echo "<textarea name='address".$row['id']."' id='address".$row['id']."'>".$row['address']."</textarea><br/>"; 
    } 
    echo "<br/><input type='submit' value='Update Note(s)' name='updateNote'/><br/>"; 
    echo "</form>"; 
} 
//this will check if the user clicked on "update" for a note, and then update the correct notes using the ID 
if(isset($_POST['updateNote'])) 
{ 
    $user_id = '96.48.1.29'; //get user IP 
    $query = "SELECT tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address ='".$user_id."';"; 
    $result = mysql_query($query) or die(mysql_error()); 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    { 
     if((isset($_POST['address'.$row['id'].'']))){ 
      $value = $_POST['address'.$row['id'].'']; 
      $theID = $row['id']; 
      $query2 = "UPDATE tbl_stored_address SET tbl_stored_address.address='".mysql_real_escape_string($value)."' WHERE tbl_stored_address.id ='".$theID."';"; 
      $result2 = mysql_query($query2) or die(mysql_error()); 
     } 
    } 
} 

?>  

<h2>Delivery Address</h2><br/><br/> 

<?php getAddys(); ?> 

**** UPDATE ****

woocommerce가없는 페이지에 코드를 추가하고 PHP 페이지에 직접 액세스하려고 시도했으며 데이터베이스를 잘 업데이트합니다. http://www.onelifehealthycuisine.com/mobile/wp-content/themes/onelife/page-test.php

URL이 .php 파일로 끝나지 않습니다. 아니면 woocommerce 템플릿의 어떤 부분에서 제대로 양식을 실행할 수 없습니까? 나는 그것을 알아낼 수 없습니다. tbl_stored_address.id 가정

+1

PDO 개체를 생성 한 다음, 더 이상 사용되지 않는'mysql_' 함수를 사용하는 특별한 이유가 있습니까? –

+0

그것이 학교에서 우리에게 가르쳐 준 것이기 때문입니다. 나는 그것이 최고의 코드가 아니라는 것을 알고 있지만, 지금은 그냥 작동시킬 필요가 있습니다. –

답변

0

int이며,이 :

WHERE tbl_stored_address.id ='".$theID."' 

아무것도 일치하지 않습니다. 말했다되고 그건

WHERE tbl_stored_address.id =".$theID." 

사용하여, 당신은 mysql_ 기능을 사용하지 않아야, 확실히 문자열 연결을 사용하여 쿼리를 작성하지 않아야합니다.

<?php 
    function dbConnect() 
    { 
     $db_host = 'xxx.xxx.com'; 
     $db_un = 'xxxx'; 
     $db_pass = 'xxxx'; 
     $db_name = 'xxxx'; 

     $conn = mysqli_connect($db_host, $db_un, $db_pass, $db_name) or die(mysqli_error());  
    } 

    dbConnect(); 

    function getAddys() 
    { 
     $user_id = '96.48.1.29'; //get user IP 
     $query = "SELECT tbl_stored_address.address, tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address = '" . $user_id . "';"; 
     $result = mysqli_query($conn, $query) or die(mysqli_error($conn)); 

     //saves number of rows returned 
     $rowCount = mysqli_num_rows($conn, $result); 
     echo "<form enctype='multipart/form-data' action='form-checkout.php' method='post'>"; 
     while ($row = mysqli_fetch_assoc($conn, $result)) 
     { 
      echo "<textarea name='address".$row['id']."' id='address" . $row['id'] . "'>" . $row['address'] . "</textarea><br/>"; 
     } 
     echo "<br/><input type='submit' value='Update Note(s)' name='updateNote'/><br/>"; 
     echo "</form>"; 
    } 

    //this will check if the user clicked on "update" for a note, and then update the correct notes using the ID 
    if(isset($_POST['updateNote'])) 
    { 
     $user_id = '96.48.1.29'; //get user IP 
     $query = "SELECT tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address = '" . $user_id . "';"; 
     $result = mysqli_query($conn, $query) or die(mysqli_error($conn)); 
     while ($row = mysqli_fetch_assoc($conn, $result)) 
     { 
      if((isset($_POST['address' . $row['id']]))) 
      { 
       $value = $_POST['address' . $row['id']]; 
       $theID = $row['id']; 
       $query2 = "UPDATE tbl_stored_address SET tbl_stored_address.address = '" . mysqli_real_escape_string($conn, $value) . "' WHERE tbl_stored_address.id = " . $theID . ";"; 
       $result2 = mysqli_query($conn, $query2) or die(mysqli_error($conn)); 
      } 
     } 
    } 
?> 

<h2>Delivery Address</h2><br /><br /> 

<?php getAddys(); ?> 

MySQLi 확장을 사용하면 연결이 첫 번째 매개 변수 인 상태로 사용할 수 있습니다 무엇에서 매개 변수의 순서를 반대로 : 그것은이가 MySQLi 확장을 사용하도록 업데이트 스크립트입니다 말했다되고. MySQL 확장과 마찬가지로, 연결 객체를 생략하면 확장 기능은 기존의 열린 연결을 재사용하려고 시도합니다.

실제로은 SQL에서 문자열 연결을 사용하지 않으므로 MySQli 확장이 지원하는 준비된 문 사용에 익숙해 져야합니다. 준비된 진술을 사용한 코드는 다음과 같습니다.

<?php 
    function dbConnect() 
    { 
     $db_host = 'xxx.xxx.com'; 
     $db_un = 'xxxx'; 
     $db_pass = 'xxxx'; 
     $db_name = 'xxxx'; 

     $conn = mysqli_connect($db_host, $db_un, $db_pass, $db_name) or die(mysqli_error()); 
    } 

    dbConnect(); 

    function getAddys() 
    { 
     $user_id = '96.48.1.29'; //get user IP 
     if ($query = mysqli_prepare($conn, "SELECT tbl_stored_address.address, tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address = ?;")) 
     { 
      mysqli_stmt_bind_param($query, "s", $user_id); # 's' indicates that this parameter is a string 
      mysqli_stmt_execute($query); 

      mysqli_stmt_bind_result($query, $row); 

      echo "<form enctype='multipart/form-data' action='form-checkout.php' method='post'>"; 
      while (mysqli_stmt_fetch(($query)) 
      { 
       echo "<textarea name='address" . $row['id'] . "' id='address" . $row['id'] . "'>" . $row['address'] . "</textarea><br/>"; 
      } 
      echo "<br/><input type='submit' value='Update Note(s)' name='updateNote'/><br/>"; 
      echo "</form>"; 

      mysqli_stmt_close($query); 
     } 
    } 

    //this will check if the user clicked on "update" for a note, and then update the correct notes using the ID 
    if(isset($_POST['updateNote'])) 
    { 
     $user_id = '96.48.1.29'; //get user IP 
     if ($query = mysqli_prepare($conn, "SELECT tbl_stored_address.address, tbl_stored_address.id FROM tbl_stored_address WHERE tbl_stored_address.ip_address = ?;")) 
     { 
      mysqli_stmt_bind_param($query, "s", $user_id); # 's' indicates that this parameter is a string 
      mysqli_stmt_execute($query); 

      mysqli_stmt_bind_result($query, $row); 

      while (mysqli_stmt_fetch(($query)) 
      { 
       if((isset($_POST['address' . $row['id']]))) 
       { 
        $value = $_POST['address' . $row['id']]; 
        $theID = $row['id']; 

        if ($updateQuery = mysqli_prepare($conn, "UPDATE tbl_stored_address SET tbl_stored_address.address = ? WHERE tbl_stored_address.id = ?;")) 
        { 
         $updateQuery = "UPDATE tbl_stored_address SET tbl_stored_address.address = ? WHERE tbl_stored_address.id = ?;"; 

         mysqli_stmt_bind_param($updateQuery, "s", $value); # 's' indicates that this parameter is a string 
         mysqli_stmt_bind_param($updateQuery, "i", $theID); # 'i' indicates that this parameter is an integer 
         mysqli_stmt_execute($updateQuery); 
        } 
       } 
      } 

      mysqli_stmt_close($query); 
     } 
    } 
?> 

<h2>Delivery Address</h2><br /><br /> 

<?php getAddys(); ?> 

위의 코드에 오류가있을 수 있으므로주의해야합니다.

MySQLi 확장을 배우기 위해 필요한 모든 것을 PHP.net 웹 사이트 : http://php.net/manual/en/book.mysqli.php에서 찾을 수 있습니다. PHP로 코딩 할 때 사용한 마지막 확장 인 PDO 용 문서도 있습니다.

+0

Thanks @Tieson MySQLi에 대해 좀 더 알아 보겠습니다. mysql_를 사용할 수 없거나 사용할 수없는 특별한 이유가 있습니까? 여전히 작동합니다. 너무 끔찍한면 왜 저에게이 모든 것을 가르쳐 준 학교에서 프로그램을 업데이트하지 않았는지 궁금합니다 (6 개월 전 수업을 들었음). –

+0

공식적으로 사용되지 않으므로 MySQL 확장을 사용하지 않아야합니다. 이는 어느 시점에 공식 배포판에서 제외된다는 것을 의미합니다. 그것은 본질적으로 안전하지 않은 라이브러리입니다. 개선 된 확장 (MySQLi의 'i')의 요점은 원래 확장의 문제 점에 대해 상당히 많은 부분을 다룹니다. 또한 객체 지향 접근법을 추가합니다. 왜 학교가 바뀌지 않았습니까? 그것이 내가 일하는 대학과 같다면 : 관성. –

+0

@KerynGill 또한 다양한 API를 비교하면 다음과 같습니다. http://php.net/manual/en/mysqlinfo.api.choosing.php –

관련 문제