Jump to content

[SOLVED] Database Data, PHP and Variables


papaface

Recommended Posts

Hello,

I was wondering if someone can help me. Currently I have a html table in a database.

The html table has php in it which allows me to generate keys and things when I select it from the database.

However when I include it in my script the php tags in the html table do not get processed, so I end up with plain php tags.

This is my code

In a database field:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Please activate your membership </td>
  </tr>
  <tr>
    <td><a href="http://llalala.com/somepage.php?k=<?php $randomnumber; ?>u=<?php $username_gen; ?>">Click on this link to activate your membership </a></td>
  </tr>
  <tr>
    <td>or copy and paste this link to your browser: http://llalala.com/somepage.php?k=<?php $randomnumber; ?>&&u=<?php $username_gen; ?></td>
  </tr>
</table>

 

I work that into my code:

                $username_gen = base64_encode($username);
	$emailselect = mysql_query("select content_content from content where content_name='email_activation_content'");
	$emailfrom = mysql_query("select content_content from content where content_name='email_activation_from'");
	$emailsubject = mysql_query("select content_content from content where content_name='email_activation_subject'");

	list($email_content) = mysql_fetch_array($emailselect);
	list($email_from) = mysql_fetch_array($emailfrom);
	list($email_subject) = mysql_fetch_array($emailsubject);
	$to = $email_address;
	$subject = $email_subject;
	$message = $email_content;
	$headers="MIME-Version:1.0\n";
	$headers .="Content-type:text/html;charset=iso-8859-1\n";
	$headers .="From: ".$email_from."\n";
	if(mail($to, $subject, $message, $headers))
		{
		echo "We have now registered you as a member, thank you for registering, your username is " . $username;
		echo '<br>We have dispatched an activation email to the address provided. You must click on the link in the email to activate your membership.';
		}

 

Why when the html table is selected from the database isnt it processing the php?

and how can I get it to do it?

When the email is sent the user simply gets a link like this: http://llalala.com/somepage.php?k=<?php+$randomnumber;+?>&&u=<?php $username_gen;+?>

Sorry about my bad explanation lol.

Link to comment
https://forums.phpfreaks.com/topic/38914-solved-database-data-php-and-variables/
Share on other sites

If you have PHP code stored in the database then PHP will treat the code as a string. In order for PHP to parse the PHP code coming from a database you will need to pass through the eval function.

 

So you do this when you get the data from the database:

$email_content = eval("$email_content");

Thanks. I get this error though:

Parse error: parse error, unexpected '<' in somefile.php(16) : eval()'d code on line 1

I used this code as it is a scaled down version of my first post:

<?php

require("includes/connect.php");
require("includes/selectdb.php");

$username = "papaface";

$randomnumber = md5(mt_rand());

$username_gen = base64_encode($username);

$contents = mysql_query("select `content_content` from `content` where `content_name`='email_activation_content'");

list($content) = mysql_fetch_array($contents);

$email_content = eval("$content");

echo $email_content;

?>

The data it is trying to eval is:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Please activate your membership </td>
  </tr>
  <tr>somesite.com/somefile.php?k
    <td><a href="http://somesite.com/somefile.php?k=<?= $randomnumber ?>&&u=<?= $username_gen ?>">Click on this link to activate your membership </a></td>
  </tr>
  <tr>
    <td>or copy and paste this link to your browser: http://somesite.com/somefile.php?k=<?= $randomnumber ?>&&u=<?= $username_gen ?></td>
  </tr>
</table>

php.net says that I can use:

As with anything that outputs its result directly to the browser, you can use the output-control functions to capture the output of this function, and save it in a string (for example).

But how would i work that into this?

I have tried to work in the output buffers as so:

list($email_content) = mysql_fetch_array($emailselect);

	ob_start();
	eval("\$email_content = \"{$email_content}\";");
	$email_content = ob_get_contents(); 

	list($email_from) = mysql_fetch_array($emailfrom);
	list($email_subject) = mysql_fetch_array($emailsubject);
	$to = $email_address;
	$subject = $email_subject;
	$message = $email_content;

 

but i get this error:

Parse error: syntax error, unexpected T_LNUMBER in somepage.php(33) : eval()'d code on line 1

 

Any help will be appreciated.

It is easier if you remove the php code tags from the content in your database and wrap your php variables in curly braces instead ({$myvar});

 

Then when you use the eval code I supplied earlier it will parse the variables.

Okay, is this correct?

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Please activate your membership </td>
  </tr>
  <tr>
    <td><a href="http://some.com/activate.php?k={$randomnumber} ?>&&u={$username_gen}">Click on this link to activate your membership </a></td>
  </tr>
  <tr>
    <td>or copy and paste this link to your browser: http://some.com/activate.php?k={$randomnumber}&&u={$username_gen}</td>
  </tr>
</table>

somepage.php:

$emailselect = mysql_query("select content_content from content where content_name='email_activation_content'");
	$emailfrom = mysql_query("select content_content from content where content_name='email_activation_from'");
	$emailsubject = mysql_query("select content_content from content where content_name='email_activation_subject'");

	list($email_content) = mysql_fetch_array($emailselect);		
	eval("\$email_content = \"{$email_content}\";");
	//$email_content = eval("\$email_content = \"{$email_content}\";");

	list($email_from) = mysql_fetch_array($emailfrom);
	list($email_subject) = mysql_fetch_array($emailsubject);
	$to = $email_address;
	$subject = $email_subject;
	$message = $email_content;

 

because if it is, I get an email like this:

Please activate your membership

Click on this link to activate your membership

or copy and paste this link to your browser: http://some.com/activate.php?k={$randomnumber}&&u={$username_gen}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.