doforumda Posted February 19, 2010 Share Posted February 19, 2010 hi i have a dynamic form. i need help in how can i use update query when values from dynamic form get pass to php file. currently when i press submit it displays this and does not update fields UPDATE contacts SET name='Array', im='Array' WHERE userid='1' this my code for dynamic form <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script> <script> var id = 0; function addFormField() { //document.getElementById("id").value; id++; $("#divTxt").append( "<p id='row" + id + "'><label>Relation <select name='relation[]' id='relation" + id + "'><option value='brother'>Brother</option><option value='sister'>Sister</option><option value='father'>Father</option><option value='mother'>Mother</option></select> <input type='text' name='name[]' /> <a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Remove</a></p>" ); } function removeFormField(id) { $(id).remove(); } </script> </head> <body> <form action="addDynamicFields.php" method="post" id="form1"> <label>Relation</label> <select name="relation[]" id="relation0"> <option value="brother">Brother</option> <option value="sister">Sister</option> <option value="mother">Mother</option> <option value="father">Father</option> <option value="daughter">Daughter</option> <option value="son">Son</option> </select> <input type="text" name="name[]" /> <div id="divTxt"></div> <p><input type="submit" value="Submit" name="submit"> <!-- <input type="reset" value="Reset" name="reset"> --></p> </form> <p><a href="#" onClick="addFormField(); return false;">Add</a></p> </body> </html> this is my php file <?php $screenname = $_POST['relation']; $name = $_POST['name']; $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); for($i=1; $i<count($screenname); $i++) { //$imscreenname = $_POST['screenname'][$i]; //$imname = $_POST['name'][$i]; //echo $imscreename; //$imscreenname = $screenname[$i]; $screennamesave = "UPDATE contacts SET name='$screenname', im='$name' WHERE userid='1'" or die(mysql_error()); $result = mysql_query($screennamesave); echo $screennamesave; } ?> Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 you have [] on the end of your field names, in your form. This means they will be sent as an array. If you only want the one value to be sent as a normal variable that you can use as for example $_POST['name'], then make name="name[]" to name="name" and name="relation[]" into name="relation". This form is designed to have multiple occurances of each of those fields, and if you were using that, then you would need to handle the values by navigating the array. Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 you have [] on the end of your field names, in your form. This means they will be sent as an array. If you only want the one value to be sent as a normal variable that you can use as for example $_POST['name'], then make name="name[]" to name="name" and name="relation[]" into name="relation". This form is designed to have multiple occurances of each of those fields, and if you were using that, then you would need to handle the values by navigating the array. let me explain this. my form is dynamic. when my form first loads there is a label "Relation" in front of it there is select menu in front of select menu there is input text field. below this there is a submit button and below submit button there is "add" link. so when user clicks on add link then one more set of field like above it appeared. and there is a no limit how many a user can add form fields this is way they are allowed. so when user input lets suppose 6 fields then i want all of the data goes to php file and update that data for current logged in user. so how can this be done to get all the data and update all the data in my db table? my table is something like this. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 ok so you are using the multiple occurences, which I missed when first reading it. so you leave the [] on the field names but need to process as an array, which you have the correct code for, but commented out. $imscreenname = $_POST['screenname'][$i]; $imname = $_POST['name'][$i]; $imscreenname = $screenname[$i]; Those lines are needed to get the individual values from the array Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 ok so you are using the multiple occurences, which I missed when first reading it. so you leave the [] on the field names but need to process as an array, which you have the correct code for, but commented out. $imscreenname = $_POST['screenname'][$i]; $imname = $_POST['name'][$i]; $imscreenname = $screenname[$i]; Those lines are needed to get the individual values from the array now i tried this but this still doesnt work. it neither update my db nor display anything means no errors no echo statments. for($i=1; $i<count($_POST['relation']); $i++) { $imscreenname = $_POST['relation'][$i]; $imname = $_POST['name'][$i]; echo $imscreename."<br>"; echo $imname."<br>"; $imscreenname = $screenname[$i]; $screennamesave = "UPDATE contacts SET name='$screenname', im='$name' WHERE userid='1'" or die(mysql_error()) or die(mysql_error()); $result = mysql_query($screennamesave); echo $screennamesave; } Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 not even insert is working $screennamesave = "INSERT INTO contacts VALUES ('1','$screenname','$name')" or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 Ok so back to basic debug. You need to put print_r($_POST); before that for loop, and see what it says. Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 this is what it says Array ( [relation] => Array ( [0] => brother ) [name] => Array ( [0] => bri ) [submit] => Submit ) Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 aha you have your for loop starting at $=1; try setting the start point to $i=0; Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 i get this now Array ( [relation] => Array ( [0] => brother ) [name] => Array ( [0] => bro ) [submit] => Submit ) Notice: Undefined variable: imscreename in C:\wamp\www\examples\addDynamicFields.php on line 20 bro INSERT INTO contacts VALUES ('1','Array','Array') Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 aha you have your for loop starting at $=1; try setting the start point to $i=0; so what should i do?? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 change for($i=1; $i<count($_POST['relation']); $i++) { to for($i=0; $i<count($_POST['relation']); $i++) { Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 change for($i=1; $i<count($_POST['relation']); $i++) { to for($i=0; $i<count($_POST['relation']); $i++) { i changed it and i get this thing Array ( [relation] => Array ( [0] => brother ) [name] => Array ( [0] => bro ) [submit] => Submit ) Notice: Undefined variable: imscreename in C:\wamp\www\examples\addDynamicFields.php on line 20 bro INSERT INTO contacts VALUES ('1','Array','Array') Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 Ok, and going back to your original code, you see you have $imscreenname = $_POST['screenname'][$i]; which means you need a field called screenname[] on your form, and you do not have that. So, either remove screenname from the query, or add the field to the form Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 19, 2010 Author Share Posted February 19, 2010 now i modify my code to this print_r($_POST); for($i=0; $i<count($_POST['relation']); $i++) { //$imscreenname = $_POST['relation'][$i]; //$imname = $_POST['name'][$i]; //echo $screename."<br>"; //echo $name."<br>"; $imscreenname = $screenname[$i]; $imname = $name[$i]; $screennamesave = "INSERT INTO contacts VALUES ('1','$screenname','$name')" or die(mysql_error()); //$screennamesave = "UPDATE contacts SET name='$screenname', im='$name' WHERE userid='1'" or die(mysql_error()); $result = mysql_query($screennamesave); echo $screennamesave; } this is the display. this time i chose all the options from my select menu by adding more dynamic fields Array ( [relation] => Array ( [0] => brother [1] => sister [2] => father ) [name] => Array ( [0] => bro [1] => sis [2] => father ) [submit] => Submit ) INSERT INTO contacts VALUES ('1','Array','Array')INSERT INTO contacts VALUES ('1','Array','Array')INSERT INTO contacts VALUES ('1','Array','Array') Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 ok good you still hjave some code commented out, and another error this : //$imscreenname = $_POST['relation'][$i]; //$imname = $_POST['name'][$i]; //echo $screename."<br>"; //echo $name."<br>"; $imscreenname = $screenname[$i]; $imname = $name[$i]; should be $imscreenname = $_POST['screenname'][$i]; $imname = $_POST['name'][$i]; //echo $screename."<br>"; //echo $name."<br>"; Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 21, 2010 Author Share Posted February 21, 2010 here is the same form as above in my post i changed variable names and db and table. here what my form is doing. when user logs in and wants to edit his education information he clicks on Edit. then below form appears. he then selects different universities and degrees from the list by clicking the link "Add" to add more fields in the form. my form code is below <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script> <script> var id = 0; function addFormField() { //document.getElementById("id").value; id++; $("#divTxt").append( "<p>" + "<label>University </label>" + "<select name='university[]' id='relation" + id + "'>" + "<option value='uit'>UIT</option>" + "<option value='nun'>NUN</option>" + "<option value='fast'>FAST</option>" + "<option value='au'>AU</option>" + "<option value='vu'>VU</option>" + "</select>" + "<input type='text' name='degree[]' />" + "<a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Remove</a>" + "</p>" ); } function removeFormField(id) { $(id).remove(); } </script> </head> <body> <form action="dynamicfieldsProcess.php" method="post" id="form1"> <label>University</label> <select name="university[]" id="relation0"> <option value="uit">UIT</option> <option value="nun">NUN</option> <option value="fast">FAST</option> <option value="uop">UOP</option> <option value="au">AU</option> <option value="vu">VU</option> </select> <input type="text" name="degree[]" /> <div id="divTxt"></div> <p><input type="submit" value="Submit" name="submit"> <!-- <input type="reset" value="Reset" name="reset"> --></p> </form> <p><a href="#" onClick="addFormField(); return false;">Add</a></p> </body> </html> then when all the values are submitted. what i want to do with them is 1st. check whether this user already has data in the table. if user has data lets say 3 universities and 3 degrees with his userid. then update all these values and if user wants to add 4th one then update the previous 3 and add the extra 4th university and degree in the table. here is the code of all this <?php $_SESSION['userid'] = 1; $university = $_POST['university']; $degree = $_POST['degree']; $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); //print_r($_POST); $get = "SELECT * FROM university WHERE userid='$_SESSION[userid]'"; $get_result = mysql_query($get); $count = mysql_num_rows($get_result); $uni = $count['university']; echo $uni; if($count > 0) { while($row = mysql_fetch_assoc($get_result)) { $uni = $row['university']; $deg = $row['degree']; //echo $univers."<br>"; } for($i = 0; $i < count($_POST['university']); $i++) { $uni = $university[$i]; $deg = $degree[$i]; $update = "UPDATE university SET university='$uni', degree='$deg' WHERE userid='$_SESSION[userid]'"; //$result_update = mysql_query($update); } } if user doesnt have any data in the table then simply insert the data in the table. code for this is here else { for($i = 0; $i < count($_POST['university']); $i++) { $uni = $university[$i]; $deg = $degree[$i]; //echo $uni.": ".$deg."<br>"; $add = "INSERT INTO university VALUES ('','$_SESSION[userid]','$uni','$deg')"; //echo $add."<br>"; //$result = mysql_query($add); } } ?> above code works perfectly but the problem is with update. when i try to update it updates all the fields with userid currently logged in with only one value from the table whereas my php script gets all the values from html form. so how can i update this dynamic form? my complete code is here <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $_SESSION['userid'] = 1; $university = $_POST['university']; $degree = $_POST['degree']; $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); //print_r($_POST); $get = "SELECT * FROM university WHERE userid='$_SESSION[userid]'"; $get_result = mysql_query($get); $count = mysql_num_rows($get_result); $uni = $count['university']; echo $uni; if($count > 0) { while($row = mysql_fetch_assoc($get_result)) { $uni = $row['university']; $deg = $row['degree']; //echo $univers."<br>"; } for($i = 0; $i < count($_POST['university']); $i++) { $uni = $university[$i]; $deg = $degree[$i]; $update = "UPDATE university SET university='$uni', degree='$deg' WHERE userid='$_SESSION[userid]'"; //$result_update = mysql_query($update); } } else { for($i = 0; $i < count($_POST['university']); $i++) { $uni = $university[$i]; $deg = $degree[$i]; //echo $uni.": ".$deg."<br>"; $add = "INSERT INTO university VALUES ('','$_SESSION[userid]','$uni','$deg')"; //echo $add."<br>"; //$result = mysql_query($add); } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 21, 2010 Author Share Posted February 21, 2010 currently my table has following data. It means that currently logged in user with userid 1 has three degrees from three universities. Now let suppose if he wants to edit first one so i need an update query and i explained above what happens when i try update. and other thing is what if he wants to edit 1st entry and add an extra degree with another university. in this case i ll need one insert and one update query. how can this be done. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 It sounds from the description of the problems you are having, that you have an issue with your table structure. To allow an individual to user to record many degrees with many univesities, you need to have the universities attended, and the degrees awarded in separate tables. So ,for example, the degrees table would have a field for user_id, to link it to the user, but also a unique Id of its own. So this unique id is what you would use for your update queries Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 21, 2010 Author Share Posted February 21, 2010 It sounds from the description of the problems you are having, that you have an issue with your table structure. To allow an individual to user to record many degrees with many univesities, you need to have the universities attended, and the degrees awarded in separate tables. So ,for example, the degrees table would have a field for user_id, to link it to the user, but also a unique Id of its own. So this unique id is what you would use for your update queries you mean like this table name= universities uniid userid uniName table name=degrees degreeid userid degree Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 yes and the uinque id can be used for the update of that particular entry Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 21, 2010 Author Share Posted February 21, 2010 quite complex i am not sure how this can be done. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.