Page 1 of 1

Need simple PHP help

Posted: Sun Dec 28, 2008 2:57 am
by prawn_86
Hi all,

let me preface this by saying i have never written a php code in my life, but i do have minimal experience with javascript so programming isn't completely foreign for me.



Im using Dreamweaver CS3 and i want a code that does the following:

I have 2 text boxes, one called 'name', the other 'link', when I input both these values in i want 'name' to appear on my page and it to be hyperlinked to 'link'.

I also want the date they were added to appear next to them.

And if possible, i would like them to only stay on my page for X number of days.


I know this is probably very simple, so if someone could point me to an easy tutorial or a simple way to do it, I would be very grateful.

What i have done so far:
Created a table in phpmyadmin with 3 attributes. Name, link and date


Thanks

Re: Need simple PHP help

Posted: Sun Dec 28, 2008 9:59 am
by cptnwinky

Code: Select all

 
# We will call this file action.php
# Ensure your form sends POST data
# and not GET data.
#
$name = addslashes($_POST['name']);
$link = addslashes(urlencode($_POST['link']));
 
if(!$name || !$link) {
    // Neither variable has been set
    // handle this error here.
    echo 'Error';
}else{
    // Ok so they exist. Add them to your database table
    // witch goes something like this.
    $query = "INSERT INTO table VALUES ('$name', '$url', NOW())";
    mysql_query($query) or die(mysql_error());
}
 
Lets call this file index.html

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="action.php">
    <label for="name">Name: </label><input type="text" title="name" name="name" />
    <label for="url">Url: </label><input type="text" title="url" name="url" />
    <input type="submit" title="submit" name="submit" />
</form>
 
<?php
$date = mysql_fetch_row(mysql_query("SELECT date FROM table WHERE name=some_condition"));
 
// Here is where you would take date and use it
// to determine what the date is of the item you
// want to get from the db. I won't do this here
// for brevity.
 
$query = "SELECT * FROM table WHERE date=$date";
 
$result = mysql_fetch_row(mysql_query($query));
 
// Remember the result should look like this
//      $result[0] = name
//      $result[1] = url
//      $result[2] = date
 
echo "<a href='$result[1]'>$result[0]</a>";
?>
 
</body>
</html>
 
Now all that is pretty sloppy and probably contains errors but it gives you a basic understanding of what needs to happen to make your idea work.

Re: Need simple PHP help

Posted: Sun Dec 28, 2008 10:01 am
by cptnwinky
Oh, and that script assumes you already have a connection to the database and the first php script would be named action.php.

Re: Need simple PHP help

Posted: Sun Dec 28, 2008 3:34 pm
by prawn_86
Wow, thanks so much for the detailed reply cptnwinky :)

I will try that later today and report back. I forgot to mention that i had already created the index.htm page, but thanks again!

Re: Need simple PHP help

Posted: Wed Dec 31, 2008 5:18 pm
by prawn_86
Ok another question:

If i want something simple like a link to be displayed once the php script is run, how do i do that? As i have tried making a little html doc part of the php but it never shows once 'action.php' runs.

Thanks

Re: Need simple PHP help

Posted: Wed Dec 31, 2008 5:31 pm
by cptnwinky
Since there's no error checking in my example above the script could be failing for many reasons. The file I mistakenly named index.html rather than index.php (or any other name .php) should display a link, as you see at the bottom of that example. Like I said though, without any error checking it could be failing on the query or the db connection or some other unforeseen error.

If you've modified the code or took the ideas from it and created your own, post the code here so I can make a better guess at why it's failing.

Re: Need simple PHP help

Posted: Wed Dec 31, 2008 7:13 pm
by prawn_86
Not sure if i am allowed to post links here, but essentially i want it to be much like the following site:

http://www.featureddomainsites.com/index.php

Re: Need simple PHP help

Posted: Wed Dec 31, 2008 7:56 pm
by cptnwinky
If your gonna code it yourself and need some help with specifics I'll be glad to help you but if you want me to write the script for you then PM me and we can work out the details.