ilikephp Posted January 22, 2011 Share Posted January 22, 2011 Hi, I am using check box group in my php form, The user can choose multiple answers, if he choose 3 answers and the form is submitted only one answer is displayed in mysql GetSQLValueString($_POST['father'], "text"), any help plz? Thanks Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2011 Share Posted January 22, 2011 We'll need to see the form elements, too. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 22, 2011 Author Share Posted January 22, 2011 <input type="checkbox" name="father" value="answer 1" /> answer 1</label> <br /> <label> <input type="checkbox" name="father" value="answer 2" /> answer 2</label> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 23, 2011 Share Posted January 23, 2011 You need to set up the checkbox groups as arrays. For example using name="father[]" would put the values in $_POST['father'][1], $_POST['father'][2], etc. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 23, 2011 Author Share Posted January 23, 2011 I tried to put this in my code: in the insert to database: GetSQLValueString($_POST['father'][1], $_POST['father'][2]), In the form: <label> <input type="checkbox" name="father[]" value="answer 1" /> answer 1</label> <br /><label> <input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label> <br /> I got an error :S Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 23, 2011 Share Posted January 23, 2011 What was that error? Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 23, 2011 Author Share Posted January 23, 2011 Unknown column 'answer 2' in 'field list' Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 23, 2011 Share Posted January 23, 2011 That's a MySQL error . . . Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 23, 2011 Author Share Posted January 23, 2011 the code that I wrote in the previous reply was correct? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 23, 2011 Share Posted January 23, 2011 The code in your first post: GetSQLValueString($_POST['father'], "text"), indicates this function expects a value and a type. However, in your next post: GetSQLValueString($_POST['father'][1], $_POST['father'][2]), you are passing two values. What exactly are you trying to do with the result of the checkbox array? Are they supposed to go into a single field, or are there separate fields for each possible answer? Be aware that you may get any number of elements in the "father" array. And, in fact, the "father" element will NOT exist in the array if the user does not check any boxes. For example: <input type="checkbox" name="father[]" value="answer 1" /> answer 1</label> <br /><label> <input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label> <br /><label> <input type="checkbox" name="father[]" value="answer 3" /> answer 3 </label> <br /> If the user checks the first and third box, the resulting array will be: $_POST[father][0] = 'answer 1'; $_POST[father][1] = 'answer 3'; If you are trying to put all the values in a single field, then perhaps something like this will work: $fatherValue = (isset($_POST['father']) ? implode(',', $_POST['father']) : ''); GetSQLValueString($fatherValue, "text"), If you want them in separate fields; or even better, in separate rows of a child table, you will have to loop through each element and build and insert statement. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 23, 2011 Author Share Posted January 23, 2011 Thx for your help! I am trying to put the values of the fields checked in the mysql databse, for example: Languages spoken by the father: * German * English * French Many fields could be checked Should I use what you wrote? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 23, 2011 Share Posted January 23, 2011 We would have to know the structure of the database, specifically the table you are trying to put them into. The code snippets you provided do not indicate to structure of your database and tables. If you are trying to put them in a single field as a comma-separated string, then, yes, you would use the implode() function to build that string. You could give it a try and see if it does what you want. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted January 23, 2011 Author Share Posted January 23, 2011 I want them to be displayed in the same field with a comma to separate them. I put this code in the form: <input type="checkbox" name="father[]" value="answer 1" /> answer 1</label> <br /><label> <input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label> <br /> <?php $fatherValue = (isset($_POST['father']) ? implode(',', $_POST['father']) : ''); ?> and in the $insertSQL = sprintf("INSERT INTO .... GetSQLValueString($fatherValue, "text"), I dont know if I did something wrong but I am getting Null value in the Mysql database Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 24, 2011 Share Posted January 24, 2011 Echo your SQL just before you execute it. Take a close look at it to see if there is something wrong. Test the return value from the execution (i.e. mysql_query() or whatever) to see if it succeeded. -- If you're using mysql, you can echo mysql_error() to see the error message. Turn error reporting on in your PHP script to see if there are any PHP errors causing problems. 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.