If you are doing this from with in WP you need to use the WP API and WP will hndle the calls. Here is an example I used (in a plugin I was using to track milage I walked on a virtual Applicaian Trail hike) to insert a row in a table I created in the WP database
//Submits data to the MYSQL database table wp_at_hike
if (isset($_POST['Submit'])) {
global $wpdb, $current_user;
get_currentuserinfo();
$trail_name = $current_user->display_name;
$year = mysql_real_escape_string($_POST['yeardata']);
$month = mysql_real_escape_string($_POST['monthdata']);
$day = mysql_real_escape_string($_POST['daydata']);
$time = 0;
$distance = mysql_real_escape_string($_POST['distancedata']);
$sql = "INSERT INTO wp_at_hike (trail_name,year,month,day,time,distance) VALUES ('$trail_name','$year','$month','$day','$time','$distance')";
if ($wpdb->query($sql) === FALSE) {
echo "Error!";
} else {
echo "Your entry was successfully added to your Trail Log.";
}
};