无法获取简单的PHP表单以向MySQL表添加/更新 [英] Unable to get simple PHP form to add/update to MySQL table

查看:64
本文介绍了无法获取简单的PHP表单以向MySQL表添加/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的MySQL表;用户,产品,地址.我正在尝试让我的表单将表中的新行添加到表中,以更新现有行.我的更新条件是,如果用户和产品都已经存在,只需更改地址即可.但是,如果既不存在(用户和产品),也不只是用户,则追加新的用户,产品和地址信息.

I have a MySQL table with three columns; user, product, address. I am trying to get my form to either add to the table a NEW ROW OF DATA OR UPDATE a pre-existing row. My conditions for update are IF both user and product already exist, just change address. BUT IF neither exist (user and product), or just user, then append new user, product, and address info.

该表单使用客户ID,产品的下拉框和新地址的文本框,但是我的SQL更新命令似乎使该页面失败,并且我找不到错误...这是我的尝试MySQL代码:

The form uses customer id, a dropdown box for the product, and a text box for the new address, however my SQL command for update seems to fail the page and I cannot find the error... Here is my attempt at MySQL code:

修改后的测试代码-仍然无法运行

mysqli_select_db("wp_newaddress", $con);

if(isset($_POST['submit'])) {
$id = $_POST['$userid'];
$product = $_POST['sku'];
$address = $_POST['address'];

$sql = "UPDATE wp_newaddress SET address='address' WHERE user='$userid' AND product='sku'";

echo "<p><h5>Change address:</h5>";

$loop = new WP_Query( $args );
echo '<br><select name="sku">';
    echo '<option>-- Select product--</option>';
while ( $loop->have_posts() ) : $loop->the_post();
  global $product;
    echo '<option value=' . $product->get_sku() . '>' . $product->get_sku() . ' </option>';
  endwhile;
echo '</select>';
echo '<br><input type="text" value="Insert new address here" id="address" size="40" />';
echo '<br><button type="submit" name="submit">Change address</button>';
$retval = mysqli_query( $sql, $con );
if(! $retval )
{
  die('Could not update data: ' . mysqli_error());
}
echo "Updated address successfully\n";

推荐答案

$sql = mysqli_query("UPDATE wp_newaddress SET address='address' WHERE user='$userid' AND product='sku'");

此代码执行查询.

对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,它将返回mysqli_result对象.对于其他成功的查询,它将返回TRUE.失败则为假

For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure

$ sql包含以上可能的返回值.

$sql contains the above possible return values.

因此,当您尝试第二次在行中执行查询

Thus when you try to execute the query second time in the line

$retval = mysqli_query( $sql, $con );

它不执行sql语句,而是执行返回值.

It is not executing the sql statement rather the return value.

例如: 如果第一个mysqli_query()的返回为FALSE, 然后是第二个mysqli_query('FALSE', $con)

For example: if the return of the first mysqli_query() is FALSE, then your second mysqli_query('FALSE', $con)

因此要更正它,您需要做

so to correct it, you will need to do

$sql = "UPDATE wp_newaddress SET address='$address' WHERE user='$userid' AND product='sku'";

这篇关于无法获取简单的PHP表单以向MySQL表添加/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆