Php email database values just created

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);

Hopefully someone can help me with this :smile:

  1. If you want to select only one row, use get_row() instead of get_results()
  2. Your link inside query should be quoted (this is main reason of your trouble, i guess)
  3. You should escape link value (as it comes from user input)
  4. You don’t need a loop and echo, you need just an assignment
  5. 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);

It’s ok if your column link_id is auto increment (you didn’t state this in first post).
But in that case i’d prefer to use

$wpdb->insert_id

it’s more essential way if you’re using $wpdb to insert rows

1 Like

Thats awesome, I will. Thanks :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.