【问题标题】:Trying to update database but the form only inserts blank entries尝试更新数据库,但表单仅插入空白条目
【发布时间】:2015-01-08 20:56:12
【问题描述】:

我有一个用户注册并登录的网站,然后被带到一个页面输入数据。

我已经成功地将数据库中的所有字段作为输入值调用到我的表单中,并且我希望能够在提交之前使用保存按钮更新这些值。

我有两个基于 javascript 提供不同操作的表单按钮。

我没有收到任何错误,但是当我点击保存按钮时,没有条目提交到我的数据库。

我正在从数据库中提取唯一键(电子邮件地址)并尝试更新与唯一电子邮件地址所在行关联的所有字段。我将此字段作为隐藏输入添加到提交中当点击保存按钮时。

我试过 REPLACE, ON DUPLICATE KEY UPDATE 来更新条目,但它仍然什么都不做。这是我的代码:

<div style="float:left;  width:450px;">
<form id="parts" class="form" name="parts" method="post" ><br>
<input type="hidden" id="email" name="email" value="<? echo $_SESSION['MM_Username']; ?>">
<input type="hidden" id="first_name" name="first_name" value="<? echo $user['first_name']; ?>">
<input type="hidden" id="last_name" name="last_name" value="<? echo $user['last_name']; ?>">
<input type="hidden" id="business_name" name="business_name" value="<? echo $user['business_name']; ?>">
<input type="hidden" id="occupation" name="occupation" value="<? echo $user['occupation']; ?>">
<input type="hidden" id="business_address_1" name="business_address_1" value="<? echo $user['business_address_1']; ?>">
<input type="hidden" id="business_address_2" name="business_address_2" value="<? echo $user['business_address_2']; ?>">
<input type="hidden" id="country" name="country" value="<? echo $user['country']; ?>">
<input type="hidden" id="city" name="city" value="<? echo $user['city']; ?>">
<input type="hidden" id="state_province" name="state_province" value="<? echo $user['state_province']; ?>">
<input type="hidden" id="zip" name="zip" value="<? echo $user['zip']; ?>">
<input type="hidden" id="phone" name="phone" value="<? echo $user['phone']; ?>">
<input type="hidden" id="special_offers" name="special_offers" value="<? echo $user['special_offers']; ?>">

  <div id="div1">&nbsp;</div>
  <div id="div2">SET PART #</div>
  <div id="div3">INSTALL DATE</div>

  <div id="div1">1.</div>
  <div id="div2"><input type="text" id="part_1" name="part_1" value="<? echo $user['part_1']; ?>">     </div>
  <div id="div3"><input type="text" id="part_1_install_date" name="part_1_install_date" value="<? echo $user['part_1_install_date']; ?>"> </div>

  <div id="div1">2.</div>
  <div id="div2"><input type="text" id="part_2" name="part_2" value="<? echo $user['part_2']; ?>"></div>
  <div id="div3"><input type="text" id="part_2_install_date" name="part_2_install_date" value="<? echo $user['part_2_install_date']; ?>"></div>

  <div id="div1">3.</div>
  <div id="div2"><input type="text" id="part_3" name="part_3" value="<? echo $user['part_3']; ?>"></div>
  <div id="div3"><input type="text" id="part_3_install_date" name="part_3_install_date" value="<? echo $user['part_3_install_date']; ?>"></div>

  <div id="div1">4.</div>
  <div id="div2"><input type="text" id="part_4" name="part_4" value="<? echo $user['part_4']; ?>"></div>
  <div id="div3"><input type="text" id="part_4_install_date" name="part_4_install_date" value="<? echo $user['part_4_install_date']; ?>"></div>

  <div id="div1">5.</div>
  <div id="div2"><input type="text" id="part_5" name="part_5" value="<? echo $user['part_5']; ?>"></div>
  <div id="div3"><input type="text" id="part_5_install_date" name="part_5_install_date" value="<? echo $user['part_5_install_date']; ?>"></div>

  <div id="div1">6.</div>
  <div id="div2"><input type="text" id="part_6" name="part_6" value="<? echo $user['part_6']; ?>"></div>
  <div id="div3"><input type="text" id="part_6_install_date" name="part_6_install_date" value="<? echo $user['part_6_install_date']; ?>"></div>

  <div id="div1">7.</div>
  <div id="div2"><input type="text" id="part_7" name="part_7" value="<? echo $user['part_7']; ?>"></div>
  <div id="div3"><input type="text" id="part_7_install_date" name="part_7_install_date" value="<? echo $user['part_7_install_date']; ?>"></div>

  <div id="div1">8.</div>
  <div id="div2"><input type="text" id="part_8" name="part_8" value="<? echo $user['part_8']; ?>"></div>
  <div id="div3"><input type="text" id="part_8_install_date" name="part_8_install_date" value="<? echo $user['part_8_install_date']; ?>"></div>

  <div id="div1">9.</div>
  <div id="div2"><input type="text" id="part_9" name="part_9" value="<? echo $user['part_9']; ?>"></div>
  <div id="div3"><input type="text" id="part_9_install_date" name="part_9_install_date" value="<? echo $user['part_9_install_date']; ?>"></div>

  <div id="div1">10.</div>
  <div id="div2"><input type="text" id="part_10" name="part_10" value="<? echo $user['part_10']; ?>"></div>
  <div id="div3"><input type="text" id="part_10_install_date" name="part_10_install_date" value="<? echo $user['part_10_install_date']; ?>"></div>

  <div id="div1">&nbsp;</div>
  <div id="div2"><input type="submit" name="save" value="Save" onclick="submitForm('save.php')"></div>
  <div id="div3"><input class="send" id="register" type="submit" value="Submit" onclick="submitForm('submit.php')" disabled="disabled"></div>
</form>

<a href="logout.php">LOGOUT</a>

</div>

这是我的保存按钮 onclick 操作:

<?php
include("includes/connection.php");


$sql = "INSERT INTO login (email, first_name, last_name, business_name, occupation, business_address_1, business_address_2, country, city, state_province, zip, phone, special_offers, part_1, part_1_install_date, part_2, part_2_install_date, part_3, part_3_install_date, part_4, part_4_install_date, part_5, part_5_install_date, part_6, part_6_install_date, part_7, part_7_install_date, part_8, part_8_install_date, part_9, part_9_install_date, part_10, part_10_install_date)
VALUES ('$_POST[email]','$_POST[first_name]','$_POST[last_name]','$_POST[business_name]','$_POST[occupation]','$_POST[business_address_1]','$_POST[business_address_2]','$_POST[country]','$_POST[city]','$_POST[state_province]','$_POST[zip]','$_POST[phone]','$_POST[special_offers]','$_POST[part_1]', '$_POST[part_1_install_date]', '$_POST[part_2]', '$_POST[part_2_install_date]', '$_POST[part_3]', '$_POST[part_3_install_date]', '$_POST[part_4]', '$_POST[part_4_install_date]', '$_POST[part_5]', '$_POST[part_5_install_date]', '$_POST[part_6]', '$_POST[part_6_install_date]', '$_POST[part_7]', '$_POST[part_7_install_date]', '$_POST[part_8]', '$_POST[part_8_install_date]', '$_POST[part_9]', '$_POST[part_9_install_date]', '$_POST[part_10]', '$_POST[part_10_install_date]')
ON DUPLICATE KEY UPDATE login SET column = 'email' WHERE column='$_POST[email]'";

if (mysqli_query($connection, $sql)) {
echo header('Location: login-ENG.php');
} else {
 echo mysqli_error($connection);
}




mysqli_close($connection);

?>

这里是区分按钮功能的javascript:

<script>
function submitForm(action)
{
    document.getElementById('parts').action = action;
    document.getElementById('parts').submit();
}
</script>

【问题讨论】:

  • 在打开 &lt;?php 标签 error_reporting(E_ALL); ini_set('display_errors', 1); 后立即将错误报告添加到文件顶部,看看它是否会产生任何结果。另外,你开始会议了吗?
  • javascript 是什么样的?而且你有一个 sql 注入问题。
  • 如果你使用了error reporting,你会把“未定义的索引......”扔到无穷大......甚至更远。另外,请确保启用短标签。
  • 该页面已启动会话,我已将页面设置为您无法访问它,除非您先登录,否则它会将您踢回登录页面。我尝试添加错误报告,但它没有显示任何内容,因为成功会将您导航到另一个页面。我相信它认为提交是成功的,因此它不会显示任何错误。 @弗雷德-ii-
  • @Fred -ii- 我根据 Marc 的回答更新了我的代码,但现在根本没有提交到数据库

标签: php mysql database forms


【解决方案1】:

您的所有表单域都没有name 属性。没有name=...,就没有什么可提交的。 id 属性与表单提交完全无关。一个简单的var_dump($_POST) 就会向您展示这一点。

<input type="text" name="email" ... />
                   ^^^^^^^^^^^^
                        |
     VALUES ('$_POST[email]','$

同样,你很容易受到sql injection attacks

【讨论】:

  • 别忘了,字符串内的数组索引仅在您使用 {} 括起变量时才有效
  • @ITroubs:无关紧要。 OP不是。 "$arr[foo]" - 非常好。 "$arr['foo']" - 错了。 "{$arr['foo']}" - 很好。
  • 它不仅容易受到 SQL 注入攻击,还容易受到 XSS 攻击。
  • 提前 5 分钟告诉 OP 使用错误报告;纳达。那会抛出未定义的索引。嗯,你能做什么。
  • 哇,真不敢相信我错过了,呃。在向所有字段添加名称属性后,数据库不再添加空白条目,但它现在什么都不做。我也没有看到任何错误。我在上面更新了我的代码。 @Marc B
猜你喜欢
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 2010-10-04
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多