mikeabe Posted December 10, 2008 Share Posted December 10, 2008 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    </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 Quote Link to comment https://forums.phpfreaks.com/topic/136363-solved-explode-send-name-and-id-to-mysql/ Share on other sites More sharing options...
rhodesa Posted December 10, 2008 Share Posted December 10, 2008 you break it up, but then don't use the values.... $sid=$part[0]; $student=$part[1]; $sql="INSERT INTO studin (sid, student) VALUES ('$sid','$student')"; Quote Link to comment https://forums.phpfreaks.com/topic/136363-solved-explode-send-name-and-id-to-mysql/#findComment-711424 Share on other sites More sharing options...
mikeabe Posted December 10, 2008 Author Share Posted December 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/136363-solved-explode-send-name-and-id-to-mysql/#findComment-711498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.