verdrm Posted March 19, 2008 Share Posted March 19, 2008 Can anyone explain how to make an array using textfields? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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."')"); } Quote Link to comment 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? Quote Link to comment 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'])."')"); Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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."')"); } Quote Link to comment 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. Quote Link to comment 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? 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.