我正在使用Wordpress / WooCommerce。我的客户不希望用户创建帐户来购买东西,但仍希望存储客户地址,以便当用户回到网站以供将来使用时自动填充。
我有一个学校项目,我将一个注释保存到数据库(文本框中的纯文本)。它抓住了"笔记"已经在数据库中,并向您显示它们,并提供更新注释的选项。这会更新数据库条目。
我正在尝试使用此代码来处理woocommerce。除了桌子的名字,我还没有太大的变化。
数据库连接正常,它根据请求从表中提取内容。 我的问题是:当我点击"更新"时,数据库没有更新条目。我不确定这是不是因为我试图在woocommerce中使用它,而且购物车引起了问题?但它与我的工作学校项目实际上是相同的代码。
您可以在此处看到它(一旦您向购物车添加内容): http://www.onelifehealthycuisine.com/mobile/checkout/
目前,我只是想让它更新地址,一旦有效,我会在其他单元格中添加更新。
数据库
名称:tbl_stored_address
| ID | ip_address |地址|城市|省|邮政|
测试条目: 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(); ?>
**** **** UPDATE
我尝试将代码添加到没有woocommerce的页面并直接访问PHP页面,它更新了数据库:http://www.onelifehealthycuisine.com/mobile/wp-content/themes/onelife/page-test.php
因此,URL不以.php文件结尾这一事实?或者woocommerce模板中的某些内容不允许我正确运行表单?我似乎无法弄明白。
答案 0 :(得分:0)
假设tbl_stored_address.id
是int
,请执行以下操作:
WHERE tbl_stored_address.id ='".$theID."'
不匹配任何东西。使用
WHERE tbl_stored_address.id =".$theID."
话虽如此,您不应该使用mysql_
函数,并且DEFINITELY不应该使用字符串连接来构建您的查询。话虽这么说,这是你的脚本更新为使用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编码时使用的最后一个扩展。