Jump to content

Recommended Posts

Hi

 

I need to send a name AND an id from a drop down menu to two different columns in  MYsql. (The drop down menu is populated from MYsql).

 

 

 

This snippet of code gets the name AND id from the drop down menu:

echo '<option value="'.$nt[sid].'|'.$nt[snames].'">' .$nt['snames'].'</option>';

 

 

This snippet of code is intended to separate id from name:

$part = explode("|", $_POST['student']);
// $part[0] is now client_id
// $part[1] is now name
$sid=$part[0];
$student=$part[1];

 

 

 

Here's the complete code for the drop down menu:




<form action=explode_action.php method=post>
<table align=left bordercolor=black cellpadding=1 border=0 borderwidth=0>
<tr align=left>
<td><font size="3">Select Client &nbsp &nbsp</font></td>
<td>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error 
connecting to mysql');
$dbname = 'explode';
mysql_select_db($dbname);
$query="SELECT sid, snames FROM studout";
$result=mysql_query($query);
echo "<select name=student>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo '<option value="'.$nt[sid].'|'.$nt[snames].'">' .$nt['snames'].'</option>'; //gets id and name
} 
echo '</select>'; // closing list
?>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"> </td>
</tr>
</table>
</form>




 

 

And here's the complete action file code:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("implode", $con);
$part = explode("|", $_POST['student']);
// $part[0] is now client_id// $part[1] is now name
$sid=$part[0];
$student=$part[1];
$sql="INSERT INTO studin (sid, student)
VALUES
('$_POST[sid]','$_POST[student]')";  //posts but not in right place-needs 2 lines above
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
?>



 

This is the result in MYsql:

 

 

    | sid |      student    |

    |      |  1|Grundy Bill  |

 

So both the id and the name are going to the student column with a | between them.

 

 

What i'm trying to get is this:

 

    | sid |      student    |

    |  1  |    Grundy Bill  |

 

 

Can anyone see where i goofed up?

 

Thanks, Mike

 

Man, I LOVE this place!!

Thanks a million rhodesa

 

For anyone interested:

The code that makes the form is fine the way it's shown above

Here's the complete working action code:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("implode", $con);


$part = explode("|", $_POST['student']);
// $part[0] is now client_id// $part[1] is now name
$sid=$part[0];
$student=$part[1];

$sql="INSERT INTO studin (sid, student) VALUES 
('$sid','$student')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
?>

 

Thanks again, Mike

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.