2

I have php function which update all records in data table and need it run by click button in html.

My php function look like this:

<?php
try {
  $sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';
  $s = $pdo->prepare($sql);
  $s->execute();
} catch (PDOException $e) {
  $error = 'Error with select data' . $e->getMessage();
  include 'error.html.php';
  exit();
}

while ($row = $s->fetch()) {
  $dane[] = array(
    'id_data' => $row['id_data'],
    'date_record' => $row['date_record'],
    'value1' => $row['value1'],
    'value2' => $row['value2'],
    'value3' => $row['value3']
  );
}


if (isset($_GET['edytion'])) {
  foreach ($data as $data2) {
    try {
      $sql = 'UPDATE data SET date_record = :date_record, value1 = :value1, value2 = :value2, value3 = :value3 WHERE id_data= :id_data';
      $s = $pdo->prepare($sql);
      $s->bindValue(':date_record', $_POST['date_record']);
      $s->bindValue(':value1', $_POST['value1']);
      $s->bindValue(':value2', $_POST['value2']);
      $s->bindValue(':value3', $_POST['value3']);
      $s->bindValue(':id_data', $_POST['id_data']);
      $s->execute();
    } catch (PDOException $e) {
      $error = 'Edit data error  ' . $e->getMessage();
      include 'error.html.php';
      exit();
    }
  }

  Header("Location: theme3.php");
}
?>

And my form in html where i try run this look:

<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Date</th>
        <th>Value 1</th>
        <th>Value 2</th>
        <th>Value 3</th>
      </tr>
    </thead>
    <?php if (isset($data)): ?>
      <?php foreach ($data as $data1): $a = 0 ?>
        <form action="?edytion" method="post" id='ed'>
          <tr class="bg-primary">
            <input type="hidden"  name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
            <td><input type="date"  name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
            <td><input type="text"  name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
            <td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
            <td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
            <!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->
          </tr>
        </form>
        <?php $a++;
      endforeach; ?>
    <?php endif; ?>
    </tbody>
  </table>
  <input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>

Ultimately when i try update data it update only first record in table, rest of them is invariable.
Anybody know what is wrong and have idea how correct it? I will be grateful for help!

5
  • show your js code also Commented Aug 23, 2017 at 7:19
  • show your php, html view, and js code properly, it looks incomplete Commented Aug 23, 2017 at 7:21
  • Where is your submit function which you used in onclick="document.getElementById('ed').submit();"? Commented Aug 23, 2017 at 7:38
  • Chris i check this result and it write all id_data correctly Commented Aug 23, 2017 at 7:40
  • Nana I dont have submit function, i was thinking it was buitd in <form> specification. Commented Aug 23, 2017 at 7:53

2 Answers 2

1

You create too many forms with the same id. Surround the table with the <form> and do foreach on <tr> only. Something like this

<div class="table-responsive">
 <form action="?edytion" method="post" id='ed'>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Date</th>
        <th>Value 1</th>
        <th>Value 2</th>
        <th>Value 3</th>
      </tr>
    </thead>
    <?php if (isset($data)): ?>
      <?php $a = 0; foreach ($data as $data1): ?>

          <tr class="bg-primary">
            <input type="hidden"  name="id_data<?php echo $a; ?>" id="id_data" value="<?php echo $data1['id_data']; ?>" />
            <td><input type="date" name="date_record<?php echo $a; ?>" value="<?php echo $data1['date_record']; ?>"> </td>
            <td><input type="text" name="value1<?php echo $a; ?>" value="<?php echo $data1['value1']; ?>"> </td>
            <td><input type="text" name="value2<?php echo $a; ?>" value="<?php echo $data1['value2']; ?>"> </td>
            <td><input type="text" name="value3<?php echo $a; ?>" value="<?php echo $data1['value3']; ?>"> </td>
          </tr>

        <?php $a++;
      endforeach; ?>
    <?php endif; ?>
    </tbody>
  </table>
  <input name="row_count" value="<?php echo isset($a) ? $a : 0; ?>" type="hidden"/>
  </form>
  <input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>

The way you created each form for each row will not work well because: (1) all your forms have the same javascript id, when you do getElementById only the first form is affected, (2) when you submit that one form the page reloads and all the changes to other rows are lost. One solution is to make only 1 form and have different name to all fields. Forms fields are sent by name and value so you need different names for all fields and you don't really need ids.

You can add row count somewhere in the form and then change php to something like this:

$row_count = $_POST['row_count'];
for($i = 0; i < $row_count; i++) {
try {
            $sql = 'UPDATE data SET
                            date_record = :date_record,
                            value1 = :value1,
                            value2 = :value2,
                            value3 = :value3
                         WHERE id_data= :id_data';

            $s = $pdo->prepare($sql);
            $s->bindValue(':date_record', $_POST['date_record' . $i]);
            $s->bindValue(':value1', $_POST['value1' . $i]);
            $s->bindValue(':value2', $_POST['value2' . $i]);
            $s->bindValue(':value3', $_POST['value3' . $i]);
            $s->bindValue(':id_data', $_POST['id_data' . $i]);
            $s->execute();
        } 
        catch (PDOException $e) {
            $error = 'Edit data error  ' . $e->getMessage();
            include 'error.html.php';
            exit();
        }
  }
Sign up to request clarification or add additional context in comments.

11 Comments

It will not work because in loop a send every change in forms depending on id_data. When i do one form button dont update.
You'll need to change the names of input fields in each iteration. You can concatenate the $a variable to the input name. You don't use that variable right now. You'll need to change the php on the server side accordingly.
I remove $a and incrementation
But i noticed when i dependence form id from $data1['id_data'] like this: <form action="?edytion" method="post" id='<?php echo $data1['id_data']; ?>'> And in input submit induce: <input type="submit" id="id_buttona" onclick="document.getElementById('<?php echo $data1[id_data]; ?>').submit();" value="Edit"/> Then it will hange last index. But how induce all id_data array?
You can make few forms with different ids but you'll need a submit button for each form. Also this does not solve the problem number (2) in my answer; you'll lose changes to other forms when you submit one form. And you can only submit one form at a time.
|
0

It is all html file

<!DOCTYPE HTML PUBLIC>
<html>
<head>
    <title>Tematyka2</title>

    <link rel="stylesheet" href="style.css" type="text/css">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>



</head>

<body>
<div id="container">
    <div id="heading"></div>
    <table border="0" width="766" cellpadding="0" cellspacing="0" align="center">

        <tr>
            <td class="t1"><p>Example title</p></td><td class="t2"> <a href="logout.php"><b class="linkW" style="padding-left: 60%">Logout</b></a></td>
        </tr>


        <tr>
            <td class="top1"><div class="inscription">Your Page</div></td>
            <td class="top2" valign="top"></td>
        </tr>

    </table>


    <table align="center" cellpadding="0">
        <tr>
            <td valign="top">
                <table cellpadding="0" cellspacing="0">
                    <tr><td class="topm">Main menu</td></tr>
                    <tr><td class="tlom">
                        :: <a href="index.php">Main page</a> <br>
                        </td></tr>

                    <tr><td class="dolm"></td></tr
                </table>

                <table cellpadding="0" cellspacing="0">
                    <tr><td class="topm">Them</td></tr>
                    <tr><td class="tlom">
                            :: <a href="them1.php">Them 1</a> <br>
                            :: <a href="them2.php"><b>Them 2</b></a> <br>
                            :: <a href="them3.php">Them 3</a> <br>
                        </td></tr>
                    <tr><td class="dolm"></td></tr
                </table>


                <table cellpadding="0" cellspacing="0">
                    <tr><td class="topm">Charts</td></tr>
                    <tr><td class="tlom">
                            :: <a href="">Chart1</a> <br>
                            :: <a href="">Chart2</a> <br>
                        </td></tr>
                    <tr><td class="dolm"></td></tr>
                </table>


                <br>
            </td>






            <td width="1"></td>
            <td valign="top">
                <table cellpadding="0" cellspacing="0">
                    <tr><td class="topn"></td></tr>
                    <tr><td class="tlon">


                            <div class="span7 center">
                                <h2>
                                    EDYTION
                                </h2>
                            </div>

                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Value 1</th>
                                        <th>Value 2</th>
                                        <th>Value 3</th>

                                    </tr>

                                    </thead>


                                    <?php if(isset($data)): ?>
                                        <?php foreach($data as $data1): $a=0?>
                                            <form action="?edytion" method="post" id='ed'>

                                                <tr class="bg-primary">


                                                    <input type="hidden"  name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
                                                    <td><input type="date"  name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
                                                    <td><input type="text"  name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
                                                    <td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
                                                    <td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
                                                   <!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->

                                                </tr>

                                            </form>
                                        <?php $a++; endforeach; ?>

                                    <?php endif; ?>





                                    </tbody>
                                </table>



                                <input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>


                            </div>


                    </td></tr>
                <!--  <tr><td class="doln"></td></tr> -->
            </table>
            </td>
    </tr>
</table>

<table align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td class="foot1"> My page</td>
        <td class="foot2"><a href="" title="szablony"><img src="images/dol2.jpg" alt="anything"></a></td>
    </tr>
</table>

And Php part:

<?php

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

include $_SERVER['DOCUMENT_ROOT'] . '/connection_to_database.php';

include $_SERVER['DOCUMENT_ROOT']. '/check_login.php';      try {
        $sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';

        $s = $pdo->prepare($sql);
        $s->execute();
    } catch (PDOException $e) {
        $error = 'Error with select data' . $e->getMessage();
        include 'error.html.php';
        exit();
    }

    while ($row = $s->fetch()) {
        $dane[] = array(
            'id_data' => $row['id_data'],
            'date_record' => $row['date_record'],
            'value1' => $row['value1'],
            'value2' => $row['value2'],
            'value3' => $row['value3']
        );
    }



    if (isset($_GET['edytion'])) {


        foreach ($data as $data2) {


            try {
                $sql = 'UPDATE data SET
                                date_record = :date_record,
                                value1 = :value1,
                                value2 = :value2,
                                value3 = :value3
                             WHERE id_data= :id_data';

                $s = $pdo->prepare($sql);
                $s->bindValue(':date_record', $_POST['date_record']);
                $s->bindValue(':value1', $_POST['value1']);
                $s->bindValue(':value2', $_POST['value2']);
                $s->bindValue(':value3', $_POST['value3']);
                $s->bindValue(':id_data', $_POST['id_data']);
                $s->execute();
            } 
            catch (PDOException $e) {
                $error = 'Edit data error  ' . $e->getMessage();
                include 'error.html.php';
                exit();
            }


        }

        Header("Location: theme3.php");

    }

?>

And that's all about update data. Php and html are in one file, I know it's look unprofessionally and i shoudln't do it in this way.

1 Comment

Just an aside note, you shouldn't post additional code in an answer, just edit the initial question instead

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.