Jump to content

Archived

This topic is now archived and is closed to further replies.

t.bo

newbie Q: Need to make and display link + page in Database

Recommended Posts

Hello everybody,

I'm a noob with PHP but slowly im workin my way around. I have a problem though. I need to have a form where you can create a new link on a page that links to another page with text that you also have to input in the form. I also need a delete function.
I cant find it on the internet and tutorials are not helping me.
Can anyone help me out, guide me trhough the process or send me to a good URL with a bit of the code?

Thanks in advance

T.bo

Share this post


Link to post
Share on other sites
sorry i dont understand wat u mean, got any examples, or can u explain a bit more.

might be me, as im half asleep.. lol

Share this post


Link to post
Share on other sites
Well im makeing this site [url=http://www.euroclinix.com]http://www.euroclinix.com[/url]
When you go to "behandelingen" (wich means treatments) you see all the possible links to treatments above. (botox, ...)
But now I want to make it possible to make new treatments with their new page and display the new link next to the other treatments.

Hope this helps cuz I really need this one.

greetz

Share this post


Link to post
Share on other sites
Its up to you, you could try looking around for a pre-built CMS (Content management system) or try to make your own smaller version, either way your going to need a database.

you are basically going to have a database table containing a treatment title, the contents of the page about the treatment and any other info.

You would then have a page that would pull all the titles from a database and show a link to them.

Alternatively, you could investigate using file uploading if you are wanting people to upload their own html files, and then you would probably only need to store the url of the file and the title in a database.

However you do it, its not going to be all that simple, perhaps it might be better trying out some other php tutorials to get a better understanding of it first.

Share this post


Link to post
Share on other sites
Thank u for the reply!!

Im one step further now.
But I have problem with making the correct links, it should be something like this <a href="index.php?content=treatment&id="$id">
That should make a link but how can I display the content that matches the id when the link is clicked?

Share this post


Link to post
Share on other sites
$content = $_GET[content];
$id = $_GET[id];
$sql = mysql_query("SELECT * FROM `table` WHERE `content`='$content' and `id`='$id'");
then grab the information you need and display it.

Share this post


Link to post
Share on other sites
Damn,
its still kinda hard for me...

If I grab the info like u said. Put it in the variables...ok

But then I dont know how I can make the link work
<a href="?id=$id>link to treatment<a>
when I click the link the content should be $content.
How can i do that?

Share this post


Link to post
Share on other sites
Do you already have a database with the content for each treatment each with a corresponding id?

Share this post


Link to post
Share on other sites
Yes exactly , so its just giving problems for a correct output...

Share this post


Link to post
Share on other sites
Well you have to query the database to get the IDs that you want e.g.
[code]
<?
$sql = mysql_query("SELECT * FROM `yourtable'");
while($row = mysql_fetch_assoc($sql))
{
$title = $row['titlefield'];
$id = $row['idfield'];
echo "<a href='page.php?id=$id'>$title</a>";
}
?>
[/code]

And then on "page.php":

[code]
<?
$id = $_GET[id];
$sql = mysql_query("SELECT * FROM `yourtable` WHERE and `idfield`='$id'");
then grab the information you need and display it.
?>
[/code]

You will have to modify the table and field names.

Share this post


Link to post
Share on other sites
thanks Im gonna try it later
Hope it works

grtz

Share this post


Link to post
Share on other sites
Damn it didn't work.

The variables stay empty I think.
This is the table :
CREATE TABLE `yourtable` (
  `idfield` int(11) NOT NULL auto_increment,
  `titlefield` varchar(250) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`idfield`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

The table already has inputs...

This is the php :
Page with the links :
[code]<?
include('dbconnect.php');
$sql = mysql_query("SELECT * FROM `yourtable'");
while($row = mysql_fetch_assoc($sql))
{
$title = $row['titlefield'];
$id = $row['idfield'];
echo "<a href='page.php?id=$id'>$title</a>";
}
?>
[/code]

Page.php  :
[code]
<?
include('dbconnect.php');
$id = $_GET[id];
$sql = mysql_query("SELECT * FROM `yourtable` WHERE and `idfield`='$id'");
$content = $_GET[content];
echo "$content";
?>
[/code]
I probably made a mistake with page.php but the page with the links should work a,d it does not...
Dbconnect is correct though

Share this post


Link to post
Share on other sites
Can anyone help me out?

I really need this one.

Greetz and thanks in advance.

Share this post


Link to post
Share on other sites
isn't content supposed to come from the database? if so:

$content = mysql_result($sql,'0',"content");
echo "$content";

Share this post


Link to post
Share on other sites
Hmm still doesnt work..

Is it possible that there is a problem with the ", ' and ` tags?

Share this post


Link to post
Share on other sites
Add an or die statement:

$sql = mysql_query("SELECT * FROM `yourtable` WHERE and `idfield`='$id'") or die(mysql_error());
$content = $_GET[content];
echo "$content";

Share this post


Link to post
Share on other sites
Ok now I get :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and `idfield`='1'' at line 1

Share this post


Link to post
Share on other sites
Ok I GOT IT  ;D

[code]<?
include('dbconnect.php');
$id = $_GET[id];
$sql = mysql_query("SELECT * FROM yourtable WHERE idfield='$id'") or die(mysql_error());
$r=mysql_fetch_array($sql);
$content = mysql_result($sql,'0',"content");
echo "$content";
?>

[/code]

Works perfect. Thanks for everything

Share this post


Link to post
Share on other sites
My input and output code works perfect... But now the DELETE code :

[code]<?
include('dbconnect.php');

if(!isset($cmd))
{

  $result = mysql_query("select * from yourtable") or die(mysql_error());
 

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

      $title=$r["titlefield"];
      $id=$r["idfield"];
   

      echo "<a href='treatedit.php?cmd=delete&idfield=$id'>$title - Delete</a>";
      echo "<br>";
    }
}
?>
<?
if($cmd=="delete")
{
    $sql = "DELETE FROM yourtable WHERE idfield='$id'";
    $result = mysql_query($sql) or die(mysql_error());
    echo "treat deleted";
}
?>
[/code]

It displays the correct links and gives the correct output (treat deleted). But it does not actually delete :-s

Can U help once again :-)

Thanks in advance

Share this post


Link to post
Share on other sites
Can anyone help me out?

I think there is a problem with the idfield & $id but I cant see it...

Thanks in advance

Share this post


Link to post
Share on other sites
Where is it getting the $id from? Was this supposed to come from a url? If so you will probably need to do:
$id = $_GET['id']

Share this post


Link to post
Share on other sites
Ok thanks again...
Should this piece of code be put in the beginning of the code or already in the delete section?

grtz

Share this post


Link to post
Share on other sites

×

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.