2017-08-23 5 views
2

私は、データテーブルのすべてのレコードを更新し、htmlのクリックボタンで実行する必要があるphp関数を持っています。ボタンをクリックしてphpを実行すると正しくない

このような私のPHPの機能を見て:

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

、私はこの外観を実行しようとHTMLでの私のフォーム:私は、更新データをしようとすると

<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> 

は、最終的にそれが表にのみ最初のレコードを更新残りは変わらない。
誰が何が間違っているのかを知っていて、どのように正しいのか考えていますか?私は助けに感謝します!

+0

があなたのJSコードも –

+0

が正しくPHP、HTMLビュー、およびJSコードを表示する表示し、それが不完全に見える –

+0

'onclick =" document.getElementById( 'ed')で使用したsubmit関数はどこにありますか?submit(); "'? –

答えて

1

同じidのフォームが多すぎます。 <form>で表を囲んでforeach<tr>に限定してください。この

<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> 

よう 何かあなたがいるのでうまく動作しません各行について、各フォームを作成方法:あなたが唯一の最初のフォームが影響を受けているgetElementByIdを行うときに(1)すべてのフォームは、同じジャバスクリプトidを持っています、( 2)その1つのフォームを送信すると、ページがリロードされ、他の行に対するすべての変更が失われます。 1つの解決策は、フォームを1つだけ作成し、すべてのフィールドに異なる名前を付けることです。フォームフィールドはnamevalueで送信されるので、すべてのフィールドに異なる名前が必要で、実際にIDは必要ありません。

フォームのどこかでカウントし、このようなものにするためのPHPを変更行を追加することができます。

$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(); 
     } 
    } 
+0

ループでは、id_dataに応じてフォーム内のすべての変更が送信されるため、動作しません。私は1つのフォームボタンを更新しないでください。 – Monika

+0

各反復で入力フィールドの名前を変更する必要があります。 '$ a'変数を入力名に連結することができます。今はその変数を使用しません。それに応じて、サーバー側のPHPを変更する必要があります。 – Dmitry

+0

私は$ aと増分 – Monika

0

これは、すべてのhtmlファイル

<!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> 

とPHPの一部です:

<?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"); 

    } 

?> 

そして、それは、更新データについてのすべてです。 phpとhtmlは1つのファイルにありますが、私はそれが不道徳に見えることを知っています。私はこのようにしません。

+2

ちょうど別の注意として、あなたは答えに追加のコードを投稿してはいけません。最初の質問を編集してください。 – Chris

関連する問題