0

Screenshot

I have 4 text boxes named offerId, AffId,status and deduction which is fetching data from the database and displaying say 5 rows. There is an "update entry" button with each row of the form, so that if a user edits the data of a text box and hits the update button, it updates the 'status' & 'deduction' against that offerId and AffId.

The problem I'm facing is my script which updates the data picks up the values of offerId and AffId of the last row (being displayed in the form), and updates the status and deduction against it. How do I use the entered value against which the button is pressed, and update it in the database?

The script of my form, and the script which updates my form to the database looks like this:

<?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();
        }
 }
?>

The script which updates:

<?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");
?>
3
  • Its time to use some JS ... Do you have any script associated with this code? If yes then can you post it? Commented Apr 10, 2017 at 5:58
  • You did not close the form tag in your first script please check you script. <input type="submit" value="Update Entry">'; $rec=array("offerId"=>$offerId,"affiliate_id"=>$affId,"status"=>$status, "deduction"=>$deduction); Commented Apr 10, 2017 at 5:59
  • No i don't. How will jS help ? Commented Apr 10, 2017 at 6:14

2 Answers 2

1

As you are creating separate form for all your rows. You can change the URL or simply add two hidden inputs which will get submitted when you click on the submit button. Then use it in your script to identify which row to update.

Fixes-

On HTML Side:

1.Close the form tag.

2.If adding extra input fields + Some JS:

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

If just changing the submit url:

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

On script side: (just use passed vars in the GET, not the variables of loop)

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

Hope it solves your issue, try and let me know.

Sign up to request clarification or add additional context in comments.

12 Comments

i don't know why, but it says file not found when i hit the update button, although the update file is present on the server. !!
I think there's something wrong with the form action. Can you check on that again. Also we made the type="Hidden', i need to show those offerId and affId, so the user knows which values of status and deduction are they changing.
Oh Sorry ... I didn't see the single quote, try this ... echo '<form target="_blank" action="updatecopy.php&update_for_offerid='.$offId.'&update_for_affid='.$affId.'" method="get">
yes i did that already, but i removed the <input type="hidden", coz of the reason mentioned above, could that be an issue ?
Its either the action url changes or the hidden input fields ... use only one of them. + the script changes. Check console and let me know if the request going on update.php with the appropriate affid and offid.
|
1

Make sure your fetching all rows, Make reference call at " $result = &$db->Execute($sql); "

Instead of For loop try While loop,

while (!$result->EOF) {
    $offerId=$result->fields[0];
    $affId=$result->fields[1];
    $status=$result->fields[2];
    $deduction=$result->fields[3];

    //Rest of your code....

    $result->MoveNext();  //  Moves to the next row
}

4 Comments

yes i am fetching all the rows, and they are being displayed in the textboxes well. Only i am not able to update those values.
The while loop did not really make a difference.
what its printing " echo "SQL Qeury:".$sql; " --- the last offerId?
So your update script is fine, the problem is where your printing the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.