Pass MySQL row ID to form...

User 2395225 Photo


Registered User
10 posts

So, let me start by saying I dont know what I'm doing :) I'm a mechanical guy, definitely not a programmer type.

I created an internal website at work a couple years ago, using WFB 1.2 - It's working great so far. Once the user inputs their data, it submits it to a MySQL database, and then I have a few pages that display the various tables. Once the data is submitted, the database assigns the row ID, and we use this ID to track each form submission in our department. One of the things I'm continually asked to do is to include the database row ID in the subject line of the form auto-response email. I've been trying to figure this out for the past couple of days and am just not having any luck.

The closest I've come is to manually modify the form.cfg.php file, to query the database for the latest row #. The query I have works fine, and will return the correct # when I manually view this PHP file in the browser. When I try to pass that variable to the Subject line of the email however, the form errors out.

Maybe this cant be done, maybe I'm just totally looking at it wrong, I dont know. Does anyone have any ideas? Below is the pertainent portion of the form.cfg.php file I've modified:

<?php
$username = "-----";
$password = "-----";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
mysql_select_db('engineering');
$highest_id = mysql_result(mysql_query("SELECT MAX(_rowid_) FROM engineeringrequest_test"), 0);
echo $highest_id;

?>
{
"settings":
{
"data_settings" :
{

-------------
"auto_response_message" :
{
"custom" :
{
---
"subject" : "<?php echo $highest_id; ?>" <----------This is where I'm trying to pass the variable off to. Viewing this file manually it outputs the correct value, but the form doesnt like it.

Thanks!
User 187934 Photo


Senior Advisor
20,271 posts

Your going to have a tough time doing this by altering the form files. You might be better of making your own php script to process the data and display it back to the user. If you want to try it I think your going to want to try to set a $_SESSION variable to carry to the confirmation.
Try this on your form.cfg.php in replace of the echo $highest_id
$_SESSION['highest_id'] = $highest_id;

Then on the confirmation echo the $_SESSION
echo $_SESSION['highest_id'];

You may need to add
session_start();
to the top of these pages. You'll also want to unset the session on the confirmation page.
Place this at the bottom of the confirmation page
unset($_SESSION['highest_id']);

I would also tweak your mysql query to be PDO and to look at more then just the latest row so you can return the actual data the user submitted.

Here's a PDO
<?php
$hostdb = 'localhost';
$namedb = 'engineering';
$userdb = 'database username';
$passdb = 'database password';
try {
// Connect and create the PDO object
$pdo = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$pdo->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
//Grab submitted data from the table
$statement = $pdo->prepare("SELECT _rowid_ FROM engineering ");
$statement->execute();

$pdo = null; // Disconnect
}
catch(PDOException $e) {
echo $e->getMessage();
}
while($row = $statement->fetch(PDO::FETCH_ASSOC)){
$_rowid_ = $row['_rowid_'];
}
$_SESSION['highest_id'] = $_rowid_;
?>
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.