Jump to content

How to add download counter to my code


larry29936

Recommended Posts

I have no idea why your dbname is not getting passed.  Did you try echoing it out from inside the function?

Where is this other error?  What is line 15?

AND WHY DO YOU KEEP EDITING MY CODE?  Do NOT include the db name in anything but the call to the connect!!

Actually change the function header to this:

function PDOConnect($l_dbname=NULL, $l_msg=null, $l_options=null)
And get RID of the database name you supplied lower down.  Tsk, tsk, tsk.

Edited by ginerjm
Link to comment
Share on other sites

// index.php

<?php
session_start();
// start of script every time.

//  setup a path for all of your canned php scripts
$php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree
//  load the pdo connection module  
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
//*******************************
//   Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))           <---- was missing closing quote per php syntax checker
{
    echo "Failed to connect to database" ;              <----- was missing ; per php syntax checker 
    //  exit?
}
else
{
    $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip');   <-- syntax copied from phpmyadmin
    // use exec() because no results are returned
    $pdo->exec($sql);
    echo "New record created successfully";
}
catch(PDOException $e)                                           <----- current error
{
    echo "Query failed to run properly: $sql <br><br>" . $e->getMessage();
}
exit();

?>

 

Link to comment
Share on other sites

The catch was copied from your second example near the bottom of page 1. I think it should be like this:


 

//*******************************
//   Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{
    echo "Failed to connect to database" ;
    //  exit?
}
elseif ($pdo = PDOConnect("foxclone_data"))
{
    $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip');
    // use exec() because no results are returned
    $pdo->exec($sql);
    echo "New record created successfully";
}
//catch(PDOException $e)
else
{
    echo "Query failed to run properly: $sql <br><br>" . $e->getMessage();
}
exit();

?>

 

Link to comment
Share on other sites

So - add some echos to check what is going on.  Or you could post the part of the current script that is making that connection call as well as the entire module that contains the connection code so that WE can see what you are seeing.  That would be so very very helpful.....

Link to comment
Share on other sites

Here's a link to a screenshot of the error when run on the host: d9PupPI.png[/url].

Here's the code I'm running that works locally:

Top of index.php:

<?php
session_start();
// start of script every time.

//  setup a path for all of your canned php scripts
$php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree
//  load the pdo connection module  
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';

//*******************************
//   Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data")):
{
    echo "Failed to connect to database" ;
    //  exit?
}
else:
{
    echo "Connected to database";
    $sql = "INSERT INTO `Logins` (`ip_address`) VALUES ('$ip')";
    // use exec() because no results are returned
    $pdo->exec($sql); 
}

endif;
//exit();

?>

<!DOCTYPE html>

PDO_Connection_Select.php:

<?php
function PDOConnect($l_dbname='', $l_msg=null, $l_options=null)
    
{
	if ($l_options == null)
	{	// set my default options
		$l_options = array(PDO::ATTR_EMULATE_PREPARES => false,
		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
		PDO::MYSQL_ATTR_FOUND_ROWS => true,
		PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
		PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
	}
	if ($l_dbname == null)
		$host="mysql:host=localhost;charset=utf8";
	else
		$host="mysql:host=localhost;dbname=$l_dbname;charset=utf8";
	$uid = "xxxxx";
	$pswd = "yyyyyy";
	try
	{
		$mysql = new PDO($host, $uid, $pswd, $l_options);
	}
	catch (PDOException $e)
	{
		if (strtoupper($l_msg) == "SHOWMSG")
			echo "Fatal Error<br>Failed to connect to mysql via PDO.  PDO Error msg is:<br>".$e->getMessage();
		else
			echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO.  Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'showmsg')";
		return false;
	}
	if (!$mysql)
		return false;
	else	// all worked - return handle to pdo connection.
		return $mysql;
}

?>

The structure is the same both locally and on the web host.

Link to comment
Share on other sites

Why is your pdoconnect header not what I gave you???

I don't know what the mention of "structure" has to do with this topic. Nor the photograph.  You're making it very difficult to help you.

The error message obviously comes from the connect logic so that is where the problem lies.  As for the main script - when you get a false result from the connect call why are you not exiting???   Add an echo to the pdoconnect logic to display the input dbname just to make sure you are actually executing this set of code.

And - I assume that you are supplying the uid and pswd in the connect code and only left it as xxx and yyy for me.

Edited by ginerjm
Link to comment
Share on other sites

Got it fixed. While I can connect to the database thru phpmyadmin using a uid of larry locally, to connect from PDO_Connect_Select I needed to use foxclone_larry.

Thanks for all your help and putting up with a newbie.

Edited by larry29936
Link to comment
Share on other sites

Now I need to figure out how to update my download table if they download a file. Hold on, I can do it all in the download table. It has the same fields as the login table plus a download name field. If I make that field to allow null, I can use it for logins and downloads. Need to modify the call in the index.php

Edited by larry29936
Link to comment
Share on other sites

The connection module is just THAT.  It is not a query module.  It is not an update module.  It is simply your permanent connection module.  You use it when you need to establish a connection and you keep it in play all during your script, if the script needs to communicate to the db.  That is it.  It is the one and only place you make a connection and that means you only have one place to maintain should there be any changes in your credentials or your database tool.  

Link to comment
Share on other sites

I'm having a problem with PDO. At the top of my index.php, I have the following:

<?php
session_start();
// start of script every time.

//  setup a path for all of your canned php scripts
$php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree
//  load the pdo connection module  
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';

//*******************************
//   Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data")):
{
    echo "Failed to connect to database" ;
    exit;
}
else:
{
    $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')";
    // use exec() because no results are returned
    $pdo->exec($sql); 
}

endif;
//exit();
?>

Further into index.php I have a download section that I want to record in the same table:

                   <?php
                    $files = glob('download/*.iso');
                    $file = $files[count($files) -1];
                    $filename =  basename($file);
                    $md5file = md5_file($file);
                   ?>

                    <div class="container">
                        <div class="divL">                       
                            <h3>Get the "<?php echo "{$filename}";?>" file (approx. 600MB)</h3>
                        <center>  <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt=""></a>  </center>
                                
                                    <?php
                                    $down=$filename
                                    $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')";      <-- parse error here
                                    $pdo->exec($sql); 
                                    ?>

I'm getting the following error: Parse error: syntax error, unexpected '$sql' (T_VARIABLE) in /home/foxclone/test.foxclone.com/index.php but don't understand why. Do I not need the $sql line?

Thanks in advance,
Larry

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.