• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

How do I put mulitple entries into database?

Hello all,

I am trying to put multiple entries into a database using "Add a Button".  The following code will show you how I am letting them add a row and then the Submit button will take it to the php code to actually put it in the database. I know how to put entries into the database, however, how do I differentiate between entries?  Do I need to add the row number to the id?  I'm a little lost!

<HTML>
<head>
<title>Add test</title>
<script language="javascript">
rnumber=0;
function addRow(tbl,row){
rnumber++;
if (rnumber<10){
 
var textbox ='<center><select id=toy><option value="select">Select</option><option value="doll">Doll</option><option 

value="truck">Truck</option><option value="train">Train</option><option value="ball">Ball</option></select></center>';

var textbox2 = '<center><select id=color><option value="select">Select</option><option value="blue">Blue</option><option 

value="red">Red</option><option value="yellow">Yellow</option><option 

value="Green">Green</option></select></center>';

var textbox3 = '<center><input type="text" size = "5" maxlength= "10" id=weight></center>';
var textbox4 = '<center><input type="file" size="10" id=upload></center>';
var textbox5 = '<center><input type="text" size = "5" maxlength= "10" id=price></center>';
var textbox6 = '<center><input type="checkbox" id=accept></center>';

var stop = '<input type="button" value="delete" onclick="deleteRow(this)" >';
 
var tbl = document.getElementById(tbl);
var rowIndex = document.getElementById(row).value;
var newRow = tbl.insertRow(rnumber);
var newCell = newRow.insertCell(0);
newCell.innerHTML = textbox;
var newCell = newRow.insertCell(1);
newCell.innerHTML = textbox2;
var newCell = newRow.insertCell(2);
newCell.innerHTML = textbox3;
var newCell = newRow.insertCell(3);
newCell.innerHTML = textbox4;
var newCell = newRow.insertCell(4);
newCell.innerHTML = textbox5;
var newCell = newRow.insertCell(5);
newCell.innerHTML = textbox6;
var newCell = newRow.insertCell(6);
newCell.innerHTML = stop;
}
 
}
    function deleteRow(r)
    {
    var i=r.parentNode.parentNode.rowIndex;
    document.getElementById('TableMain').deleteRow(i);
     
    rnumber--;
    }
 
</script>
</head>
<body>

<table width="75%" >
<tr>
<td width = 100%>

<form name="toyentry" method="post"> 
<table width="100%" border="0" cellspacing="0" cellpadding="2" id="TableMain">
<th><center>Toy</th>
<th>Color</th>
<th>Weight</th>
<th>Upload Picture</th>
<th>Price</th>
<th>Accept</th>
<tr id="row1">
</tr>
</table>
</form></td>
<td valign="top" width = 20%>
<input type="button" name="Button" value="Add more" onClick="addRow('TableMain','row1')"></td>
</tr>
</table>
<table>
<tbody>
<tr><td>
<input type="submit" class="button" name="submit" value="Submit">
</td></tr>
</tbody>
</table>
</body>
</html>

Open in new window


Thanks in advance for your help!
~Paula
0
happysunny
Asked:
happysunny
  • 9
  • 8
3 Solutions
 
slightwv (䄆 Netminder) Commented:
I've always have problems with dynamic HTML but that was in the .Net world.  Not sure about the PHP world...

You should be able to just loop through the HTML table rows and insert the data.  If you need some ID in the database then you can generate on in HTML or at insert time.

I know very little PHP but here is code that takes an HTML table and creates a PHP array.
http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array

If that works, it should be a simple matter to insert the data in a loop through the array (or rewrite the array piece to insert directly).
0
 
Ray PaseurCommented:
Add this to the top of your script before the <html> tag:

<?php var_dump($_POST); ?>

It will show you what is coming into the PHP action script.

HTH, ~Ray
0
 
happysunnyAuthor Commented:
I am familiar with php loops, but I don't know as much when it comes to JavaScript.  Would doing something like this work?

var textbox ='<center><select id=toy'+rnumber+'><option value="select">Select</option><option 

value="doll">Doll</option><option value="truck">Truck</option><option value="train">Train</option><option 

value="ball">Ball</option></select></center>';

var textbox2 = '<center><select id=color'+rnumber+'><option value="select">Select</option><option 

value="blue">Blue</option><option value="red">Red</option><option value="yellow">Yellow</option><option 

value="Green">Green</option></select></center>';

var textbox3 = '<center><input type="text" size = "5" maxlength= "10" id=weight'+rnumber+'></center>';
var textbox4 = '<center><input type="file" size="10" id=upload'+rnumber+'></center>';
var textbox5 = '<center><input type="text" size = "5" maxlength= "10" id=price'+rnumber+'></center>';
var textbox6 = '<center><input type="checkbox" id=accept'+rnumber+'></center>';

Open in new window


Also, this may seem like a dumb question and I apologize, but how do I change the variable from JavaScript to PHP?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
happysunnyAuthor Commented:
I have to take each individual row and add it to the database.  Any suggestions?
0
 
slightwv (䄆 Netminder) Commented:
Sorry got tied up and couldn't get back to this.

Once you get the loop through the HTML table the rest should be easy.

Did you see it the code in the link I posted works with your dynamic HTML table?

I doubt I can help directly with the php specifics.
0
 
happysunnyAuthor Commented:
Yes, I did take a look at the link, but I'm trying to figure out how to pass the JavaScript variables over to PHP so I can work a while loop.  I can't seem to figure out how.  Any idea?  I've been googling like crazy but can't come up with something concrete.  I can't really use GET and POST.
0
 
slightwv (䄆 Netminder) Commented:
Not sure what JavaScript variables you need to pass but in the past I have declared hidden (CSS display:false;) fields to hold values I needed to post back to the server.  That was JQuery with ASP.Net but the approach should work here as well.
0
 
happysunnyAuthor Commented:
My thoughts are that I will need the rnumber variable to be differentiate between all of the fields.
0
 
slightwv (䄆 Netminder) Commented:
Maybe but how will that number differ over requests/users/???

If two people open the page about the same time, what will the numbers get you?
0
 
happysunnyAuthor Commented:
No, this will be on the management interface where only one person will be entering it in at a time.
0
 
slightwv (䄆 Netminder) Commented:
Are you planning on selecting some max value to start the counter?

Best to not worry about the id/key client side.  Leave it to the database server.
0
 
happysunnyAuthor Commented:
I'd like to check the database and see if there is anything under a certain order number first.  If so, I want to pull those out and show them with the opportunity to add more fields if necessary.  If there is nothing in the database, I don't need to start with any fields, but add them as I go.  The max value on the counter above is set up as 10 (rnumber<10), but I believe I'm going to change that to 20.
0
 
slightwv (䄆 Netminder) Commented:
Querying/building a webpage from a query then tracking/adding new rows is really a different question.

Still not following what a javascript generated I'd column will buy you.  Now if you generated a guid for 'new' rows and retrieved the guid from the query, then you will have unique ids for any situation.

You just need to account for 'unique'.
0
 
happysunnyAuthor Commented:
Have you tried my code and seen it in action?  I'm having a hard time understanding your post.  Each input box needs to be unique.  Once the customer is done adding the boxes and putting in their information, they will press Submit.  That information needs to go into the database.  So the issue is that I need help with taking the information from JavaScript and changing it to PHP so it can go into the database.  Have any idea how I can do this successfully?
0
 
slightwv (䄆 Netminder) Commented:
>>Have you tried my code and seen it in action?

No.

>> Each input box needs to be unique.  

This is where my confusion sets in.  I can see each database row needing a Primary Key.  Why does each 'new' field on the web page need to create that key with javascript?

>>Have any idea how I can do this successfully?

I've provided my suggestion.  On form submission back to PHP, loop through the HTML table rows.

I'm not sure where the Javascript comes into play except to dynamically add rows.

the only thing that might mess this up is if PHP will net handle dynamic html well on post back.

Again, I'm not a PHP person but have had major problems with dynamic html and .Net when posting back to the server.
0
 
slightwv (䄆 Netminder) Commented:
You might want to click the Request Attention link above and see if a Moderator can locate additional Experts to help with this.
0
 
happysunnyAuthor Commented:
I believe I have this part figured out :)

<?php
include ("dbinfo.php");
include ("add.php");
?>
<HTML>
<head>
<title>Add test</title>
<php
$row_no="0";
?>
<script language="javascript">
rnumber=0;
function addRow(tbl,row){

<?php
//row count
$row_no++;
 
if ($row_no<20){
 ?>
 
var textbox ='<center><select name=toy[]><option value="select">Select</option><option value="doll">Doll</option><option value="truck">Truck</option><option value="train">Train</option><option value="ball">Ball</option></select></center>';

var textbox2 = '<center><select name=color[]><option value="select">Select</option><option value="blue">Blue</option><option value="red">Red</option><option value="yellow">Yellow</option><option value="Green">Green</option></select></center>';

var textbox3 = '<center><input type="text" size = "5" maxlength= "10" name=weight[]></center>';
var textbox4 = '<center><input type="file" size="10" name=upload[]></center>';
var textbox5 = '<center><input type="text" size = "5" maxlength= "10" name=price[]></center>';
var textbox6 = '<center><input type="checkbox" name=accept[]></center>';

var stop = '<input type="button" value="delete" onclick="deleteRow(this)" >';
 
var tbl = document.getElementById(tbl);
var rowIndex = document.getElementById(row).value;
var newRow = tbl.insertRow(rnumber);
var newCell = newRow.insertCell(0);
newCell.innerHTML = textbox;
var newCell = newRow.insertCell(1);
newCell.innerHTML = textbox2;
var newCell = newRow.insertCell(2);
newCell.innerHTML = textbox3;
var newCell = newRow.insertCell(3);
newCell.innerHTML = textbox4;
var newCell = newRow.insertCell(4);
newCell.innerHTML = textbox5;
var newCell = newRow.insertCell(5);
newCell.innerHTML = textbox6;
var newCell = newRow.insertCell(6);
newCell.innerHTML = stop;
}
 
}
    function deleteRow(r)
    {
    var i=r.parentNode.parentNode.rowIndex;
    document.getElementById('TableMain').deleteRow(i);
     
<?php
    $row_no--;
?>
    }
 
</script>
</head>
<body>

<table width="75%" >
<tr>
<td width = 100%>

<form name="toyentry" method="post"> 
<table width="100%" border="0" cellspacing="0" cellpadding="2" id="TableMain">
<th><center>Toy</th>
<th>Color</th>
<th>Weight</th>
<th>Upload Picture</th>
<th>Price</th>
<th>Accept</th>
<tr id="row1">
</tr>
</table>
</form></td>
<td valign="top" width = 20%>
<input type="button" name="Button" value="Add more" onClick="addRow('TableMain','row1')"></td>
</tr>
</table>
<table>
<tbody>
<tr><td>
<input type="submit" class="button" name="submit" value="Submit">
</td></tr>
</tbody>
</table>
</body>
</html>

Open in new window


add.php:
<?php

if(isset($_POST['submit']))
{

include ("dbinfo.php");

$weight = $_POST["weight"];

$cnt=count($weight);

for ($counter=0; $counter < $cnt; $counter++)
{
$weight = $_POST["weight"][$counter];
$file = $_POST["upload"][$counter];
$price = $_POST["price"][$counter];
$toy = $_POST["toy"][$counter];
$color = $_POST["color"][$counter];

mysql_query("INSERT INTO table (Weight, File, Price, Toy, Color) VALUES('$weight', '$file', '$price', '$toy', '$color')") or die(mysql_error());  
}

}
?>

Open in new window

0
 
happysunnyAuthor Commented:
I had to mark my own answer as the solution, but I awarded points based on what helped me :)  Thank you very much!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now