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.";
}
};
If I wanted to do a google maps API that when you clicked on it, it added a new infowindow that can be saved into MySQL (map already created http://www.tothenationsworldwide.com/social-travel-map/)
Where would I include this code you just showed me? into my functions.php?
this code:
<?php
require(“phpsqlinfo_dbinfo.php”);
// Gets data from URL parameters
$name = $_GET[‘name’];
$address = $_GET[‘address’];
$lat = $_GET[‘lat’];
$lng = $_GET[‘lng’];
$type = $_GET[‘type’];
// Opens a connection to a MySQL server
$connection=mysql_connect (“localhost”, $username, $password);
if (!$connection) {
die(‘Not connected : ‘ . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die (‘Can\’t use db : ‘ . mysql_error());
}
// Insert new row with user data
$query = sprintf(“INSERT INTO markers ” .
” (id, name, address, lat, lng, type ) ” .
” VALUES (NULL, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’);”,
mysql_real_escape_string($name),
mysql_real_escape_string($address),
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($type));
$result = mysql_query($query);
if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}
?>
You could make it a function and put it in functions.php and then you can use the function where you want.
Are you opening a new connection to a differentdatabase than th ewordpress database? if so, why? (I’m curious)
I suggest you learn Jquery, because doing a clickable map from API will probably involve quite a lot of Jquery <– in/out –> with php.
Not sure it answers your question,
I have added the table to my database on myphpadmin with my host. I just want to connect the map to the table so everytime someone clicks and saves, it adds a new row to the table and saves the information.
So is the database the one defined for wordpress or a seperate database? If it is the same one, use the wordpress API to add things to your new table.
It is the same database. Sorry for my confusion. I added a table in the same database and want users to be able to add information to the table when clicking on the map and saving.
How do I go about this?
Look at my example again – you don’t want to give each user access to the database, the wordpress user already has that access. I would suggest that when they click on the map, you have code check that they are logged in so you can grab their name fron their user row in the wp-suers table, then do an insert into your new table with th einformation you want to store it in.