1st, I am new to php (and coding in general) so forgive me if I dont explain correctly or my code looks naff
One of my variables appears to be losing it's value....or my mysql INSERT isn't working correctly.
The code has two selects
1st is to select a division, the division id is stored in $sel_divid & passed to var $divid.
2nd is to select 1 or more drivers to add to the division. The selected ids are stored in $sel_drvid[]
I then have a while loop to insert the selected info into a table called GPL_lsdd, which has 3 fields -
sid - int(4) - Season id
divid - int(4) - Division id
did - int(5) - Driver id
However, when I view the table after running the script, only the driver field is being populated. so records appear as -
0,0,1
0,0,2 etc etc
I've put echos of £sel_divid - and had ones for divid - and the division id is being stored in there, however I dont get any echo from within the while loop
Can someone point out where I'm going wrong ?
Code: Select all
<?php
$conn = mysql_connect("[i]server[/i]", "[i]user[/i]", "[i]password[/i]");
mysql_select_db("leaguedb", $conn);
if ($_POST[op1] != "select1")
{
$get_list = "select id, div_name from GPL_division";
$get_list_res = mysql_query($get_list) or die(mysql_error());
$display_block = "<h1>Add Driver to Division</h1>";
$display_block .= "
<form method="post" action="$_SERVER[PHP_SELF]">
<p><strong> Select Division:</strong><br>
<select name="sel_divid">
<option value="">-- Go on, pick your division --</option>";
while ($divrecs = mysql_fetch_array($get_list_res))
{
$id = $divrecs[id];
$div_name = stripslashes($divrecs[div_name]);
$display_block .= "
<option value="$id">$div_name</option>";
}
$display_block .= "
</select>
<input type="hidden" name="op1" value="select1">
<p><INPUT type="submit" value="Select"></p>
</form>";
$divid = $sel_divid;
}
elseif ($_POST[op2] != "select2")
{
echo $sel_divid;
$get_list = "select id, driver_name from GPL_drivers";
$get_list_res = mysql_query($get_list) or die(mysql_error());
$display_block = "<h1>Add Driver to Division</h1>";
$display_block .= "
<form method="post" action="$_SERVER[PHP_SELF]">
<p><strong> Select Drivers:</strong><br>
<select name="sel_drvid[]" multiple>
<option value="">-- Go on, pick your drivers --</option>";
while ($drvrecs = mysql_fetch_array($get_list_res))
{
$id = $drvrecs[id];
$driver_name = stripslashes($drvrecs[driver_name]);
$display_block .= "
<option value="$id">$driver_name</option>";
}
$display_block .= "
</select>
<input type="hidden" name="op2" value="select2">
<p><INPUT type="submit" value="Select"></p>
</form>";
echo $sel_divid;
}
if ($_POST[op2]= "select2")
{
$selects = sizeof($sel_drvid);
$i= 0;
echo $sel_divid;
while ($i < $selects)
{
$did = $sel_drvid[$i];
echo $i;
$i++;
$make_link = "INSERT INTO GPL_lsdd VALUES ('', '$sel_divid', '$did' )";
$make_link_res = mysql_query($make_link) or die(mysql_error());
}
}
?>
<html>
<head>
<title>Add Drivers to Division</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>