php - 数据库未更新(woocommerce 中的集成)

标签 php mysql sql database wordpress

我正在使用 Wordpress/WooCommerce。我的客户不希望用户必须创建一个帐户才能购买东西,但仍希望存储客户地址,以便在用户出于 future 目的返回该站点时自动填充。

我有一个学校项目,我将笔记保存到数据库(文本框中的纯文本)。它会抓取数据库中已有的“笔记”,并将它们显示给您,并提供更新笔记的选项。这将更新数据库条目。

我正在尝试调整此代码以与 woocommerce 一起使用。除了表的名称外,我没有做太多更改。

数据库连接正常,并按要求从表中提取内容。 我的问题是:当我点击“更新”时,数据库没有更新条目。我不确定这是否是因为我试图在 woocommerce 中使用它,而购物车导致了问题?但它实际上与我在工作的学校项目中的代码相同。


你可以看到它在这里工作(一旦你把东西添加到你的购物车): http://www.onelifehealthycuisine.com/mobile/checkout/

目前,我只是想让它更新地址,一旦成功,我将添加另一个单元格进行更新。


数据库

名称:tbl_stored_address

|编号 | ip_地址 |地址 |城市 |省 |邮政 |

测试入口: 2, 96.48.1.29, 123 fake street, vancouver, 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(); ?>

****更新****

我试过在没有 woocommerce 的情况下将代码添加到页面并直接访问 PHP 页面,它更新了数据库:http://www.onelifehealthycuisine.com/mobile/wp-content/themes/onelife/page-test.php

那么 URL 不是以 .php 文件结尾的事实吗?或者 woocommerce 模板中的某些内容不允许我正确运行表单?我似乎无法弄清楚。

最佳答案

假设 tbl_stored_address.id 是一个 int,这:

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

不会匹配任何东西。使用

WHERE tbl_stored_address.id =".$theID."

也就是说,您不应该使用 mysql_ 函数,并且绝对不应该使用字符串连接来构建您的查询。话虽如此,这是您的脚本已更新为使用 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
        $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 扩展将参数的顺序与您可能习惯的顺序相反,连接是第一个参数。与 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(); ?>

以上代码可能有错误,买者见谅。

您可以在 PHP.net 网站上找到学习 MySQLi 扩展所需的一切:http://php.net/manual/en/book.mysqli.php .您还会找到 PDO 的文档,这是我在 PHP 中编码时使用的最后一个扩展。

关于php - 数据库未更新(woocommerce 中的集成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24747868/

相关文章:

php - Twig 模板引擎不读取对象属性

mysql - 使用 SQL 更新数据库中的数据

mysql - Vapor MySQL 时间戳默认值

sql - SQL EXISTS 语句如何工作?

mysql - SQL查询以查找酒店的空闲房间

java - SQL速度提升: Left Join,或笛卡尔积/where子句

php - MySQL 选择组依据

php - 如何替换 PHP 中的 dynamic_sidebar?

php - JQuery colorbox 不适用于 PHP/MySQL

php - 如何在带有 cURL 的 php 中使用 google gmail api 发送电子邮件?