verdrm Posted March 19, 2008 Share Posted March 19, 2008 Can anyone explain how to make an array using textfields? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/ Share on other sites More sharing options...
kenrbnsn Posted March 19, 2008 Share Posted March 19, 2008 Your question is unclear. Please ask it again, explaining what you are looking to do. Ken Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-495563 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 I have a bunch of text fields with the same purpose (ex: responses to a ballot question), and I would like the insert them into MySQL. The number of text fields, however, can fluctuate because I have a JavaScript function to add more text fields. I assume I would use an array to get the # of text fields and then insert them? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497601 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 Use the same form field name with empty square brackets to get an array of values. <input type="text" name="ballot_answer[]" /> <input type="text" name="ballot_answer[]" /> <input type="text" name="ballot_answer[]" /> <input type="text" name="ballot_answer[]" /> Then in PHP, $_POST['ballot_answer'] will php a numeric array you can work with. Does that help? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497609 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 Since I am new to arrays, can you explain what happens after $_POST['ballot_answer']? What are the steps after that to getting it into an INSERT statement? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497617 Share on other sites More sharing options...
papaface Posted March 21, 2008 Share Posted March 21, 2008 Something like: foreach ($_POST['ballot_answer'] as $value) { mysql_query("INSERT INTO `tblname` (answers) VALUES ('".$value."')"); } Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497624 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 How would I take all the values in the array and insert them once in the same MySQL record with commas? For example, if I have response a,b,c,d....instead of inserting each in a new record how would I just do: a,b,c,d in one record? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497632 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')"); Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497659 Share on other sites More sharing options...
papaface Posted March 21, 2008 Share Posted March 21, 2008 mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')"); As far as I am aware that will not work. Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497698 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')"); As far as I am aware that will not work. Why not? Implode just makes a string, that is then stored into the 'answers' column, which will need to be of datatype varchar or text Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497713 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 What will work then? I haven't tried this yet. Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497715 Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 Both should work. However I'd go with storing answers in separate rows instead of all in a single comma-separated field. It's a much better db design. [pre] Answers Method A -----+----------+--------+ user | question | answer | -----+----------+--------+ 1 | 1 | a | Q Which users answered "c" to Q2 ? 1 | 2 | c | 1 | 3 | b | A SELECT user FROM answers 1 | 4 | a | WHERE question=2 AND answer='c' 1 | 5 | c | 2 | 1 | c | 2 | 2 | a | 2 | 3 | b | 2 | 4 | c | 2 | 5 | b | 3 | 1 | b | 3 | 2 | c | 3 | 3 | b | 3 | 4 | a | 3 | 5 | b | Answers Method B -----+-------------------+ user | answers | -----+-------------------+ 1 | a,c,b,a,c | Q Which users answered "c" to Q2 ? 2 | c,a,b,c,b | 3 | b,c,b,a,b | A Ermmmm! Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497775 Share on other sites More sharing options...
papaface Posted March 21, 2008 Share Posted March 21, 2008 mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')"); As far as I am aware that will not work. Why not? Implode just makes a string, that is then stored into the 'answers' column, which will need to be of datatype varchar or text As Barand said, both will work. But this one is better than the other: foreach ($_POST['ballot_answer'] as $value) { mysql_query("INSERT INTO `tblname` (answers) VALUES ('".$value."')"); } Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497780 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 I agree, I want to do it that way without the commas, but I wanted to see both ways of doing it. Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497795 Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 How do I prevent blank textfields from being INSERTED in that array? Link to comment https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.