【问题标题】:Using php how can I update a row in mysql database based on a forms input value?使用 php 如何根据表单输入值更新 mysql 数据库中的一行?
【发布时间】:2015-03-19 01:52:06
【问题描述】:

我有一个包含各种输入的表单。其中一个输入包含一个预先确定的值,该值由名为“invoiceid”的脚本加载。这个 invoiceid 是表单输入的名称和 id,也是我的数据库中列的名称。如何更新我的数据库中在 invoiceid 列下包含相同值的行并提交所有其他表单数据?我不知道我在做什么......请帮忙。感谢您的宝贵时间。

表格

<form action="update.php" id="contactForm" method="post">

<input id="invoiceid" name="invoiceid" type="hidden" value=""/>

<input id="txt1" name="txt1" type="text" value=""/>

<input id="q1" name="q1" value="9.50" checked="checked" type="radio">
<input id="q1" name="q1" value="12.50" type="radio">

<select id="selectbox" name="selectbox">
<option selected="selected" value="">Please select...</option> 
 <option value="PURCHASE">Order for Purchase</option>
 <option value="REVIEW">Order for Review</option>
</select>

<button id="btn1" type="submit" name="submit">Submit</button></div>   

</form>

update.php

//Table name: seguin_orders

<?php

// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");


// Check if the connection failed
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  die();
}

   if (isset($_GET['invoiceid']))
{
    $invoiceid= $_GET['invoiceid'];



}
?>

【问题讨论】:

  • 如果你使用POST方法提交表单,那么你必须使用$_POST来获取值。例如 $invoiceid = $_POST['invoiceid']
  • 另外值得注意的是,将原始$_POST 数据输入数据库会使您容易受到 SQL 注入的影响,这可能导致数据库泄露(或更糟)。一定要事先熟悉this article,这样你就会知道如何让你的程序更安全:)

标签: php forms mysqli


【解决方案1】:

我认为它可以帮助你。

update.php

<?php
   // Create the connection to the database
   $con=mysqli_connect("xxx","xxx","xxx","xxx");

  // Check if the connection failed
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
   }

  if (isset($_POST['invoiceid']))
  {
   $invoiceid= $_POST['invoiceid'];
   $column1 = $_POST['txt1']; 
   $column2 = $_POST['q1'];
   $column3 = $_POST['selectbox'];
   $sql = "UPDATE TableName SET column1='".$column1."', column2='".$column2."', column3='".$column3."' WHERE invoiceid='".$invoiceid."'";
  }
?>

///note: change the table name and column names according to your database

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2017-09-05
    • 2012-07-19
    相关资源
    最近更新 更多