Forum Moderators: coopster
I am trying to create different page <title> tags for each page.
I have tried this:
<title><?php print $title; ?> - blah, blah standard text</title>
This however only shows "- blah, blah standard text".
How do I get the <%PRODUCT_NAME%> to show up?
What code is required, and where do I put it?
ok i've figured out why it wasnt reading, its the way its setup on my server. I wont go into details. Anyway, i reverted back to your coding of: $query = "SELECT * FROM products";
And it comes up with:
Connected to MySQL!
Connected to Database!Title: Electrical Products
So the text in the first name field title now shows up. Great!
Whats the next step? How do i create the variable to the $id as the other code with this in it didnt work.
Cheers
example.com/database_test.php?id=4
Pass an id of 602...
example.com/database_test.php?id=602
<?
$username = your_username;
$password = your_password;
$database_name = your_database_name;
mysql_connect("localhost", $username, $password) or die(mysql_error());
echo "Connected to MySQL!<br />";
mysql_select_db($database_name) or die(mysql_error());
echo "Connected to Database!";
//change the username, password, and database_name to yours.
$id = $_GET['id']; //gets variable value from URL
$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
while($row = mysql_fetch_array($sql))
{
$title = $row['name'];
echo "Title: ".$title."<br>";
}
?>
If this works, don't think you're done! The variable still needs to be parsed for hacking attempts before used with regular people. But one step at a time.
<?
$username = your_username;
$password = your_password;
$database_name = your_database_name;
mysql_connect("localhost", $username, $password) or die(mysql_error());
echo "Connected to MySQL!<br />";
mysql_select_db($database_name) or die(mysql_error());
echo "Connected to Database!";
//change the username, password, and database_name to yours.
$id = $_GET['id']; //gets variable value from URL
$id = addslashes($id);
$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
$row = mysql_fetch_array($sql)
$title = $row['name'];
?>
...
<title><?php echo $title; ?></title>
addslashes() isn't bulletproof, but it helps. I would also make a MYSQL account with read-only access and use that username/password to connect to the database for your users.
<?
$username = your_username;
$password = your_password;
$database_name = your_database_name;
mysql_connect("localhost", $username, $password) or die(mysql_error());
mysql_select_db($database_name) or die(mysql_error());
//change the username, password, and database_name to yours.
$id = $_GET['id']; //gets variable value from URL
$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
while($row = mysql_fetch_array($sql))
{
$title = $row['name'];
}
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
Example page. Title should be on browser.
</body>
</html>
$id = $_GET['parent_id'];
if($id == "")
{$id = $_GET['product_id'];}
Ternary operator [php.net] is pretty cool here:
$id = empty($id) ? $_GET['product_id'] : $_GET['parent_id'];