473,466 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to delete in database using php and javascript?

4 New Member
Hello all,

I am trying to use PHP to pull information from a mySQL database, as well as offer an "Add More" button through the use of JavaScript. I'm able to put the new information into the database and I am able to pull the information out of the database to show. However, I am unable to figure out how to delete the information that's pulled from the database when I hit the delete button beside it (line 138 below). Any thoughts or ideas?

Thank you in advance!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("dbinfo.php");
  3. include ("add.php");
  4. ?>
  5. <HTML>
  6. <head>
  7. <title>Add test</title>
  8. <php
  9. $rnumber="0";
  10. ?>
  11. <script language="javascript">
  12. rnumber=0;
  13. function addRow(tbl,row){
  14.  
  15. <?php
  16. //row count
  17. $rnumber++;
  18.  
  19. if ($rnumber<20){
  20.  ?>
  21.  
  22. var textbox = '<center><input type="file" size="10" name=upload[]></center>';
  23.  
  24. var textbox2 ='<center><select name=toy[]><option 
  25.  
  26. value="select">Select</option><option value="doll">Doll</option><option 
  27.  
  28. value="truck">Truck</option><option value="train">Train</option><option 
  29.  
  30. value="ball">Ball</option></select></center>';
  31.  
  32. var textbox3 = '<center><select name=color[]><option 
  33.  
  34. value="select">Select</option><option value="blue">Blue</option><option 
  35.  
  36. value="red">Red</option><option value="yellow">Yellow</option><option 
  37.  
  38. value="Green">Green</option></select></center>';
  39.  
  40. var textbox4 = '<center><input type="text" size = "5" maxlength= "10" name=weight[]
  41.  
  42. ></center>';
  43. var textbox5 = '<center><input type="text" size = "5" maxlength= "10" name=price[]
  44.  
  45. ></center>';
  46. var textbox6 = '<center><input type="checkbox" name=accept[]></center>';
  47.  
  48. var stop = '<input type="button" value="delete" onclick="deleteRow(this)" >';
  49.  
  50. var tbl = document.getElementById(tbl);
  51. var rowIndex = document.getElementById(row).value;
  52. var newRow = tbl.insertRow(rnumber);
  53. var newCell = newRow.insertCell(0);
  54. newCell.innerHTML = textbox;
  55. var newCell = newRow.insertCell(1);
  56. newCell.innerHTML = textbox2;
  57. var newCell = newRow.insertCell(2);
  58. newCell.innerHTML = textbox3;
  59. var newCell = newRow.insertCell(3);
  60. newCell.innerHTML = textbox4;
  61. var newCell = newRow.insertCell(4);
  62. newCell.innerHTML = textbox5;
  63. var newCell = newRow.insertCell(5);
  64. newCell.innerHTML = textbox6;
  65. var newCell = newRow.insertCell(6);
  66. newCell.innerHTML = stop;
  67.  <?php
  68. }
  69. ?>
  70.  
  71. }
  72.     function deleteRow(r)
  73.     {
  74.     var i=r.parentNode.parentNode.rowIndex;
  75.     document.getElementById('TableMain').deleteRow(i);
  76.  
  77. <?php
  78.     $rnumber--;
  79. ?>
  80.     }
  81.  
  82. </script>
  83. </head>
  84. <body>
  85.  
  86. <table width="75%" >
  87. <tr>
  88. <td width = 100%>
  89.  
  90. <form action="<?php echo $_SERVER['PHP_SELF'];?>"name="toyentry" method="post"> 
  91. <table width="100%" border="0" cellspacing="0" cellpadding="2" id="TableMain">
  92. <th>Upload Picture</th>
  93. <th><center>Toy</th>
  94. <th>Color</th>
  95. <th>Weight</th>
  96. <th>Price</th>
  97. <th>Accept</th>
  98.  
  99. $id=$_SESSION['id'];
  100. $id=htmlspecialchars($id);
  101. $id_sq=mysql_real_escape_string($id);
  102.  
  103. <?php
  104. $sql="SELECT * FROM table WHERE id='$id_sq'";
  105. $result = mysql_query($sql);
  106.  
  107. while ($row = mysql_fetch_assoc($result))
  108. {
  109. $toydb=$row["toy"];
  110. $colordb=$row["color"];
  111. $weightdb=$row["Weight"];
  112. $picdb=$row["ImagePath"];
  113. $pricedb=$row["Price"];
  114.  
  115. echo "<tr><td>";
  116. echo "<center><img src=\"$picdb_sq\" height=\"75\" width=\"100\"></center>";
  117. echo "</td><td>";
  118. echo "<center>";
  119. echo $toydb_sq;
  120. echo "</center>";
  121. echo "</td><td>";
  122. echo "<center>";
  123. echo $colordb_sq;
  124. echo "</center>";
  125. echo "</td><td>";
  126. echo "<center>";
  127. echo $weightdb_sq;
  128. echo "</center>";
  129. echo "</td><td>";
  130. echo "<center>$";
  131. echo $pricedb_sq;
  132. echo "</center>";
  133. echo "</td><td>";
  134. echo "<center><input type=\"checkbox\"></center>";
  135. echo "</td><td>";
  136. echo "<center><input type=\"checkbox\"></center>";
  137. echo "</td><td>";
  138. echo "<input type=\"button\" value=\"Delete\" onclick=\"deleteRow(this)\" >";
  139. echo "</td></tr>";
  140. }
  141. ?>
  142.  
  143. <tr id="row1">
  144. </tr>
  145. </table>
  146. </td>
  147. <td valign="top" width = 20%>
  148. <input type="button" name="Button" value="Add more" onClick="addRow
  149.  
  150. ('TableMain','row1')"></td>
  151. </tr>
  152. </table>
  153. <table>
  154. <tbody>
  155. <tr><td>
  156. <input type="submit" class="button" name="submit" value="Submit">
  157. </td></tr>
  158. </tbody>
  159. </table>
  160. </form>
  161. </body>
  162. </html>
  163.  
Here is add.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if(isset($_POST['submit']))
  4. {
  5.  
  6. include ("dbinfo.php");
  7.  
  8. $weight = $_POST["weight"];
  9.  
  10. $cnt=count($weight);
  11.  
  12. for ($counter=0; $counter < $cnt; $counter++)
  13. {
  14. $weight = $_POST["weight"][$counter];
  15. $file = $_POST["upload"][$counter];
  16. $price = $_POST["price"][$counter];
  17. $toy = $_POST["toy"][$counter];
  18. $color = $_POST["color"][$counter];
  19.  
  20. mysql_query("INSERT INTO table (Weight, File, Price, Toy, Color) VALUES('$weight', '$file', '$price', '$toy', '$color')") or die(mysql_error());  
  21. }
  22.  
  23. }
  24. ?>
  25.  
Oct 28 '11 #1

✓ answered by zorgi

I didn't look through your code but to delete rows from the database you should use MySql DELETE statement.

http://dev.mysql.com/doc/refman/5.0/en/delete.html

I am not sure why do you want to use javascript and php to do this but if you really have to... than use ajax.

4 3863
zorgi
431 Recognized Expert Contributor
I didn't look through your code but to delete rows from the database you should use MySql DELETE statement.

http://dev.mysql.com/doc/refman/5.0/en/delete.html

I am not sure why do you want to use javascript and php to do this but if you really have to... than use ajax.
Oct 29 '11 #2
shinypenny
4 New Member
Well, I have to use JavaScript to dynamically add the rows and php to talk to the database. Is Ajax similar to jQuery? Thanks for your response :)
Oct 29 '11 #3
zorgi
431 Recognized Expert Contributor
  • jQuery is javascript library
  • Ajax is a way of programming

With jQuery you can preforme AJAX request.

http://api.jquery.com/jQuery.ajax/
Oct 29 '11 #4
shinypenny
4 New Member
I see. Thanks so much :)
Oct 29 '11 #5

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

Similar topics

17
by: kartheek | last post by:
hi friends, can any one out here help me by giving me the code to connect to an MSACCESS database using javascript.
4
by: vee10 | last post by:
hi, I need help to connect to database(SQLSERVER2005) using javascript please help me
6
by: yc022 | last post by:
Hi all, this is my first time using this so i'm not really sure how it works. please bear with me. i am trying to delete a row in a database using a select list. First of all i have a query to...
11
by: srivijayaragavan | last post by:
Can any one help me how to connect MS Access from Javascript with example? Is any component required or that is to be installed in client side? I have read that some component needs to be installed.
1
by: Sunlis | last post by:
I need to find a way to communicate with a MS Access database using Javascript, PHP, or some other client-side scripting. It's for a Computer Science exam, and I cannot solve this problem. Users...
2
by: Abhi~Menon | last post by:
I've a situation here where i want to interact with the local Access Database from an HTML page. I've created a DSN for the Access. I'm wondering how the connection part has to made from Java Script....
5
by: lintolawrance | last post by:
hi friends, can any one out here help me by giving me the code to connect to an SOL database using javascript.
0
by: tarkuser | last post by:
Hai... I have problem in storing values from flash to database and to select data from a database using Javascript. First i get details of a student through flash and then sotre it to database...
3
by: mkhizess | last post by:
Hi Buddzy Can you plz help to connect to Access DataBase input from HTML form using insert statement. I am still new on programming with html here is my code . <html> <head> ...
7
by: saadkhan | last post by:
Is there any way to create xml file and save it in SQL Server database all using javascript? I have no security issues for the purpose i need to do it. Though if anyone could give me some better idea...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.