Jump to content

[SOLVED] PHP + MYSQL Useful links page - Help


winmastergames

Recommended Posts

Hi

Im trying to make a page with useful links that are hyperlinked to other websites for a client and it also has a administration page where you can add new links to the page of useful links and it will also have a title and description.

Where should i start the script off i tried making one myself but it didnt work out it was linking to the same website (http://domain.com/http://www.google.co.nz) Like that i thought it might be easier just making a new one cause i lost the old one anyway

Link to comment
https://forums.phpfreaks.com/topic/80729-solved-php-mysql-useful-links-page-help/
Share on other sites

use a for loop to display each link and add in a line break.

 

something like:

 

<?php
// connect to db
// select db

$myquery = mysql_query("select * from tblName");

while($r=mysql_fetch_array($myquery)) {

$rowlink=$r["my_links"]; // put your field's name is where "my_links" is

echo "<a href=\"$rowlink\">$rowlink</a><br>";

}
?>

 

as far as the administration page; you would need to set-up a mysql update based on each user table.

Unless you already know how to setup a user login (or a keen to learn) I suggest just making a "add link" form and protecting it with .htaccess.

 

Do you want the links displayed like below?  It is very annoying to setup this kind of thing with a text file if you do not have the ability to use a database. 

 

PHP Forums(url title)

http://www.phpfreaks.com(url)

People on PHPFreaks will do the code so you don't have to(url description)

Unless you already know how to setup a user login (or a keen to learn) I suggest just making a "add link" form and protecting it with .htaccess.

 

Do you want the links displayed like below?  It is very annoying to setup this kind of thing with a text file if you do not have the ability to use a database. 

 

PHP Forums(url title)

http://www.phpfreaks.com(url)

People on PHPFreaks will do the code so you don't have to(url description)

 

Yes thats what I want And I do have access to a database well not really what im thinking of doing i just have it hosted on my webserver (The PHP and MYSQL bits and have Frames showing the data on my webserver there is alot of limitations on my clients webserver host is there any other way i can do this? not using the below blocked features

 

    *  customer CGI scripts

    * Frontpage extensions

    * PHP

    * ASP

    * MYSQL databases

    * Perl

    * SSI

 

Also im not the best really at MYSQL WITH PHP JUST TO MAKE THAT CLEAR so s can someone please help me out a bit more then normal Please :)

If you do not have scripting limitation; host a server side file on your site or a free  php hosting site and use a frame or iframe to display the page your hosting or some other site is hosting, in there site. I would say that is about your only option.

something a bit like this

| Website Name | Website Link | Description |

|                    |                  |                |

 

Kind of like that in Rows. Also This also needs to have a Admin page where the administrator can add links into this table

 

ok - take the original code I provided you and do this:

 

<?php
// connect to db
// select db

$myquery = mysql_query("select * from tblName");

while($r=mysql_fetch_array($myquery)) {

$rowwebsite=$r["their_website"]; // put your field's name is where "their_website" is
$rowlink=$r["their_link"]; // put your field's name is where "their_link" is
$rowdescription=$r["their_description"]; // put your field's name is where "their_description" is

echo "$rowwebsite | ";
echo "<a href=\"$rowlink\">$rowlink</a> | ";
echo "$rowdescription<br>";

}
?>

 

this will display the content from your database.

 

are you going to be adding each link yourself or do you want them to be able to login and add links?

I will have to be able to login and add links into the database to display them thanks

 

and where it has connect to db and select db do i just put a code something like this into there? or is it something else

<?
session_start();
$db_host = "Mysql_host";
$db_username = "username";
$db_password = "password";
$db_name = "database_name";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());
?>

 

what you wrote above for connecting is fine,

and for the admin page do something like

 

<?php
// Connect to MySQL DB

function insertLink(){
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
echo 'Name:<br>'."\n"
echo '<input type="text" name="name" size="20"><br>'."\n";
echo 'URL:<br>'."\n";
echo '<input type="text" name="url" size="20"><br>'."\n";
echo 'Description:<br>'."\n";
echo '<textarea name="description" rows="7" cols="20"></textarea><br>'."\n";
echo '<input type="submit" name="submit" value="Insert Link">'."\n";
echo '</form>';
}

if(empty($_POST['submit'])){
insertLink();
} else{
$name = $_POST['name'];
$url = $_POST['url'];
$description = $_POST['description'];
$sql = "INSERT INTO `table_name` 
(name, url, description) VALUES('$name', '$url', '$description')";
mysql_query($sql) or die(mysql_error());
echo '<a href="'.$url.'">'.$name.'</a> Added To The Database!';
}
?>

be sure to change the sql code to match your database,

for example where I wrote

INSERT INTO `table_name`
(name, url, description)

 

change table_name

to your table name

and change:

name to the column name where you want the name of the website

url to the column name of where you want the url of the website

description to the column name of where you want the description of the website

 

and protect the directory with this file with a .htaccess file or create a login script....

be sure to safe guard yourself against mysql injections by using mysql_real_escape_string() or php strip_tags().

 

update: never mind; that will not apply to you - since you are not allowing others to enter the content in your database - sorry about that.

Um it came up with these errors

 

Index.php

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\LINK GEN\index.php:1) in C:\xampp\htdocs\LINK GEN\index.php on line 4

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\LINK GEN\index.php on line 14

 

admin.php

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\xampp\htdocs\LINK GEN\admin.php on line 15

 

Whats wrong? and how will i insert the data posted by Northern Flame?

Because wouldnt the PHP script need to connect to the table

 

This is the finished code with the MYSQL connection

Index.php

<?php
// connect to db
// select db
session_start();
$db_host = "localhost";
$db_username = "Username_was_here";
$db_password = "Passsword_was_here";
$db_name = "Database_was_here";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());

$myquery = mysql_query("select * from tblName");

while($r=mysql_fetch_array($myquery)) {

$rowwebsite=$r["their_website"]; // put your field's name is where "their_website" is
$rowlink=$r["their_link"]; // put your field's name is where "their_link" is
$rowdescription=$r["their_description"]; // put your field's name is where "their_description" is

echo "$rowwebsite | ";
echo "<a href=\"$rowlink\">$rowlink</a> | ";
echo "$rowdescription<br>";

}
?>

 

And

Admin.php

<?php
// Connect to MySQL DB
session_start();
$db_host = "localhost";
$db_username = "Username_was_here";
$db_password = "Passsword_was_here";
$db_name = "Database_was_here";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());


function insertLink(){
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
echo 'Name:<br>'."\n"
echo '<input type="text" name="name" size="20"><br>'."\n";
echo 'URL:<br>'."\n";
echo '<input type="text" name="url" size="20"><br>'."\n";
echo 'Description:<br>'."\n";
echo '<textarea name="description" rows="7" cols="20"></textarea><br>'."\n";
echo '<input type="submit" name="submit" value="Insert Link">'."\n";
echo '</form>';
}

if(empty($_POST['submit'])){
insertLink();
} else{
$name = $_POST['name'];
$url = $_POST['url'];
$description = $_POST['description'];
$sql = "INSERT INTO `table_name` 
(name, url, description) VALUES('$name', '$url', '$description')";
mysql_query($sql) or die(mysql_error());
echo '<a href="'.$url.'">'.$name.'</a> Added To The Database!';
}
?>

if your not using or setting a session; why do you have session_start() - it belongs at the very first of your php document anyways or it does not work.

 

Northern Flame included the connection; you just have to put in your info, like you did in the first script.

 

<?php
// Connect to MySQL DB
session_start();
$db_host = "localhost";
$db_username = "Username_was_here";
$db_password = "Passsword_was_here";
$db_name = "Database_was_here";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());
?>

 

Update: There is a problem somewhere - hold on I looking for it.

 

There was a semi-colon missing - try this:

 

<?php
// Connect to MySQL DB
session_start();
$db_host = "localhost";
$db_username = "Username_was_here";
$db_password = "Passsword_was_here";
$db_name = "Database_was_here";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());


function insertLink(){
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
echo 'Name:<br>'."\n";
echo '<input type="text" name="name" size="20"><br>'."\n";
echo 'URL:<br>'."\n";
echo '<input type="text" name="url" size="20"><br>'."\n";
echo 'Description:<br>'."\n";
echo '<textarea name="description" rows="7" cols="20"></textarea><br>'."\n";
echo '<input type="submit" name="submit" value="Insert Link">'."\n";
echo '</form>';
}

if(empty($_POST['submit'])){
insertLink();
} else{
$name = $_POST['name'];
$url = $_POST['url'];
$description = $_POST['description'];
$sql = "INSERT INTO `table_name` 
(name, url, description) VALUES('$name', '$url', '$description')";
mysql_query($sql) or die(mysql_error());
echo '<a href="'.$url.'">'.$name.'</a> Added To The Database!';
}
?>

 

be sure to edit the connection variable; so it will work with your database.

yea i forgot to insert a ; but phpQuestioner corrected it  ;)

 

also,

you did not establish a MySQL connection!

all you did was create the variables,

replace

 

$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name, $conn) or die(mysql_error());

 

with

 

$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());

I tried it out admin.php works now to add the Links but i dont know exactly what to put into the MYSQL database so can you please give me SQL code to insert into the database that would really help :)

 

But in the index.php (Page to view the links) it comes up with this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\LINK GEN\index.php on line 13

 

This is the code sorry but i might of messed it up maybe?

<?php
// connect to db
// select db
$db_host = "localhost";
$db_username = "my_username";
$db_password = "my_password";
$db_name = "my_database";
$conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());

$myquery = mysql_query("select * from tblName");

while($r=mysql_fetch_array($myquery)) {

$rowname=$r["name"]; // put your field's name is where "their_website" is
$rowurl=$r["url"]; // put your field's name is where "their_link" is
$rowdescription=$r["description"]; // put your field's name is where "their_description" is

echo "$rowname | ";
echo "<a href=\"$rowurl\">$rowurl</a> | ";
echo "$rowdescription<br>";

}
?>

can anyone help me? if you want to see what its doing LIVE then goto the below links

index.php (view Links)

http://www2.winmastergames.com:82/LINK%20GEN/index.php

Admin.php (add Links)

http://www2.winmastergames.com:82/LINK%20GEN/admin.php

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.