Jump to content

Error Message Problem (Undefined offset)


samf_20

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/117505-error-message-problem-undefined-offset/
Share on other sites

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 ?

 

 

 

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. :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.