动态行数据未插入数据库

时间:2015-04-13 12:06:45

标签: javascript php

我通过选择框动态添加一行。我面临以下代码的三个问题

  1. 我可以创建新行,但是它的顺序与(3,2,1)相反,而不是(1,2,3)。
  2. 其次,当我选择另一个数字来创建该行数时,则之前的数据不会被删除,而是将新数据附加到其上。
  3. 第三,动态行数据未插入数据库。
    
    
    function addrow(x){
               alert(x);
    
            for (var l=1; l <= x-1; l++){
                                       $(function() {
       var k=4;
                                                  //  var addDiv1 = $('#table_row');
                                                //  var k = $('#table_row p').size() + 1;
    var index = 4;
    newRow = "<tr>" +
                    "<td><label>Select Pagelayout"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "SELECT name FROM layout";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                         $select4 .= "<option  value='select'>". select ."</option>";
                                         while($row = $result->fetch_assoc()) {
                                          $select4 .= "<option>".$row['name']."</option>";
                                                  }
                                        echo "<select name='layout.'l'.' id='layout.'l'.'>".$select4."</select>";
                          mysqli_close($dbLink);
                            }?></td></tr>"+
                    "<tr><td><label>Select Data"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select file_name from data_file";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select5 .= "<option  value='select'>Select</option>";
                                        while($row = $result->fetch_assoc()) {
                                        $select5 .= "<option>".$row['file_name']."</option>";
                                                                   }
                                    echo "<select name='data.'l'.' id='data.'l'.'>".$select5."</select>";  
                          mysqli_close($dbLink);
                            }?></td></tr>";
    
    
    $('#myTable > tbody > tr').eq(k).after(newRow);
         //  $('#myTable > tbody > tr').eq(k-1).after(newRow);
    
        return false;
               
                                                                    });
                
            }  scrnselection.h.value=l;  
               alert(l);
                           
              
                 }
     
    &#13;
        
    >    if(isset($_POST['submit']))
    >    {
            $screen_name = $_REQUEST['multiname'];
            $screen_scr = $_POST['scr'];
            $screen_header = $_POST['header'];
        //$screen_layout = $_POST['layout'];
       // $screen_data =  $_POST['data'];
        $screen_footer = $_POST['footer'];
        $num = $_POST['h'];
    
       echo $num;
    
        
       $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
       if(mysqli_connect_errno()) {
         die("MySQL connection failed: ". mysqli_connect_error());
                                                }
    $sql="SELECT screen_name from `multi_screen` WHERE name='$screen_name'";
         $check=mysqli_query($dbLink, $sql);
    
    
     if(mysqli_num_rows($check)>=1)
      
    $error= "Name already exist!";
    
    
    else
      { 
     for($i=0;$i<=$num;$i++){
    if(isset($_REQUEST['layout$i']))
    {
       $screen_layout = $_POST['layout$i'];
    
       echo $screen_layout;
    }
    if(isset($_REQUEST['data$i']))
    { 
            $screen_data = $_REQUEST['data$i'];
    } 
     $sql1="INSERT INTO `multi_screen` (screen_name, screen_scr, screen_header, screen_layout, screen_footer) VALUES ('$screen_name','$screen_scr','$screen_header','$screen_layout','$screen_footer')";
    
       $result1 = mysqli_query($dbLink, $sql1) or die (mysqli_error($dbLink));
    
         if (!$result1)
    		{
    		 die('Error: ' . mysqli_error());
      echo "<script type=\"text/javascript\" >  alert(\"Problem ocuured with server. Please try again later.\");   </script>";
    				}
    
       else $accept= "Data Inserted !Continue with the process.";
    }
    
     mysqli_close($dbLink);
      //   header("Location: layout.php");
      }
    }
    &#13;
    <form name="scrnselection" method="post" onsubmit="return validate();">
                <center>
              <?php if(!empty($error)) echo '<div class="error"><b>'.$error.'</b></div>'; ?>  
                <?php if(!empty($accept)) echo '<div class="accept"><b>'.$accept.'</b></div>'; ?> 
    
            <input name="h" type="hidden" id="h" value="0" />
            <label id="lab" style="visibility:hidden;"></label> <br/> 
                <table id="mytable">
                    <tr>
                        <td>
                            <label for="multiname">Enter the name of screen:</label>
                        </td>
                        <td>
                            <input type="text" name="multiname" id="name" value="" />
                        </td>
                    </tr>
                         
                   <tr>
                        <td>
                            <label for="scrn">Select no. of screen:</label>
                        </td>
                        <td>
                           <select name="scr" id="scr" onchange="addrow($(this).val());">
                                    <option value="select" selected>select</option>
      				<option value="2">2</option>
      				<option value="3">3</option>
      				<option value="4">4</option>
      			</select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="header">Select Header:</label>
                        </td>
                        <td>
                             <?php
                            $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select header_name from screen_header";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select .= "<option>".$row['header_name']."</option>\n";
                                                                   }
                                    echo "<select name='header' id='header_file'>".$select."</select>";  
                          mysqli_close($dbLink);
                            }
                                ?>            
                        </td>
                    </tr>
                     <tr>
                                <td>
                            <label for="pagelayout">Select Pagelayout:</label>
                        </td>
                        <td>
                            <?php
    
                              $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                      else {
                                        $sql="SELECT name FROM layout";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select1 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select1 .= "<option>".$row['name']."</option>\n";
                                           }
                                        echo "<select name='layout[]' id='layout_file'>".$select1."</select>";  
                               mysqli_close($dbLink);
                              }   
                            ?>       
                        </td>
                    </tr>
                   
                   <tr>
                        <td>
                            <label for="data">Select Data:</label>
                        </td>
                        <td>
    
                            <?php
                                $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select file_name from data_file";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select2 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select2 .= "<option>".$row['file_name']."</option>\n";
                                                                   }
                                    echo "<select name='data{}' id='data_file'>".$select2."</select>";  
                           mysqli_close($dbLink);
                            }
                                ?> 
    
                      </td>
                    </tr>
                   <tr>
                        <td>
                            <label for="footer">Select Footer:</label>
                        </td>
                        <td><?php
                               $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select footer_name from screen_footer";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select3 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select3 .= "<option>".$row['footer_name']."</option>\n";
                                                                   }
                                    echo "<select name='footer' id='footer_file'>".$select3."</select>";  
                          mysqli_close($dbLink);
                            }
                           ?>
                        </td>
                    </tr>
                </table>
               
                <input type="submit" value="Save" name="submit" id="submit" />
                   </center>
                </form>
    &#13;
    &#13;
    &#13;
  4. &#13;
    &#13;
    <script>
     function addrow(x){
               alert(x);
    
            for (var l=1; l <= x-1; l++){
                                       $(function() {
       var k=4;
                                                  //  var addDiv1 = $('#table_row');
                                                //  var k = $('#table_row p').size() + 1;
    var index = 4;
    newRow = "<tr>" +
                    "<td><label>Select Pagelayout"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "SELECT name FROM layout";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                         $select4 .= "<option  value='select'>". select ."</option>";
                                         while($row = $result->fetch_assoc()) {
                                          $select4 .= "<option>".$row['name']."</option>";
                                                  }
                                        echo "<select name='layout.'l'.' id='layout.'l'.'>".$select4."</select>";
                          mysqli_close($dbLink);
                            }?></td></tr>"+
                    "<tr><td><label>Select Data"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select file_name from data_file";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select5 .= "<option  value='select'>Select</option>";
                                        while($row = $result->fetch_assoc()) {
                                        $select5 .= "<option>".$row['file_name']."</option>";
                                                                   }
                                    echo "<select name='data.'l'.' id='data.'l'.'>".$select5."</select>";  
                          mysqli_close($dbLink);
                            }?></td></tr>";
    
    
    $('#myTable > tbody > tr').eq(k).after(newRow);
         //  $('#myTable > tbody > tr').eq(k-1).after(newRow);
    
        return false;
               
                                                                    });
                
            }  scrnselection.h.value=l;  
               alert(l);
                           
              
                 }
    &#13;
    <form name="scrnselection" method="post" onsubmit="return validate();">
                <center>
              <?php if(!empty($error)) echo '<div class="error"><b>'.$error.'</b></div>'; ?>  
                <?php if(!empty($accept)) echo '<div class="accept"><b>'.$accept.'</b></div>'; ?> 
    
            <input name="h" type="hidden" id="h" value="0" />
            <label id="lab" style="visibility:hidden;"></label> <br/> 
                <table id="mytable">
                    <tr>
                        <td>
                            <label for="multiname">Enter the name of screen:</label>
                        </td>
                        <td>
                            <input type="text" name="multiname" id="name" value="" />
                        </td>
                    </tr>
                         
                   <tr>
                        <td>
                            <label for="scrn">Select no. of screen:</label>
                        </td>
                        <td>
                           <select name="scr" id="scr" onchange="addrow($(this).val());">
                                    <option value="select" selected>select</option>
      				<option value="2">2</option>
      				<option value="3">3</option>
      				<option value="4">4</option>
      			</select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="header">Select Header:</label>
                        </td>
                        <td>
                             <?php
                            $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
                        // Check connection
                                if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select header_name from screen_header";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select .= "<option>".$row['header_name']."</option>\n";
                                                                   }
                                    echo "<select name='header' id='header_file'>".$select."</select>";  
                          mysqli_close($dbLink);
                            }
                                ?>            
                        </td>
                    </tr>
                     <tr>
                                <td>
                            <label for="pagelayout">Select Pagelayout:</label>
                        </td>
                        <td>
                            <?php
    
                              $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                      else {
                                        $sql="SELECT name FROM layout";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select1 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select1 .= "<option>".$row['name']."</option>\n";
                                           }
                                        echo "<select name='layout[]' id='layout_file'>".$select1."</select>";  
                               mysqli_close($dbLink);
                              }   
                            ?>       
                        </td>
                    </tr>
                   
                   <tr>
                        <td>
                            <label for="data">Select Data:</label>
                        </td>
                        <td>
    
                            <?php
                                $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select file_name from data_file";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select2 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select2 .= "<option>".$row['file_name']."</option>\n";
                                                                   }
                                    echo "<select name='data{}' id='data_file'>".$select2."</select>";  
                           mysqli_close($dbLink);
                            }
                                ?> 
    
                      </td>
                    </tr>
                   <tr>
                        <td>
                            <label for="footer">Select Footer:</label>
                        </td>
                        <td><?php
                               $dbLink = new mysqli('localhost', 'root', 'root', 'display');
                                    
                                     if(mysqli_connect_errno()) {
                                          die("MySQL connection failed: ". mysqli_connect_error());
                                                }
                                else{              
                                      $sql = "select footer_name from screen_footer";
                                        $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
                                        $select3 .= "<option  value='select'>Select</option>\n";
                                        while($row = $result->fetch_assoc()) {
                                        $select3 .= "<option>".$row['footer_name']."</option>\n";
                                                                   }
                                    echo "<select name='footer' id='footer_file'>".$select3."</select>";  
                          mysqli_close($dbLink);
                            }
                           ?>
                        </td>
                    </tr>
                </table>
               
                <input type="submit" value="Save" name="submit" id="submit" />
                   </center>
                </form>
    &#13;
    &#13;
    &#13;

    >    if(isset($_POST['submit']))
    >    {
            $screen_name = $_REQUEST['multiname'];
            $screen_scr = $_POST['scr'];
            $screen_header = $_POST['header'];
        //$screen_layout = $_POST['layout'];
       // $screen_data =  $_POST['data'];
        $screen_footer = $_POST['footer'];
        $num = $_POST['h'];
    
       echo $num;
    
    
       $dbLink = new mysqli('localhost', 'root', 'root', 'display');
    
       if(mysqli_connect_errno()) {
         die("MySQL connection failed: ". mysqli_connect_error());
                                                }
    $sql="SELECT screen_name from `multi_screen` WHERE name='$screen_name'";
         $check=mysqli_query($dbLink, $sql);
    
    
     if(mysqli_num_rows($check)>=1)
    
    $error= "Name already exist!";
    
    
    else
      { 
     for($i=0;$i<=$num;$i++){
    if(isset($_REQUEST['layout$i']))
    {
       $screen_layout = $_POST['layout$i'];
    
       echo $screen_layout;
    }
    if(isset($_REQUEST['data$i']))
    { 
            $screen_data = $_REQUEST['data$i'];
    } 
     $sql1="INSERT INTO `multi_screen` (screen_name, screen_scr, screen_header, screen_layout, screen_footer) VALUES ('$screen_name','$screen_scr','$screen_header','$screen_layout','$screen_footer')";
    
       $result1 = mysqli_query($dbLink, $sql1) or die (mysqli_error($dbLink));
    
         if (!$result1)
            {
             die('Error: ' . mysqli_error());
      echo "<script type=\"text/javascript\" >  alert(\"Problem ocuured with server. Please try again later.\");   </script>";
                    }
    
       else $accept= "Data Inserted !Continue with the process.";
    }
    
     mysqli_close($dbLink);
      //   header("Location: layout.php");
      }
    }
    

0 个答案:

没有答案