0

こんにちは、私はphpを初めて使用し、追加ボタンが押されたときにテーブル行を動的に追加するフォームを作成しました。データベースに保存でき、ユーザーが編集をクリックしたときに値を保持できますが、私の問題は送信を押します。つまり、更新ボタンを押すと、以前のレコードが削除され、新しく追加された行だけが保存されます。または、変更を加えずに更新ボタンを押すと、動的に追加されたテーブルからすべての値が削除されます。データベースに挿入する前に削除コマンドを書きました以前のレコードをデータベースに削除して新しいレコードを保存する必要がありますが、レコードを削除し、新しく追加された行だけを保存します誰か助けてください。ここで何か間違っているのは、javascript と一緒に私のコードです

<?php
    session_start();
    if (!isset($_SESSION["username"])) {
        header("Location: unauthorize_access.php");
        }
        include("config.php");
        /*mysql_query("LOCK TABLES `makhtab` WRITE, `makhzim` WRITE, `makhalim` WRITE, `country` WRITE, `state` WRITE,  `district` WRITE, `tehsil` WRITE, `city` WRITE, `centre` WRITE, `qabiliyat` WRITE") or die(mysql_error());*/

        $newsid = mysql_real_escape_string($_REQUEST['code']); // is used for both $_GET/$_POST variables
    if(isset($_POST['submit']))
    {
             $fname     = mysql_real_escape_string($_POST['name']);
             $fadd1     = mysql_real_escape_string($_POST['add1']);
             $fadd2     = mysql_real_escape_string($_POST['add2']);
             $fadd3     = mysql_real_escape_string($_POST['add3']);
             $fpin      = mysql_real_escape_string($_POST['pin']);
             $ftel      = $_POST["contact"];
             $fcenter   = mysql_real_escape_string($_POST['cmbcen']);
             $fcity     = mysql_real_escape_string($_POST['cmbcity']);
             $fteh      = mysql_real_escape_string($_POST['cmbteh']);
             $fdist     = mysql_real_escape_string($_POST['cmbdist']);
             $fstate    = mysql_real_escape_string($_POST['cmbstate']);
             $fcoun     = mysql_real_escape_string($_POST['cmbcoun']);
             $fstu      = mysql_real_escape_string($_POST['stu']);
             $fbatch    = mysql_real_escape_string($_POST['batch']);

             mysql_query("UPDATE `makhtab` SET `name`='$fname', `add1`='$fadd1', `add2`='$fadd2', `add3`='$fadd3', `pin`='$fpin', 
                         contact='$ftel', `center`='$fcenter', `city`='$fcity', `tehsil`='$fteh', `district`='$fdist', `state`='$fstate', 
                        `country`='$fcoun', students='$fstu', batch='$fbatch'  WHERE `code`='$newsid' ") or die(mysql_error());

             $num =  $_POST['rr'];
             mysql_query("DELETE FROM `makhalim` WHERE makhcode ='$newsid'")or die(mysql_error());
            print'<script type="text/javascript">';
            print"alert('alim'.$num.)";
            print'</script>';
             for($i=1;$i<$num;$i++)
             {
                $mname       = $_POST["alimname_$i"];
                $mmob        = $_POST["alimmob_$i"];
                $mqab        = $_POST["qabil_$i"];
                if(!empty($mname)){
                    mysql_query("INSERT INTO `makhalim`(makhcode, srno, `name`, mobile, `qabiliyat`) Values('$newsid', '$i', '$mname', '$mmob', '$mqab')") or die(mysql_error());
                 }
              }
             $hh =  $_POST['hh'];
             mysql_query("DELETE FROM makhzim WHERE makhcode ='$newsid'")or die(mysql_error());
             if($hh > 0){
                for($h=1;$h<$hh;$h++)
                {          
                    $m_name       = $_POST["zimname_$h"];
                    $m_mob        = $_POST["zimmob_$h"];

                    if(!empty($m_name)){
                       mysql_query("INSERT INTO `makhzim`(makhcode, srno, `name`, mobile) Values('$newsid', '$h', '$m_name','$m_mob')") or die(mysql_error());
                    }
                }
              }    

              /*mysql_query("UNLOCK TABLES") OR die(mysql_error());*/

              print'<script type="text/javascript">';
              print "alert('Records Updated Sucessfully')";
              header("refresh: 2; makh_tab.php");
              print'</script>';

              }

              elseif($newsid)
              {

              $result = mysql_query("SELECT `name`, `add1`, `add2`, `add3`, pin, contact, `center`, `city`, `tehsil`, `state`, `district`, `country`, students, batch FROM makhtab WHERE code='$newsid' ",$connect);
              $myrow = mysql_fetch_assoc($result);
                    $cname      = $myrow["name"];
                    $cadd1      = $myrow["add1"];
                    $cadd2      = $myrow["add2"];
                    $cadd3      = $myrow["add3"];
                    $cpin       = $myrow["pin"];
                    $ccen       = $myrow["center"];
                    $ccity      = $myrow["city"];
                    $cteh       = $myrow["tehsil"];
                    $cstate     = $myrow["state"];
                    $cdist      = $myrow["district"];
                    $ccoun      = $myrow["country"];
                    $cstu       = $myrow["students"];
                    $cbatch     = $myrow["batch"];
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>Makhtab</title>
                <link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
                    <script type="text/javascript" src="form2/view.js"></script>
                    <script type="text/javascript" src="form2/calendar.js"></script>
                    <meta http-equiv="Content-Script-Type" content="text/javascript"> 

                        <script language="javascript" type="text/javascript">
                            var jj=1;
                            function addRow()
                            {
                                //alert(jj)
                                var tbl = document.getElementById('zimtable');
                                var lastRow = tbl.rows.length;
                                var iteration = lastRow - 1;
                                var row = tbl.insertRow(lastRow);

                                var firstCell = row.insertCell(0);
                                var el = document.createElement('input');
                                el.type = 'text';
                                el.name = 'zimname_' + jj;
                                el.id = 'zimname_' + jj;
                                el.size = 40;
                                el.maxlength = 40;
                                firstCell.appendChild(el);

                                var secondCell = row.insertCell(1);
                                var el2 = document.createElement('input');
                                el2.type = 'text';
                                el2.name = 'zimmob_' + jj;
                                el2.id = 'zimmob_' + jj;
                                el2.size = 13;
                                el2.maxlength = 13;
                                secondCell.appendChild(el2);


                                // alert(i);
                                jj++;
                                makhtab.hh.value=jj;
                                 alert(jj);
                            }
                        </script>
                        <script language="javascript" type="text/javascript">
                            var ii=1;
                            function addalRow()
                            {
                               // alert(ii);
                                var tbl = document.getElementById('alimtable');
                                var lastRow = tbl.rows.length;
                                var iteration = lastRow - 1;
                                var row = tbl.insertRow(lastRow);

                                var firstCell = row.insertCell(0);
                                var el = document.createElement('input');
                                el.type = 'text';
                                el.name = 'alimname_' + ii;
                                el.id = 'alimname_' + ii;
                                el.size = 40;
                                el.maxlength = 40;
                                firstCell.appendChild(el);

                                var secondCell = row.insertCell(1);
                                var el2 = document.createElement('input');
                                el2.type = 'text';
                                el2.name = 'alimmob_' + ii;
                                el2.id = 'alimmob_' + ii;
                                el2.size = 13;
                                el2.maxlength = 13;
                                secondCell.appendChild(el2);

                                var thirdCell = row.insertCell(2);
                                var element4 = document.createElement("select");
                                element4.name ='qabil_'+ii;
                                var option1 = document.createElement("option");
                                option1.value='';
                                option1.innerHTML='';
                                element4.appendChild(option1);

                                var option2 = document.createElement("option");
                                option1.value='MUFTI';
                                option1.innerHTML='MUFTI';
                                element4.appendChild(option1);

                                var option3 = document.createElement("option");
                                option2.value='AALIM';
                                option2.innerHTML='AALIM';              
                                element4.appendChild(option2);

                                var option4 = document.createElement("option");
                                option3.value='QARI';
                                option3.innerHTML='QARI';               
                                element4.appendChild(option3);

                                var option5 = document.createElement("option");
                                option4.value='HAFIZ';
                                option4.innerHTML='HAFIZ';              
                                element4.appendChild(option3);

                                thirdCell.appendChild(element4);

                                // alert(i);
                                ii++;
                                makhtab.rr.value=ii;
                                  alert(ii);
                            }
                        </script>
                        <script  type="text/javascript" src="tran/js/jquery-1.6.1.min.js"></script>
                        <script  type="text/javascript" src="tran/js/jquery-ui-1.8.14.custom.min.js"></script>

                        <script  type="text/javascript" src="tran/js/jq-ac-script.js"></script>
          <SCRIPT language=Javascript>
          <!--
          function isNumberKey(evt)
          {
             var charCode = (evt.which) ? evt.which : event.keyCode
             if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

             return true;
          }
          //-->
    </SCRIPT>

                        </head>
                        <body id="main_body" >

                            <div id="form_container">
                                <form id="makhtab" class="appnitro" enctype="multipart/form-data" method="POST"  onsubmit="return checkForm()">
                                    <div class="form_description">
                                        <br />
                                        <h2>Makhtab Details</h2>
                                    </div>

                               <table border="0px" width="100%">
                                            <tr>
                                                <td align="right"><label class="description" for="element_1">Code</label></td><td><input  name="code"  type="text" maxlength="11" size="11"  Placeholder="Please enter a code" value="<?php echo $newsid; ?>" disabled="disabled"/></td>
                                            </tr>
                                            <tr>
                                                <td align="right"><label class="description" for="element_1">Name</label></td><td><input id="name" name="name" onkeypress=""  tabindex="1"type="text" maxlength="40" size="40" value="<?php echo $cname; ?>"/></td></td>
                                            </tr>
                                            <tr>
                                                <td align="right"><label class="description" for="element_1">Address</label></td><td><input id="add1" name="add1"  type="text" maxlength="80" size="80"  value="<?php echo $cadd1; ?>"/></td></td>
                                            </tr>
                                            <tr>
                                                <td align="right"></td><td><input id="add2" name="add2"  type="text" maxlength="80" size="80"  value="<?php echo $cadd2; ?>"/></td>
                                            </tr>
                                            <tr>
                                                <td align="right"></td><td><input id="add3" name="add3"  type="text" maxlength="80" size="80"  value="<?php echo $cadd3; ?>"/></td></td>
                                            </tr>
                                             <tr>
                                                <td align="right"><label class="description" for="element_1">Pin</label></td><td><input id="pin"  name="pin"   type="text" maxlength="10" size="10"  value="<?php echo $cpin; ?>"/></td>
                                             </tr>
                                             <tr>
                                                <td align="right"><label class="description" for="element_1">Telephone</label></td><td><input id="contact"  name="contact"   type="text" maxlength="10" size="10"  value="8082034771"/></td>
                                            </tr>
                                         <tr>
                                                 <td align="right"><label class="description" for="element_1">Center</label></td><td><select name="cmbcen" class="element text medium" />
                                                 <option value="<?php echo $ccen; ?>"><?php echo $ccen; ?></option>
                                                <?php

                                                $result = mysql_query("SELECT code, `name` FROM centre") or die(mysql_error());

                                                while ($coun = mysql_fetch_array($result)) {

                                                    echo "<option value='$coun[name]'>$coun[name]</option>";
                                                }
                                                ?>
                            </select>
                                              </tr>
                                        <tr>

                                       <td align="right"><label class="description" for="element_1">City</label></td><td><select id="cmbcity" name="cmbcity" class="element text medium" style="font-size:14px;">
                                                <option value="<?php echo $ccity; ?>"><?php echo $ccity; ?></option>
                                               <?php

                                                $result = mysql_query("SELECT `name` FROM city ") or die(mysql_error());
                                                  while ($coun = mysql_fetch_array($result)) {
                                                          echo "<option value=".$coun['name'].">".$coun['name']."</option>";
                                                  }
                                                ?></select>
                                        </td>
                                        </tr>
                                     <tr>
                                        <td align="right"><label class="description" for="element_1">Tehsil</label></td><td><select id="cmbteh" name="cmbteh" class="element text medium" style="font-size:14px;">
                                                <option value="<?php echo$cteh; ?>"><?php echo$cteh; ?></option>
                                                <?php
                                                $result = mysql_query("SELECT  `name` FROM tehsil ") or die(mysql_error());
                                                   while($coun = mysql_fetch_array($result)) {
                                                    echo "<option value=".$coun['name'].">".$coun['name']."</option>";
                                                }
                                                ?></select>
                                        </td></tr>
                                                             <tr>
                                        <td align="right"><label class="description" for="element_1">District</label></td><td><select id="cmbdist" name="cmbdist" class="element text medium" style="font-size:14px;">
                                                <option value="<?php echo $cdist; ?>"><?php echo $cdist; ?></option>
                                                <?php
                                                $result = mysql_query("SELECT  `name` FROM district ") or die(mysql_error());
                                                   while($coun = mysql_fetch_array($result)) {
                                                    echo "<option value=".$coun['name'].">".$coun['name']."</option>";
                                                }
                                                ?></select>
                                        </td></tr>
                                        <tr>
                                        <td align="right"><label class="description" for="element_1">State</label></td><td><select id="cmbstate" name="cmbstate" class="element text medium" style="font-size:14px;">
                                                <option value="<?php echo $cstate; ?>"><?php echo $cstate; ?></option>
                                                <?php
                                                $result = mysql_query("SELECT  `name` FROM state ") or die(mysql_error());

                                                while ($coun = mysql_fetch_array($result)) {
                                                 echo "<option value=".$coun['name'].">".$coun['name']."</option>";
                                                 }
                                                ?></select>
                                        </td></tr>
                                                       <tr>
                                        <td align="right"><label class="description" for="element_1">Country</label></td><td><select id="cmbcoun" name="cmbcoun" class="element text medium" style="font-size:14px;">
                                                <option value="<?php echo $ccoun; ?>"><?php echo $ccoun; ?></option>
                                                <?php
                                                $result = mysql_query("SELECT  `name` FROM country ") or die(mysql_error());
                                                 while($coun = mysql_fetch_array($result)) {
                                                 echo "<option value=".$coun['name'].">".$coun['name']."</option>";a

                                                }
                                                ?></select>

                                        </td></tr>
                                  </table>          <br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
                                                    <div class="form_description">
                                                        <h2>Zimmedar Details</h2>
                                                    </div>

                                   <table  id="zimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
                                                    <tr>
                                                        <td><strong>Zimmedar Name</strong></td>
                                                        <td><strong>Mobile</strong> </td>
                                                    </tr>
                                  <?php

                                            $zim = mysql_query("SELECT * FROM `makhzim` WHERE makhcode='$newsid' ORDER BY srno")or die(mysl_error());
                                            $ctr= 0;
                                            while ($zrow = mysql_fetch_array($zim)){
                                                 $ctr++;
                                                       echo"<tr>";
                                                        echo "<td><input name='zimname_$ctr' type='text' id='zimname_$ctr' size='40' maxlength='20' value=$zrow[name] /></td>";
                                                        echo "<td><input name='zimmob_$ctr' type='text' id='zimmob_$ctr' size='13' maxlength='20' value=$zrow[mobile] /></td>";
                                                        echo "</tr>";
                                            }?>
                                       </table>
                                              <input type="button" value="Add" onclick="addRow();" /><input name="hh" type="hidden"  id="hh" value="<?php echo $ctr; ?>"/>

                                                    <br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
                                                    <div class="form_description">
                                                        <h2>Muallim Details</h2>
                                                        <!--<p>This is your form description. Click here to edit.</p>-->
                                                    </div>

                                       <table  id="alimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
                                                    <tr>
                                                        <td><strong>Muallim Name</strong></td>
                                                        <td><strong>Mobile</strong> </td>
                                                        <td><strong>Qabiliyat</strong> </td>

                                                    </tr>
                                                    <?php 
                                                    $alim = mysql_query("SELECT * FROM  `makhalim` WHERE makhcode='$newsid' ORDER BY srno") or die(mysql_error());

                                                    $mctr = 0;
                                                    while($arow = mysql_fetch_array($alim)){
                                                        $ctr++;
                                                        echo "<tr>";
                                                        echo "<td><input name='alimname_$mctr' type='text' id='alimname_$mctr' size='40' value=$arow[name] maxlength='20' /></td>";
                                                        echo "<td><input name='alimmob_$mctr' type='text' id='alimmob_$mctr' size='13' maxlength='20' value=$arow[mobile] /></td>";      
                                                        echo "<td><select id='qabil' name='qabil_$mctr' class='element text large' style='font-size:14px;'/>
                                                        <option value='$arow[qabiliyat]'>$arow[qabiliyat]</option>    
                                                           <option value=''></option>    
                                                            <option value='MUFTI'>MUFTI</option>
                                                            <option value='AALIM'>AALIM</option>
                                                            <option value='QARI'>QARI</option>
                                                            <option value='HAFIZ'>HAFIZ</option>
                                                            </select></td>";
                                                        echo"</tr>";
                                                    }
                                                    ?>
                                                </table>
                                                <input type="button" value="Add" onclick="addalRow();" /><input name="rr" type="hidden"  id="rr" value="<?php echo $mctr; ?>" />
                                                    <br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
                                                <table border="0px" width="85%">
                                                    <tbody><tr>
                                                            <td width="105"><label class="description">No. of Students</label></td>
                                                            <td width="65"><input type="text" name="stu" onkeypress="return isNumberKey(event)" size="5" maxlength="5" value="<?php echo $cstu; ?>"></input></td>
                                                            <td width="105"><label class="description">No. of Batches</label></td><td width="14"><input type="text" name="batch" onkeypress="return isNumberKey(event)" size="5" maxlength="3" value="<?php echo $cbatch; ?>"></input></td>
                                                        </tr>
                                                        <tr>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                                    <br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
                                                    <input type="submit" name="submit" value="Update">&nbsp;&nbsp;<input type="submit" name="close" value="Exit"/>
                                                     </form>
                                                    </div>
                                                </body>
                                                </html>

      <?php
    }
    if(isset($_POST['close'])){
        header("Location: makh_tab.php");
    }
    ?>
4

1 に答える 1

0

これが問題だと思います

  1. あなたの削除ステートメントは、主キーではないフィールドを対象としています

    mysql_query("DELETE FROM `makhalim` WHERE makhcode ='$newsid'") or die(mysql_error());
    AND 
    mysql_query("DELETE FROM makhzim WHERE makhcode ='$newsid'") or die(mysql_error());
    

makhcode は主キーではないため、同じコードのすべてのレコードを照合しています。

于 2013-01-30T13:31:06.817 に答える