[SOLVED] Problem with a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

[SOLVED] Problem with a variable

Post by fccolon »

Hi,

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>
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

my first thought is why is there a comma preceding your data variables in the insert statement? Could that be messing things up?
Quote:

Code: Select all

<?php
$make_link = "INSERT INTO GPL_lsdd VALUES ('', '$sel_divid', '$did' )"; ?>
Try taking it out?
?>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

might wanna start by replacing

Code: Select all

if ($_POST[op2]= "select2")
with ...

Code: Select all

if ($_POST['op2']== "select2")
also looking at your form layout it looks like you are only ever passing one of the variables to the script at any time?!

you also need to make sure your sid column is auto_increment if you want it to generate its own code.
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post by fccolon »

Thanks for the pointers, I'll give them a go and let you know.

With ref to Waynes post -
The script does show one selection box at a time.....no reason why it can't show both boxes together, just me learning as I go along....however I did put an echo of the sel_divid var inside the elsif clause to ensure it's value wasn't being scrubbed because of this pass.

the sid variable will eventually be populated in a similar fashion to divid. I've left it out til I get this bit working.

I did think about having a link between season & division on a seperate table, but as the drivers in a division could change from season to season, one table to cover all seemed the best option.

Thanks to you both again
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

why are you using not equal (!=) ? that seems a bit confusing =)

im still not 100% sure what you're trying to do.. do you have a link to the site so i can see?
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post by fccolon »

I changed -

Code: Select all

$make_link = "INSERT INTO GPL_lsdd VALUES ('', '$sel_divid', '$did' )";
to

Code: Select all

$make_link = "INSERT INTO GPL_lsdd VALUES ('0', '$sel_divid', '$did' )";
and

Code: Select all

if ($_POST[op2]= "select2")
with ...

Code: Select all

if ($_POST['op2']== "select2")
but get the same result
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

ok, you can do this to save yourself some confusion: instead of using hidden form fields to tell you what submit button is clicked, use a different value for your submit button...

have a loook at this and see if it helps any. I left out the insert code, but added in the bit at the bottom to print out the vars you get after you do both pages.

Code: Select all

if(!$_POST[action]){  // Initial page load, action will be null
  $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> 
   <p><INPUT type="submit" name = "action" value="Select Division"></p> 

   </form>"; 


   $divid = $sel_divid;  
}
elseif($_POST[action] == "Select Division"){
echo $sel_divid;  // this would be null!!
echo $_POST[sel_divid];
  $sel_divid = $_POST[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]"> 
       <input type="hidden" name="sel_divid" value="$sel_divid">
       <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> 
        <p><INPUT type="submit" name="action" value="Select Driver"></p> 

   </form>"; 
   echo $sel_divid;
}
else {
  print_r($_POST[sel_drvid]);
  print"<br>\n";
  print"$_POST[div_id]";
}
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post by fccolon »

Thank you liljester........it works....put the insert code in and all runs smoothly

Now I can go and bodge the rest of it :)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

cool =)
Post Reply