Link to home
Start Free TrialLog in
Avatar of happysunny
happysunnyFlag for United States of America

asked on

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
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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).
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of happysunny

ASKER

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?
I have to take each individual row and add it to the database.  Any suggestions?
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.
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.
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.
My thoughts are that I will need the rnumber variable to be differentiate between all of the fields.
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?
No, this will be on the management interface where only one person will be entering it in at a time.
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.
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.
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'.
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might want to click the Request Attention link above and see if a Moderator can locate additional Experts to help with this.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had to mark my own answer as the solution, but I awarded points based on what helped me :)  Thank you very much!