• I am trying to follow this tutorial:
    https://developers.google.com/maps/articles/phpsqlinfo_v3

    I have the map placed on my page, but I am trying to figure out where I need to place the .php code that inserts a row into my database? Also, where do I place the code that includes my username, password, and database name?

    I tried placing these as separate php files in my themes directory, but this did not work.

    Any suggestions?

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • 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.";
    		}
    	};
    Thread Starter plet9090

    (@plet9090)

    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 https://www.tothenationsworldwide.com/social-travel-map/)

    Where would I include this code you just showed me? into my functions.php?

    Thread Starter plet9090

    (@plet9090)

    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,

    Thread Starter plet9090

    (@plet9090)

    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.

    Thread Starter plet9090

    (@plet9090)

    The map is already completed, I have that done at https://www.tothenationsworldwide.com/social-travel-map/

    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.

    Thread Starter plet9090

    (@plet9090)

    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.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Connecting MySQL with php code in WordPress’ is closed to new replies.