I’m having a form where the user submits their site and in the email provided they are being emailed the code used to gather votes for their site.
Now I have a problem to find out how to email them the ID form the newly created record in the database so they can easily add the correct ID their link for it to work.
I tried adding another sql query asking for the id of the record that matches with the link that the user submitted, but I’m doing something wrong cause the $id never shows up in the mail.
Here is my code
$data = $wpdb->get_results( "SELECT link_id FROM wp_links WHERE link_url = " .$_POST['link_url']);
foreach ( $data as $row )
{
echo $GLOBALS['lid'] = $row->link_id;
}
$to = $_POST['link_notes'];
$subject = "Your toplist submission";
$txt = "Thanks for joining the Terraincrafters.com toplist. Here is your special link to collect votes from your visitors
http://www.terraincrafters.com/wp-content/plugins/wptoplist/counter.php?id=".$GLOBALS['lid'];
$headers = "From: postman@terraincrafters.com";
mail($to,$subject,$txt,$headers);
If you want to select only one row, use get_row() instead of get_results()
Your link inside query should be quoted (this is main reason of your trouble, i guess)
You should escape link value (as it comes from user input)
You don’t need a loop and echo, you need just an assignment
You should not use $GLOBALS, but just a regular variable
How i would modify your code:
$sql = $wpdb->prepare("SELECT link_id FROM wp_links WHERE link_url = '%s'", $_POST['link_url']);
$row = $wpdb->get_row($sql);
$lid = $row->link_id;
$to = $_POST['link_notes'];
$subject = "Your toplist submission";
$txt = "Thanks for joining the Terraincrafters.com toplist. Here is your special link to collect votes from your visitors
http://www.terraincrafters.com/wp-content/plugins/wptoplist/counter.php?id={$lid}";
$headers = "From: postman@terraincrafters.com";
mail($to,$subject,$txt,$headers);
PS: here is more info about working with database in wordpress
Thanks. I ended up using the mysql_insert_id() istead. Is that okay for just getting the ID ?
Like this
$to = $_POST['link_notes'];
$subject = "Your toplist submission";
$txt = "Thanks for joining the Terraincrafters.com toplist. Here is your special link to collect votes from your visitors.
Click it now to get your first vote! http://www.terraincrafters.com/wp-content/plugins/wptoplist/counter.php?id=". mysql_insert_id();
$headers = "From: postman@terraincrafters.com";
mail($to,$subject,$txt,$headers);