Forum Moderators: coopster

Message Too Old, No Replies

undefined index for a multiple selection box generated from mysql

         

bogdanb

6:09 am on May 12, 2008 (gmt 0)

10+ Year Member



hi.
i have on my project a multiple selection list in a register form.
it is populated from a mysql database.

echo '<p>Countries I\'d Like to Work in * <select name="preferinte_tari" multiple size="7">
';
$cerereSQL = 'SELECT nume_tara FROM tari';
$rezultat = mysql_query($cerereSQL);

while($rand = mysql_fetch_row($rezultat))
{
print("<option value=\"$rand[0]\">$rand[0]</option>");
}
echo '</select>';

the script works fine,it populate the selection box, when i press the submit button sends the data to the mysql table, doesn't return any error if i select something.
but when i don't select anything and i press submit it returns this error:
Notice: Undefined index: preferinte_tari in C:\wamp\www\au pair\autentificare\inregistrare.php on line 590

i don't understand why. this is my 590 line: $_SESSION['preferinte_tari'] = $_POST['preferinte_tari'];
also the session is started in a config.php file which is called at the begining of the script.

this is my script:

<?php
require_once('config.php');
if(!isset($_GET['actiune'])) $_GET['actiune'] = '';
if(!isset($_SESSION['preferinte_tari'])) $_SESSION['preferinte_tari'] = '';
switch($_GET['actiune'])
{
case '':

echo '<p><form name="formular" action="inregistrare.php?actiune=validare" method="post">Countries I\'d Like to Work in * <select name="preferinte_tari" multiple size="7">
';
$cerereSQL = 'SELECT nume_tara FROM tari';
$rezultat = mysql_query($cerereSQL);

while($rand = mysql_fetch_row($rezultat))
{
print("<option value=\"$rand[0]\">$rand[0]</option>");
}
echo '</select>';
echo '<p align="justify"><input name="Submit" type="submit" id="Submit" value="Submit"></p>
</form>';
break;

case 'validare':
$_SESSION['preferinte_tari'] = $_POST['preferinte_tari'];
if(($_SESSION['preferinte_tari'] == ''))

{
echo 'Nu ai introdus date in formular sau cele introduse nu sunt corecte. <br>
Apasa <a href="inregistrare.php">aici</a> pentru a te intoarce la pagina anterioara.';
}
else
{
echo 'Va multumim. <br>
Datele au fost introduse cu succes in baza de date. <br>
Pentru a va autentifica apasati <a href="autentificare.php">aici</a>.';
$cerereSQL = "INSERT INTO `utilizatori`(`preferinte_tari`) VALUES( '".addentities($_SESSION['preferinte_tari'])."')";
mysql_query($cerereSQL) or die (mysql_error());
$_SESSION['preferinte_tari'] = '';
}
break;
}

?>

can you tell me please where i go wrong?

cameraman

8:06 am on May 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The error is telling you what you already know: that no selection has been made. You can test for the condition with the isset() function:
if(isset($_POST['preferinte_tari'])) {
$_SESSION['preferinte_tari'] = $_POST['preferinte_tari'];
.
.
.
}
else {
} // Course of action for no selection

bogdanb

2:09 pm on May 12, 2008 (gmt 0)

10+ Year Member



thanks.
that did it for me.
nice catch.
keep up the good work.

bogdanb

2:12 pm on May 14, 2008 (gmt 0)

10+ Year Member



i have a problem storing the info from the multiple selection box in the database.
i know i should make an array like that name="preferinte_tari[]" but i didn't figure how to write the sql statement.
i found this after searching the internet:

while (list($key, $val) = each ($Type)) {
$insertSQL = sprintf("INSERT INTO yourtable (yourfield) VALUES (%s)",
GetSQLValueString($val, "text"));
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error()); and other examples like that but i didn't manage to send the info to database.