Jump to content

Store an array of data into the database...


snakez

Recommended Posts

Hey, Im gonna need ur help guys... As you can see, I have here the codes below(pls never mind about my coding, im just a newbie :)). Well anyway by default I have an array of elements in which I am supposed to save it to the mysql database using PHP. Now i have created a command button("Add Part") in which i can add another array of elements as many as i want. The problem is that whenever i tried to add another array of elements, whatever the last array of elements i have added, that was the only array of elements that will be saved to the database when I finally clicked the submit button. The rests... well i dont really even know where they gone :). I hope you can help me with this guys...Will you pls give me also some tips on what should be the appropriate query for this to save all the array of elements to the DB and some tips also on what should be the right code to display the output of array of elements on a page? Here is my code in javascript....

		function addPart()		{				var tbl = document.getElementById('myTable')				var nRow = tbl.rows.length - 13				var row = tbl.insertRow(nRow)				var blankCell = row.insertCell(0)				var addCell = row.insertCell(1)				var PartText = new Array(2)				var Append = addCell.appendChild				var Caption = document.createTextNode				for(var i=0;i<=2;i++)				{						PartText[i] = document.createElement('input')						PartText[i].type = 'text'						PartText[i].id = 'part' + nRow				}				PartText[0].name = 'partname'				PartText[1].name = 'partnumber'				PartText[2].name = 'version'				blankCell.bgColor="#f1f1f1"				addCell.bgColor="#f1f1f1"				Append(Caption('Part Name: '))				Append(PartText[0])				Append(Caption('	   Part Number: '))				Append(PartText[1])				Append(Caption('	   Version: '))				Append(PartText[2])		}</script>

Here is my code in html with the php values....

<tr><td bgcolor='#f1f1f1'></td><td bgcolor='#f1f1f1' align='left'> Part Name: <input type="text" class='bginput'name="partname" id="part1" value='<?=$partname?>'/> Part Number: <input type="text" class='bginput' name="partnumber" id="part1" value='<?=$partnumber?>'/> Version: <input type="text" class='bginput' name="version" id="part1" value='<?=$version?>'/></td></tr><tr><td bgcolor='#f1f1f1'></td><td bgcolor='#f1f1f1' align='right'>	 <input type="button" value="Add Part" onclick=addPart() /> 	 <input type="button" value="Cancel" onclick=delPart() /></td></tr>

Tnx in advance guyz... I hope you could give some ideas... :)

Link to comment
Share on other sites

You are giving all of the elements the same name, so the most recent one is the only one that gets submitted. Put the brackets [] after the name to specify that you are creating an array of elements. This only works for PHP.Part Name: <input type="text" class='bginput' name="partname[]" id="part1" value='<?=$partname?>'/>Part Number: <input type="text" class='bginput' name="partnumber[]" id="part1" value='<?=$partnumber?>'/>Version: <input type="text" class='bginput' name="version[]" id="part1" value='<?=$version?>'/>Then you can get the array from $_POST['version'] or whatever, and go through it. Also, it's a good idea to always use <?php instead of just <?, because a lot of servers do not support the short tags, and they are also confusing for XML documents.

Link to comment
Share on other sites

justsomeguy,Thanks for some suggestions of using php scripts. By the way, I already change the name of my elements into partname[],partnumber[],version[]. I also get their array from $_POST['partname'] $_POST['partnumber'] $_POST['version'] respectively. However, i still get the same results. Perhaps you're right of what you have stated above, maybe i'm just an idiot(not reli :) ) of getting the code well done directly. Is there still any wrong with the code i've edited?

Link to comment
Share on other sites

I don't see the changes to the code above. Maybe post your new code. One thing I noticed, but it wouldn't really matter, is that you define PartText as an array with 2 elements, but you later give it three elements. But this is the type of stuff you want to change: PartText[0].name = 'partname[]' PartText[1].name = 'partnumber[]' PartText[2].name = 'version[]'In addition to what I posted before.

Link to comment
Share on other sites

These are the changes i've made to my code during my 1st post above.This is the part in javascript code that was changed:

PartText[0].name = 'partname[]'PartText[1].name = 'partnumber[]'PartText[2].name = 'version[]'

And this is the part in html/php code that was changed:

Part Name: <input type="text" class='bginput'name="partname[]" id="part1" value='<?php=$partname?>'/>Part Number: <input type="text" class='bginput' name="partnumber[]" id="part1" value='<?php=$partnumber?>'/>Version: <input type="text" class='bginput' name="version[]" id="part1" value='<?php=$version?>'/>

This is the code i've made also to get the value inside the array:

$partname=$_POST['partname'];$partnumber=$_POST['partnumber'];$version=$_POST['version'];

Primarily i have a set of 3 elements(partname,partnumber,version), then i added a button to add another set of 3 elements as many as i can. I have the submit button also to save and post all the values inputted from the set of 3 elements or another sets, all of them. Now, regarding the code above, whatever strings i am going to input to the set of elements(partname,partnumber,version) or to another set of elements, after i clicked submit button, only 1 set of 3 elements was save in the databse and another thing is that the string that was saved to each element in the database is "Array". That was also the string that is being outputted.Can you explain it to me why? maybe there is really something wrong with my code...

Link to comment
Share on other sites

I added this code below also into the javascript codes....

PartText[0].value ='<?=$partname?>'PartText[1].value ='<?=$partnumber?>'PartText[2].value ='<?=$version?>'

Same thing happens.... Still "Array" strings are the one that was saved into the database, not the real value that i inputted to the elements.

Link to comment
Share on other sites

It is showing "Array" because the value is an array instead of a string. Use var_dump in PHP to print the value of the variable so you can see what is going on.var_dump($partname);You probably need to set the value to be $partname[0] or something like that. You won't be able to set default values for the new elements though from PHP, because PHP will finish executing before the javascript runs. If you wanted to include a dynamic value for a new element, you would need to use something like Ajax (using XMLHTTPRequest) to get another value from the server.

Link to comment
Share on other sites

I dont reli know what is going on now with the code... i just can't really figure it out coz this is not actually my own scripts/codes. Im just an OJT actually in a particular company and they assigned me to edit the scripts/codes of their online request form. My head is spinning round here coz I can't even trace it.Thanks anyway for your help... perhaps your suggestions and ideas are really useful but i just dont know how coz Im not really familiar with php/myqsl/ajax/javascript... They just let me edit/update the codes of their online request form without even teaching me the background of it. They just only say just have a self study on it and if there's any problems, just "Google it!!!". Well anyway, they're right coz anything can be found in google(but not everything I think so). I will not bother you anymore about this problem... i'l just try to find it out myself.... there's just only one last thing I need you to help me:Where can I find some other sources or tutorials about php/javascript/mysql/ajax or any related to it?Well(I know!), w3schools is really great source/tutorials but i need other sites aside from it... those that can really help me easily to understand the way it is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...