Need help to add the same variable used in my 2nd php file? (PHP Files
Attached)
This is my 2nd Post about the same Topic, but I am adding both of the
files that I am using in this project!
I have created a script that allows users to create "Folder" using the
Textarea, and it creates also a table in my database using the same name
from the Textarea. But befor I started with this script it was not
creating new tables. I got some help by user Prix on here who helped me to
add the variable $name instead of the Text used befor...
The text used befor was called "visitor_tracking". And I just found
another php file that also includes the "visitor_tracking" and I need to
change it to the same $variable used in my first php file from the
textarea !!
This is my first and main php file which is already fixed and works fine:
<form action="" method="post" >
<table width="400" border="0" cellspacing="0" cellpadding="5">
<tr>
<td colspan="3" align="center">Please write gallery name and
description</td>
</tr>
<tr>
<td>Name </td>
<td> </td>
<td><input type="text" name="gname" id="text" value=""></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="submit"
id="submit" value="Submit"></td>
</tr>
</table>
</form>
<?PHP
if (isset($_POST['submit'])) {
$name = $_POST['gname'];
echo " ";
echo "Conguradulations. Your new URL is sucessfully created.!";
echo " ";
echo " ";
echo "Your new URL name is www.WebsiteName.com/$name";
}
$dbhost = 'HOSTNAME';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE ' . $name . '('.
'entry_id int(11) NOT NULL AUTO_INCREMENT, '.
'ip_address varchar(15) NOT NULL, '.
'visitor_id int(11) default NULL, '.
'page_name text, '.
'query_string text, '.
'timestamp timestamp NOT NULL default CURRENT_TIMESTAMP, '.
'PRIMARY KEY ( entry_id ), '.
'KEY visitor_id ( visitor_id , timestamp )) ENGINE=InnoDB DEFAULT
CHARSET=latin1 AUTO_INCREMENT=22 ;';
mysql_select_db('DATABASENAME');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
mysql_query($sql,$conn);
?>
As you can see above in the SQL Query the textarea is using the variable
$name to create the folder as a table name. I need to change "tracking"
text to the same variable that I am using in my first (main) php file
called $name, and I simply dont know how to do it! I tried many ways but
simply failed to do it!
And here is the file that I need help for to fix and add the $name
variable instead of visitor_tracker:
<?php
//define our "maximum idle period" to be 30 minutes
$mins = 30;
//set the time limit before a session expires
ini_set ("session.gc_maxlifetime", $mins * 60);
session_start();
$ip_address = $_SERVER["REMOTE_ADDR"];
$page_name = $_SERVER["SCRIPT_NAME"];
$query_string = $_SERVER["QUERY_STRING"];
$current_page = $page_name."?".$query_string;
//connect to the database using your database settings
include("db_connect.php");
if(isset($_SESSION["tracking"])){
//update the visitor log in the database, based on the current visitor
//id held in $_SESSION["visitor_id"]
$visitor_id = isset($_SESSION["visitor_id"])?$_SESSION["visitor_id"]:0;
if($_SESSION["current_page"] != $current_page)
{
$sql = "INSERT INTO visitor_tracking
(ip_address, page_name, query_string, visitor_id)
VALUES ('$ip_address', '$page_name', '$query_string',
'$visitor_id')";
if(!mysql_query($sql)){
echo "Failed to update visitor log";
}
$_SESSION["current_page"] = $current_page;
}
} else {
//set a session variable so we know that this visitor is being tracked
//insert a new row into the database for this person
$sql = "INSERT INTO visitor_tracking
(ip_address, page_name, query_string)
VALUES ('$ip_address', '$page_name', '$query_string')";
if(!mysql_query($sql)){
echo "Failed to add new visitor into tracking log";
$_SESSION["tracking"] = false;
} else {
//find the next available visitor_id for the database
//to assign to this person
$_SESSION["tracking"] = true;
$entry_id = mysql_insert_id();
$lowest_sql = mysql_query("SELECT MAX(visitor_id) as next FROM
visitor_tracking");
$lowest_row = mysql_fetch_array($lowest_sql);
$lowest = $lowest_row["next"];
if(!isset($lowest))
$lowest = 1;
else
$lowest++;
//update the visitor entry with the new visitor id
//Note, that we do it in this way to prevent a "race condition"
mysql_query("UPDATE visitor_tracking SET visitor_id = '$lowest'
WHERE entry_id = '$entry_id'");
//place the current visitor_id into the session so we can use it on
//subsequent visits to track this person
$_SESSION["visitor_id"] = $lowest;
//save the current page to session so we don't track if someone
just refreshes the page
$_SESSION["current_page"] = $current_page;
}
}
I am really crossing my fingers to get some help with this after so many
days of trying! Thank you very much in advance!
No comments:
Post a Comment