尝试更新数据库,但表单仅插入空白条目
Trying to update database but the form only inserts blank entries
我有一个站点,用户可以在其中注册和登录,然后转到一个页面以输入数据。
我已经成功地将数据库中的所有字段作为输入值调回我的表单,我希望能够在提交前使用保存按钮更新这些值。
我有两个表单按钮提供基于 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"> </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"> </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>
这是我的保存按钮点击操作:
<?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>
NONE 个表单域具有 name
个属性。没有 name=...
,就没有什么可以提交的。 id
属性与表单提交完全无关。一个简单的 var_dump($_POST)
就会告诉你这个。
<input type="text" name="email" ... />
^^^^^^^^^^^^
|
VALUES ('$_POST[email]','$
此外,您很容易受到 sql injection attacks
我有一个站点,用户可以在其中注册和登录,然后转到一个页面以输入数据。
我已经成功地将数据库中的所有字段作为输入值调回我的表单,我希望能够在提交前使用保存按钮更新这些值。
我有两个表单按钮提供基于 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"> </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"> </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>
这是我的保存按钮点击操作:
<?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>
NONE 个表单域具有 name
个属性。没有 name=...
,就没有什么可以提交的。 id
属性与表单提交完全无关。一个简单的 var_dump($_POST)
就会告诉你这个。
<input type="text" name="email" ... />
^^^^^^^^^^^^
|
VALUES ('$_POST[email]','$
此外,您很容易受到 sql injection attacks