473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting multiple record in MYSQL one at a time sequentially using AJAX

3 New Member
Hi All,
This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description:

1)The FIRST page will list all the records from the table which Admin can EDIT with CHECKBOX for each record to select. He can select one or more than one record to EDIT.
2)Now if he selects ONLY ONE record, then in the SECOND page where he will enter NEW values for record and submit the form then the JavaScript which I had written there in is not working. It is not getting the FORM values in variables.
3)But if he selects more than one then the script is working fine and giving me correct values for all the records.
4)Suppose he selects 2 records and enters new values for them and hit the submit button then both the record should get processed sequentially, means, first Record A should get into processing and for this a message should get displayed as "Processing " and when it gets done then the message should appear as "Processed" and then starts the cycle for the next record in line. In between if any error occurs for any record the Erros message should get displayed but processing should carry on for subsequent records, if present.

As far as I've understood my requirement I know I have to use AJAX for this and below is my attempt to this but here also I'm confused about the strange behaviour. Whats happening here is, though the Ajax function is called once for each record but in the function the statements inside
"if(xmlObj.read yState == 4 && xmlObj.status == 200)" is executing twice for each record. Below is my code. Can anyone point me out what mistake am I doing.

Expand|Select|Wrap|Line Numbers
  1. /*edit_process.php starts here*/
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <HTML>
  4. <script type="text/javascript">
  5. function ajaxFunction()
  6. {
  7.     var xml;
  8.     try
  9.     {  
  10.         xml=new XMLHttpRequest();  // Firefox, Opera 8.0+, Safari
  11.     }
  12.     catch (e)
  13.     {  
  14.         try
  15.         {
  16.             xml=new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 6.0+
  17.         }
  18.         catch (e)
  19.         {
  20.             try
  21.             {
  22.                 xml=new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 5.5+
  23.             }
  24.             catch (e)
  25.             {
  26.                 alert("Your browser does not support AJAX!");
  27.                 return false;
  28.             }
  29.         }
  30.     }
  31.      return xml;  // Mandatory Statement returning the ajax object created
  32. } //ajaxFunction closes
  33.  
  34. function postValues(params, div, xmlObj)
  35. {
  36.     var url="edit_trailer.php";
  37.     var flag;    
  38.     //alert(params);    
  39.  
  40.     xmlObj.open("POST", url, true);        
  41.     xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  42.     xmlObj.setRequestHeader("Content-length", params.length);
  43.     //xmlObj.setRequestHeader("Connection", "close");    
  44.  
  45.     xmlObj.onreadystatechange = function()
  46.     {    
  47.         alert("Ready state is: " + xmlObj.readyState);
  48.         if(xmlObj.readyState != 4)
  49.         {
  50.             alert('processing for ' + div);
  51.             document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader.gif">';    
  52.         }
  53.  
  54.         if(xmlObj.readyState == 4 && xmlObj.status == 200)
  55.         { //All below statements are executing twice for each record
  56.             alert('processed for ' + div);
  57.             document.getElementById(div).innerHTML = '<img src="http://bytes.com/images/loader2.gif">';
  58.             alert(xmlObj.responseText);
  59.             flag=true;
  60.             alert('returning from callback ' + flag);
  61.             return;
  62.         }
  63.     }
  64.     xmlObj.send(params);
  65.     alert('returning after send ' + flag);
  66.     return flag;
  67. } //postValues closes
  68.  
  69. function submit_data()
  70. {    
  71.     document.getElementById('update').disabled = true;
  72.  
  73.     var xmlHttp;
  74.     xmlHttp=ajaxFunction();
  75.     if (xmlHttp==null)
  76.     {
  77.         alert ("Browser does not support HTTP Request");
  78.         return;
  79.     }
  80.     //alert("Ajax created");    
  81.  
  82.     var name, id, synop, thumb, url, recID;
  83.     name = document.edit.elements["name[]"];
  84.  
  85.     if (name.length==undefined) //this part is not working when there is one record
  86.     {
  87.         alert('only one element');
  88.         name = document.edit.name;
  89.         id = document.edit.id;
  90.         synop = document.edit.synop;
  91.         thumb = document.edit.thumb;
  92.         url = document.edit.url;
  93.         recID = document.edit.recID;
  94.  
  95.         alert('Name ' + name.value);
  96.         alert('Id ' + id.value);
  97.         alert('Syn ' + synop.value);
  98.         alert('thumb ' + thumb.value);
  99.         alert('url ' + url.value);        
  100.     }
  101.     else  //this part is working when there are more then one record
  102.     {
  103.         id = document.edit.elements["id[]"];
  104.         synop = document.edit.elements["synop[]"];
  105.         thumb = document.edit.elements["thumb[]"];
  106.         url = document.edit.elements["url[]"];
  107.         recID = document.edit.elements["recID[]"];        
  108.  
  109.         flag=true;//set flag to true to call function for the first record
  110.         for(i=0;i<name.length;i++)
  111.         {
  112.             var param="name="+name[i].value+"&id="+id[i].value+"&syn="+synop[i].value;
  113.             param+="&thumb="+thumb[i].value+"&url="+url[i].value+"&recId="+recID[i].value;    
  114.  
  115.             var div='div'+(i+1);            
  116.  
  117.             if(flag)
  118.             {
  119.                 alert(flag + ' bfore calling');
  120.                 flag=false;//set flag to false and get its value from the function
  121.                 flag=postValues(param, div, xmlHttp);//call function    
  122.                 alert(flag + ' after calling');
  123.             }
  124.  
  125.         } //for loop closes
  126.  
  127.     }// else closes
  128.  
  129.     return false;
  130. }
  131. </script>
  132. </HEAD>
  133.  
  134. <BODY>
  135. <?php    
  136. require("db_connect.php");
  137. require("db_open.php");
  138. if (!$conn)
  139. {
  140.     exit("Error connecting to the database: " . $conn);
  141. }
  142.  
  143. //This value is coming from FIRST page where admin select record for editing
  144. $record = $_POST['record']; 
  145. $query = "select mov_name, mov_id, mov_synop, mov_thumb, page_name ";
  146. $query.= "FROM bhod_test_trailers WHERE mov_nos=";
  147. ?>
  148. <FORM method="post" id="edit" onSubmit="return submit_data()" NAME="edit" enctype="multipart/form-data">
  149. <TABLE border="0" cellspacing="0" cellpadding="0" align="center" width="500">
  150. <?php
  151.     $divid=1;
  152.     foreach ($record as $rec)
  153.     {
  154.         $sql = $query . $rec;
  155.         $result = mysql_query($sql);
  156.         if (!$result)
  157.         {
  158.             echo 'Could not run query: ' . mysql_error();
  159.             exit;
  160.         }
  161.         $row = mysql_fetch_row($result);
  162. ?>
  163.     <TR>
  164.         <TD width="96" align="left" valign="top">
  165.             <img src="trailers/<?php echo $row[3]; ?>.jpg" border="0" width="92" height="85">
  166.         </TD>
  167.         <TD width="254" align="left">            
  168. <TABLE border="0" cellspacing="0" cellpadding="0" width="254">
  169. <input type="hidden" name="recID[]" value="<?php echo $row[1]; ?>" />
  170.     <TR>
  171.         <TD align="left" valign="top">
  172.     <label for="mov_name">Movie Name:</label>
  173.     <input type="text" name="name[]" value="<?php echo $row[0]; ?>" />
  174.         </TD>
  175.     </TR>
  176.  
  177.     <TR>
  178.         <TD align="left" valign="top">
  179.             <label for="mov_synop">Synopsis:</label>
  180.             <textarea name="synop[]" cols="20" rows="3"><?php echo $row[2]; ?></textarea>
  181.         </TD>
  182.     </TR>
  183.  
  184.     <TR>
  185.         <TD align="left" valign="top">
  186.             <label for="mov_id">Clip ID:</label>
  187.             <input type="text" name="id[]" value="<?php echo $row[1]; ?>" />
  188.         </TD>
  189.     </TR>
  190.  
  191.     <TR>
  192.         <TD align="left" valign="top">
  193.         <label for="page_name">URL:</label>
  194.         <input type="text" name="url[]" value="<?php echo $row[4]; ?>" />
  195.         </TD>
  196.     </TR>
  197.  
  198.     <TR>
  199.         <TD align="left" valign="top">
  200.             <label for="mov_thumb">Thumbnail:</label>
  201.             <input type="file" name="thumb[]" />
  202.         </TD>
  203.     </TR>
  204. </TABLE>
  205.         </TD>
  206.         <TD width="150" align="left" valign="top">
  207.             <div id="div<?php echo $divid; ?>"></div> //this div will hold our process status
  208.         </TD>
  209.     </TR>
  210.  
  211.     <TR>
  212.         <TD colspan="3"><BR></TD>
  213.     </TR>
  214.  
  215. <?php    
  216.         $sql = '';
  217.         $divid++;
  218.     }//for loop closes
  219. ?>
  220.     <TR>
  221.         <TD colspan="3">            
  222.             <input type="submit" id="update" value="Update" />
  223.         </TD>
  224.     </TR>
  225. </TABLE>
  226. </FORM>
  227. </BODY>
  228. </HTML>
  229. /*edit_process.php ends here*/
  230.  
Jan 3 '09 #1
1 4608
acoder
16,027 Recognized Expert Moderator MVP
It's executed twice because you're calling the Ajax function twice.
Jan 6 '09 #2

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

Similar topics

1
2760
by: Ryan Hubbard | last post by:
I'm inserting a record into MySQL 4.0 using Visual Basic ADO. When using the AddNew and Update method I am unable to retrieve the value of a Auto incrment field (Yes I know I can MoveLast but this icreases the update time by a factor of 10 and when your talking about 200,000 inserts its way to long). Is there a way to insert the record into multiple tables in one statement where you can use the value of the auto incremented field as the...
4
4403
by: Greg Ofiesh | last post by:
Anyone who can help, I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign key relating to K2. My initial question has been how can I add a row to both tables with accurate Fx values in one SQL INSERT statement? I have been told it cannot be done.
4
16755
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from customers; I can execute each statement individually but get the 'you have an error in
10
12298
by: Roger Withnell | last post by:
I'm using ASP, VBScript and SQL Server. I'm also using UTF-8 character set and so my codepage is 65001 and SQL Server datatype nvarchar. I can insert unicode characters correctly into the database table using INSERT.... (field1) ...VALUES ......... (N'Characters'). How do I do this using Rs.Update viz-a-viz:
2
2419
by: csgraham74 | last post by:
Hello, im using MS Access as a front end to link to MYSQL. although this is possibly an Access issue i thought someone would be able to help me. the error message "ODBC--insert on a linked table 'tablname' failed" is displayed when i attempt to insert records to the MYSQL table. Does anyone know of a way round this ?? im using the most recent MYSQL driver and i have only text fields in my insert statement.
52
6329
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
7
6643
by: ebindia0041 | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, Asp.net 1.1 with c# I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same data as the previous insert incident, (except for the timestamp). Here is an example. Follow the values of the 'Search String' field:
16
15564
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be faster, so I'd like to try creating multiple threads, and having them load different chunks of the file at the same time and process it asynchronously. Is it possible to do something like that, and if so, what would be needed to do so?
1
3027
by: djmeltdown | last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky. I've tried the mysql_query statement without a preceeding variable as well. System information Server Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.3 Time: 12/07/2007 01:20:06 PM PHP PHP Version: 5.2.3 Safe Mode activated: no PHP Memory...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.