Jump to content

[SOLVED] Explode: Send name AND id to mysql


mikeabe

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

Archived

This topic is now archived and is closed to further replies.

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