473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

unable to insert dynamic row data to database

I am dynamically adding a row through select box. I am facing three problem with below code
1) I am able to create the new row but its in reverse order like (3,2,1) instead of (1,2,3).
2) Secondly, when i select another number to create that number of rows then then previous data don't delete instead new data append on it.
3) Thirdly dynamic rows data is not inserting into the database.

Please help me with this i'm very new to php and javascript. Thanks in advance


HTML CODE
--------

Expand|Select|Wrap|Line Numbers
  1.   <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
  2.                         $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
  3.                     // Check connection
  4.                             if(mysqli_connect_errno()) {
  5.                                       die("MySQL connection failed: ". mysqli_connect_error());
  6.                                             }
  7.                             else{              
  8.                                   $sql = "select header_name from screen_header";
  9.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  10.                                     $select .= "<option  value='select'>Select</option>\n";
  11.                                     while($row = $result->fetch_assoc()) {
  12.                                     $select .= "<option>".$row['header_name']."</option>\n";
  13.                                                                }
  14.                                 echo "<select name='header' id='header_file'>".$select."</select>";  
  15.                       mysqli_close($dbLink);
  16.                         }
  17.                             ?> </td> </tr> <tr> <td> <label for="pagelayout">Select Pagelayout:</label> </td> <td> <?php
  18.  
  19.                           $dbLink = new mysqli('localhost', 'root', 'root', 'display');
  20.  
  21.                                  if(mysqli_connect_errno()) {
  22.                                       die("MySQL connection failed: ". mysqli_connect_error());
  23.                                             }
  24.                                   else {
  25.                                     $sql="SELECT name FROM layout";
  26.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  27.                                     $select1 .= "<option  value='select'>Select</option>\n";
  28.                                     while($row = $result->fetch_assoc()) {
  29.                                     $select1 .= "<option>".$row['name']."</option>\n";
  30.                                        }
  31.                                     echo "<select name='layout[]' id='layout_file'>".$select1."</select>";  
  32.                            mysqli_close($dbLink);
  33.                           }   
  34.                         ?> </td> </tr> <tr> <td> <label for="data">Select Data:</label> </td> <td> <?php
  35.                             $dbLink = new mysqli('localhost', 'root', 'root', 'display');
  36.  
  37.                                  if(mysqli_connect_errno()) {
  38.                                       die("MySQL connection failed: ". mysqli_connect_error());
  39.                                             }
  40.                             else{              
  41.                                   $sql = "select file_name from data_file";
  42.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  43.                                     $select2 .= "<option  value='select'>Select</option>\n";
  44.                                     while($row = $result->fetch_assoc()) {
  45.                                     $select2 .= "<option>".$row['file_name']."</option>\n";
  46.                                                                }
  47.                                 echo "<select name='data{}' id='data_file'>".$select2."</select>";  
  48.                        mysqli_close($dbLink);
  49.                         }
  50.                             ?> </td> </tr> <tr> <td> <label for="footer">Select Footer:</label> </td> <td><?php
  51.                            $dbLink = new mysqli('localhost', 'root', 'root', 'display');
  52.  
  53.                                  if(mysqli_connect_errno()) {
  54.                                       die("MySQL connection failed: ". mysqli_connect_error());
  55.                                             }
  56.                             else{              
  57.                                   $sql = "select footer_name from screen_footer";
  58.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  59.                                     $select3 .= "<option  value='select'>Select</option>\n";
  60.                                     while($row = $result->fetch_assoc()) {
  61.                                     $select3 .= "<option>".$row['footer_name']."</option>\n";
  62.                                                                }
  63.                                 echo "<select name='footer' id='footer_file'>".$select3."</select>";  
  64.                       mysqli_close($dbLink);
  65.                         }
  66.                        ?> </td> </tr> </table> <input type="submit" value="Save" name="submit" id="submit" /> </center> </form>
  67.  

JAVASCRIPT CODE
---------------
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  function addrow(x){
  3.            alert(x);
  4.  
  5.         for (var l=1; l <= x-1; l++){
  6.                                    $(function() {
  7.    var k=4;
  8.                                               //  var addDiv1 = $('#table_row');
  9.                                             //  var k = $('#table_row p').size() + 1;
  10. var index = 4;
  11. newRow = "<tr>" +
  12.                 "<td><label>Select Pagelayout"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
  13.                     // Check connection
  14.                             if(mysqli_connect_errno()) {
  15.                                       die("MySQL connection failed: ". mysqli_connect_error());
  16.                                             }
  17.                             else{              
  18.                                   $sql = "SELECT name FROM layout";
  19.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  20.                                      $select4 .= "<option  value='select'>". select ."</option>";
  21.                                      while($row = $result->fetch_assoc()) {
  22.                                       $select4 .= "<option>".$row['name']."</option>";
  23.                                               }
  24.                                     echo "<select name='layout.'l'.' id='layout.'l'.'>".$select4."</select>";
  25.                       mysqli_close($dbLink);
  26.                         }?></td></tr>"+
  27.                 "<tr><td><label>Select Data"+l+": </label></td><td><?php $dbLink = new mysqli('localhost', 'root', 'root', 'display'); 
  28.                     // Check connection
  29.                             if(mysqli_connect_errno()) {
  30.                                       die("MySQL connection failed: ". mysqli_connect_error());
  31.                                             }
  32.                             else{              
  33.                                   $sql = "select file_name from data_file";
  34.                                     $result = mysqli_query($dbLink, $sql) or die (mysqli_error($dbLink));
  35.                                     $select5 .= "<option  value='select'>Select</option>";
  36.                                     while($row = $result->fetch_assoc()) {
  37.                                     $select5 .= "<option>".$row['file_name']."</option>";
  38.                                                                }
  39.                                 echo "<select name='data.'l'.' id='data.'l'.'>".$select5."</select>";  
  40.                       mysqli_close($dbLink);
  41.                         }?></td></tr>";
  42.  
  43.  
  44. $('#myTable > tbody > tr').eq(k).after(newRow);
  45.      //  $('#myTable > tbody > tr').eq(k-1).after(newRow);
  46.  
  47.     return false;
  48.  
  49.                                                                 });
  50.  
  51.         }  scrnselection.h.value=l;  
  52.            alert(l);
  53.  
  54.  
  55.              }
  56.  
  57. </script>
  58.  
PHP CODE
--------
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit']))
  2. {
  3.     $screen_name = $_REQUEST['multiname'];
  4.     $screen_scr = $_POST['scr'];
  5.     $screen_header = $_POST['header'];
  6.     //$screen_layout = $_POST['layout'];
  7.    // $screen_data =  $_POST['data'];
  8.     $screen_footer = $_POST['footer'];
  9.     $num = $_POST['h'];
  10.  
  11.    echo $num;
  12.  
  13.  
  14.    $dbLink = new mysqli('localhost', 'root', 'root', 'display');
  15.  
  16.    if(mysqli_connect_errno()) {
  17.      die("MySQL connection failed: ". mysqli_connect_error());
  18.                                             }
  19. $sql="SELECT screen_name from `multi_screen` WHERE name='$screen_name'";
  20.      $check=mysqli_query($dbLink, $sql);
  21.  
  22.  
  23.  if(mysqli_num_rows($check)>=1)
  24.  
  25. $error= "Name already exist!";
  26.  
  27.  
  28. else
  29.   { 
  30.  for($i=0;$i<=$num;$i++){
  31. if(isset($_REQUEST['layout$i']))
  32. {
  33.    $screen_layout = $_POST['layout$i'];
  34.  
  35.    echo $screen_layout;
  36. }
  37. if(isset($_REQUEST['data$i']))
  38.         $screen_data = $_REQUEST['data$i'];
  39.  $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')";
  40.  
  41.    $result1 = mysqli_query($dbLink, $sql1) or die (mysqli_error($dbLink));
  42.  
  43.      if (!$result1)
  44.         {
  45.          die('Error: ' . mysqli_error());
  46.   echo "<script type=\"text/javascript\" >  alert(\"Problem ocuured with server. Please try again later.\");   </script>";
  47.                 }
  48.  
  49.    else $accept= "Data Inserted !Continue with the process.";
  50. }
  51.  
  52.  mysqli_close($dbLink);
  53.   //   header("Location: layout.php");
  54.   }
  55. }
Apr 13 '15 #1
2 1461
Claus Mygind
571 512MB
Can't give you an answer to question #1, code looks too confusing. Perhaps you need to add an "order by" clause to your sql query.

Answer to qeustion #2
You appear to only be inserting data into your table. I don't see an update or delete of existing rows in the table. That could explain why you keep building the number of rows you output

Answer to question #3

Your SQL is incorrectly written.

You have
Expand|Select|Wrap|Line Numbers
  1. $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')";
  2.  
You need to add ." ". to your string like this
Expand|Select|Wrap|Line Numbers
  1. $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."')";
Apr 13 '15 #2
RonB
589 Expert Mod 512MB
Contrary to Claus's suggestion, you don't need to use concatenation in the sql statement. PHP is perfectly capable of interpolating vars within a double quoted string. And, in most cases, as it would be here, it only servers to make the code more messy/unreadable.

You need to fix your code formatting/indentation. Due to its very messy/inconsistent indentation, it's very difficult to follow its logic and a line length of just under 900 characters is absurd.
Apr 13 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jim M | last post by:
For about a year and a half now I have been working in Access 2002 at home and converting to Access 2000 for work (I need both versions). I made a few changes to forms and queries then converted to...
4
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be...
5
by: sjl | last post by:
I've got an .aspx webform for searching my database. It basically takes user input and passes it as a parm into a stored proc to search a table. The results are returned in a SQLDataReader and...
0
by: phpmaet | last post by:
hi I have migrated the Access 2003 database to Oracle 10g database by Oracle work bench. Actually i am using the platform is Microsoft Windows Server 2003 for migaration. and i have...
0
by: geeteshss | last post by:
the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code what should i do...earlier i could view it also below this code is...
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
1
by: anjanareddy | last post by:
Hai, i am unable to inserting duplicate data into UNIQUE index column, Is there any other process, to insert duplicate data into UNIQUE index column? plz let me know.. Thanks & Regards...
0
by: =?Utf-8?B?QWFyb24=?= | last post by:
I am recieving the HttpException "Unable to validate data." This happens every time my insert or update LinkButtons for my DetailsView are clicked. It happens on my development environment as well...
5
by: Ray Ward | last post by:
I'm trying to capture the dynamic data that comes in with the URL and put them into variables and then store them in my database. Following line is the disguised Url with the data....
0
by: sreedharmcts | last post by:
I have Bulk data ,just want to insert that data from Gridview to Database table how it possible? I have done this way is it correct? protected void btn_insert_Click(object sender, EventArgs e) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.