samf_20 Posted July 31, 2008 Share Posted July 31, 2008 Hi, Im fairly new to using Php and recently created a multiple select form which then writes the data from the form into a mysql database. Once I select a few options on the multiple select form and press submit, the information that is highlighted does get stored into the database but get the following error message appears every time unless I highlight all the fields in the form. The error message is: Notice: Undefined offset: 1 in *** on line 16 Notice: Undefined offset: 2 in *** on line 16 Notice: Undefined offset: 3 in *** on line 16 Notice: Undefined offset: 4 in *** on line 16 Notice: Undefined offset: 5 in *** on line 16 Notice: Undefined offset: 6 in *** on line 16 Which I believe is something to do with the array not being declared. The code for both forms is as following: <html> <body> <form method="post" action="sqlsess.php"> <select name="ilm[]" size="7" multiple="multiple"> <option value="ilmjan"> January </option> <option value="ilmfeb"> February </option> <option value="ilmapr"> April </option> <option value="ilmmay"> May Buyers Guide </option> <option value="ilmjuly"> July </option> <option value="ilmaug"> August </option> <option value="ilmoct"> October </option> </select> <input type="submit" value="Submit"> </form> </body> </html> <?php $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $ilm = $_POST['ilm']; foreach($ilm as $value) { echo $value."\n"; } $Query = "INSERT INTO selectilm (ilm1, ilm2, ilm3, ilm4, ilm5, ilm6, ilm7) VALUES ('$ilm[0]', '$ilm[1]', '$ilm[2]', '$ilm[3]', '$ilm[4]', '$ilm[5]', '$ilm[6]')"; $result = mysql_query($Query) or die ("Error in query: $Query. ". mysql_error()); /*if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";*/ mysql_close($con) ?> I know its probably pretty basic - But any help would be appriciated:) Cheers Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 31, 2008 Share Posted July 31, 2008 Change this: $ilm = $_POST['ilm']; foreach($ilm as $value) { echo $value."\n"; } To this: $tmp_ilm = $_POST['ilm']; foreach($tmp_ilm as $value) { $ilm[] = $value; } Quote Link to comment Share on other sites More sharing options...
samf_20 Posted July 31, 2008 Author Share Posted July 31, 2008 Cheers, its only got rid of the Notice: Undefined offset: 1 in *** on line 16 Out of all the 6 seperate ones that appear. So now getting just the 5 starting at "2" and ending at "6" Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 31, 2008 Share Posted July 31, 2008 Place this print_r($ilm); after the foreach() loop, and see what it outputs. Quote Link to comment Share on other sites More sharing options...
samf_20 Posted July 31, 2008 Author Share Posted July 31, 2008 Having tried and tested it, it seemed to make no difference to the output of error messages Quote Link to comment Share on other sites More sharing options...
samshel Posted July 31, 2008 Share Posted July 31, 2008 What are you exactly trying to do? If i select all 7 options, it will insert them in 7 fields in 1 record, right? If i select only 3 options say ilmjan ilmfeb ilmmay it will insert these three in first 3 columns ?? OR it will insert jan in first , feb in second and may in fifth and keep others empty ? Quote Link to comment Share on other sites More sharing options...
samf_20 Posted July 31, 2008 Author Share Posted July 31, 2008 Yes, if you select jan feb and may. It will only insert them 3 and keep the rest of the fields blank. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 31, 2008 Share Posted July 31, 2008 Well the remaining errors will be because you haven't selected enough months and it is trying to grab that number from the array. You may need to make the query dynamic. Quote Link to comment Share on other sites More sharing options...
samshel Posted July 31, 2008 Share Posted July 31, 2008 Yes, if you select jan feb and may. It will only insert them 3 and keep the rest of the fields blank. it will insert these three in first 3 columns ?? OR it will insert jan in first , feb in second and may in fifth and keep others empty ? Quote Link to comment Share on other sites More sharing options...
samf_20 Posted July 31, 2008 Author Share Posted July 31, 2008 At the moment it would be inserted into the first 3 columns, not a assigned field. Its abit of a flaw but im going to try sort that out Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 31, 2008 Share Posted July 31, 2008 Try this code and let me know if it fixes it... <?php $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $ilm = $_POST['ilm']; foreach($ilm as $value) { $values[] = $value; } for($i = 0; $i < count($values); $i++){ $fields[] = "ilm".$i+1; //so it starts at 1 etc... } $Query = "INSERT INTO selectilm (".implode(',',$fields).") VALUES (".implode(',',$values).")"; $result = mysql_query($Query) or die ("Error in query: $Query. ". mysql_error()); /*if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";*/ mysql_close($con) ?> Not tested. Quote Link to comment Share on other sites More sharing options...
samf_20 Posted July 31, 2008 Author Share Posted July 31, 2008 Ive tried and its fixed the initial problem, But caused several more ha :-\ Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 1, 2008 Share Posted August 1, 2008 Like? Could you explain what these new errors are? 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.