将用户输入的值从php表单更新到数据库

时间:2017-04-10 05:51:43

标签: php html mysql

Screenshot

我有4个名为offerId, AffId,状态和扣除的文本框,它从数据库中获取数据并显示5行。有一个"更新条目"按钮与表单的每一行,以便如果用户编辑文本框的数据并点击更新按钮,它会更新'状态' &安培; '扣'反对offerIdAffId

我面临的问题是我的脚本更新数据会获取最后一行offerIdAffId的值(显示在表单中),并更新状态并扣除它。如何使用按下按钮的输入值,并在数据库中更新它?

我的表单脚本和将表单更新到数据库的脚本如下所示:

<?php

include('adodb/adodb.inc.php');

echo '<h1>Mxpresso Revenue Management Solution</h1>';

echo '<img src="http://mxpresso.com/images/logo.png" alt="mxpresso logo" style="width:171px;height:108px;">';

echo '<h2>1. To See existing records</h2>';

$db=NewADOConnection('mysql');$db->Connect("127.0.0.1", "vcc", "abc", "vcc");

$sql="select * from rev";
$result = $db->Execute($sql);
if ($result === false) die("failed2");
$records=array();
$count=$result->RecordCount();
echo "Total Records Found :".$count."<br>";
echo '<form action="rev_insert.php" > 
       <input type="submit" value="Add Entry">
       </form>';

if($count > 0) {
    for ($x=0;$x<$count;$x++) {
        $offerId=$result->fields[0];
        $affId=$result->fields[1];
        $status=$result->fields[2];
        $deduction=$result->fields[3];

       echo '<form target="_blank" action="updatecopy.php" method="get">

        <table id="dataTable" class="form" border="1">
                  <tbody><tr>  
                      <p>
                        <td><input type="checkbox" required="required" name="chk[]" checked="checked"></td>
                        <td>
                        <h4>OfferID</h4>
                         <input type="text" name="offerId" value='.$offerId.' >

                         </td>
                         <td>
                         <h4>ffId</h4>
                            <input type="text" name="affId" value='.$affId.' >

                         </td>
                         <td>
                         <h4>deduction:</h4>
                            <input type="text" name="deduct" value='.$deduction.' >

                         </td>
                         <td>
                            <h4>Status</h4>
                            <input type="text" name="status" value='.$status.' >
                            </select>
                         </td>
                        </p>
                    </tr>
                    </tbody>
                </table>

                <input type="submit" value="Update Entry">';
        $rec=array("offerId"=>$offerId,"affiliate_id"=>$affId,"status"=>$status, "deduction"=>$deduction);
        array_push($records,$rec);
        $result->MoveNext();
        }
 }
?>

更新的脚本:

<?php
include('adodb/adodb.inc.php');
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else
 {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$url=curPageURL();
$str = parse_url($url, PHP_URL_QUERY );
parse_str($str);

$db=NewADOConnection('mysql');
//$db->debug = true;
echo "created DB connection";
$db->Connect("127.0.0.1", "vcc", "abc", "vcc");

$sql="update rev set deduction=".$deduct.", status=".$status." where offerId='".$offerId."' and affId='".$affId."';";
echo "SQL Query:".$sql;
 $result = $db->Execute($sql);
 if ($result === false) die("Failed to Update. Check if OfferId and AffID are correct");
?>

2 个答案:

答案 0 :(得分:1)

确保您获取所有行,并在&#34; $ result =&amp; $ db-&gt;执行($ sql); &#34;

而不是For循环尝试While循环,

&

答案 1 :(得分:1)

在为所有行创建单独的表单时。您可以更改URL或只是添加两个隐藏的输入,这些输入将在您单击提交按钮时提交。 然后在脚本中使用它来标识要更新的行。

修正 -

在HTML端:

1.关闭表格标签。

2.如果添加额外的输入字段+一些JS:

<input type='hidden' name='update_for_offerid' value='<?php echo $offId?>'>
<input type='hidden' name='update_for_affid' value='<?php echo $affId?>'>

如果只是更改提交网址:

echo '<form target="_blank" action="updatecopy.php&update_for_offerid=$offId&update_for_affid=$affId" method="get">

在脚本方面: (只使用GET中传递的变量,而不是循环变量)

$sql="update rev set deduction=".$_GET['deduct'].", status=".$_GET['status']." where offerId='".$_GET['update_for_offerid']."' and affId='".$_GET['update_for_affid']."';"

希望它能解决你的问题,试着告诉我。