LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Home Alone with PHP, part 2: Lost in links! (https://www.linuxquestions.org/questions/programming-9/home-alone-with-php-part-2-lost-in-links-223043/)

Mega Man X 08-27-2004 08:07 AM

Home Alone with PHP, part 2: Lost in links!
 
Here I am, once again, trying to create an useful page with Apache, PHP, MySQL. This is SuSE 9.1 pro, so it's Apache 2.x and PHP4.

I'm trying to create a simple page: On the left you have a menu with three links: Home, Images and About. Clicking on these links, the content on the right table should change, displaying the data from the Database instead. That's exactly what does not work :(

Database
Code:

mysql> select * from main;
+----+----------+-----------------------------------------+
| id | header  | text                                    |
+----+----------+-----------------------------------------+
|  1 | home    | Welcome to my useless page              |
|  2 | images  | Here is some pictures of me            |
|  3 | about me | if there's anything you need to know... |
+----+----------+-----------------------------------------+
3 rows in set (0.00 sec)

The index.php itself
PHP Code:

<HEAD>
<TITLE> Welcome to my Personal Website </TITLE>
</HEAD>

<BODY>
<TABLE width="800" border="1" align="center">
  <TR valign="top" align="left">
    <TD width="150" bgcolor="#6495ED">
      <P align="center"><FONT color="White">..:: main ::..</P></FONT>
        <LI><A href="index.php?pageid=1">home</A></LI>
    <LI><A href="index.php?pageid=2">images</A></LI>
    <LI><A href="index.php?pageid=3">about me</A></LI>
    </TD>
    <TD width="650" valign="top" height="300">
    
    <?php
      $conn 
mysql_connect("localhost""root");
      
$rs   mysql_select_db("databas"$conn);
      if (!
$pageid) { $pageid=1; }
      
$sql  "select * from main where id = $pageid";
      
$rs   mysql_query($sql$conn);
      while(
$row=mysql_fetch_array($rs)) {
      
$tmp_image =$row["image"];
      
$tmp_header=$row["header"];
      
$tmp_text  =$row["text"];
      echo(
$pageid); // here is the problem and I can't think in a wise way to fix
      
}
      
    
?>
    
    </TD>
  </TR>

</TABLE>
</BODY>
</HTML>

Any help would be really much appreciated. Also, any good links, tutorials and howto's about creating and designing webpages with a database in focus would be lovely. I've been hunting google down for a long while now :(.

I can do a neat game list though :)

Thanks in advance, once again!

kev82 08-27-2004 08:47 AM

this looks remarkably like another register globals problem like the one we discussed in thread 214565

you cant access external variables the way you want to, if the variable is from a http post then it can be found in the $_POST array, if it is from an http get(like above) then it can be found in the $_GET array, same for cookies($_COOKIE array)

so you need to use $_GET['pageid'] everywhere or at the top of your script put $pageid = $_GET['pageid'];

rjcrews 08-27-2004 09:03 AM

why cant you just do

while($row=mysql_fetch_array($rs)) {
$tmp_image =$row["image"];
$tmp_header=$row["header"];
$tmp_text =$row["text"];
echo ($tmp_header); echo ($tmp_text);

i got it to work like that

Mega Man X 08-27-2004 03:55 PM

Thanks a lot kev82 (once again) and rjcrewsfor the input. I've got it to work using a bit of both ideas :). Here is how it looks like now:

PHP Code:

<HTML>
<HEAD>
<TITLE> Welcome to my Personal Website </TITLE>
</HEAD>

<BODY>
<TABLE width="800" border="1" align="center">
  <TR valign="top" align="left">
    <TD width="150" bgcolor="#6495ED">
      <P align="center"><FONT color="White">..:: main ::..</P></FONT>
        <LI><A href="index.php?pageid=1">home</A></LI>
    <LI><A href="index.php?pageid=2">images</A></LI>
    <LI><A href="index.php?pageid=3">about me</A></LI>
    </TD>
    <TD width="650" valign="top" height="300">
    
    <?php
      $conn 
mysql_connect("localhost""root");
      
$rs   mysql_select_db("databas"$conn);
      if (!
$pageid) { $pageid=$_GET['pageid']; }
      
$sql  "select * from main where id = $pageid";
      
$rs   mysql_query($sql$conn);
      while(
$row=mysql_fetch_array($rs)) {
      
$tmp_image =$row["image"];
      
$tmp_header=$row["header"];
      
$tmp_text  =$row["text"];
      echo(
$tmp_text);
      }
      
      
/*echo ("
      <INPUT TYPE=hidden NAME=pageid VALUE=$pageid>
      ");*/
      
    
?>
    
    </TD>
  </TR>
</TABLE>

</BODY>
</HTML>

As you see, I'm using both ideas. I'm still not quite sure when or how I've to use the variables of type $_POST and $_GET, or how different they are from ordinary variables. Sounds newbiesh, specially because I've coded in VB, C and Java before, but I'm a bit lost with PHP (still).

Thanks all again! I've been in this forum for a long time now, and the help here is like MasterCard ads: Priceless :)

Cheers!

Mega Man X 08-27-2004 05:04 PM

Cool. One more question. What if I need to use more then one table in the very same database. e.g

- I have one database called "databas", which has two tables: main and news.

- "main" holds the body of the page, meaning "home", "images" and "about". Like this:

Code:

mysql> select * from main;
+----+----------+-----------------------------------------+
| id | header  | text                                    |
+----+----------+-----------------------------------------+
|  1 | home    | Welcome to my useless page              |
|  2 | images  | Here is some pictures of me            |
|  3 | about me | if there's anything you need to know... |
+----+----------+-----------------------------------------+
3 rows in set (0.00 sec)

The table news, in another hand, will hold values of "date" and "text", like this:

Code:

mysql> select * from news;
+----+-------------------------------+----------------------------+
| id | date                          | text                      |
+----+-------------------------------+----------------------------+
|  1 | Friday 27 August 2004 - 23h29 | Nothing interesting yet :( |
+----+-------------------------------+----------------------------+
1 row in set (0.00 sec)

So, let's say that the user clicks in a link called "news" (or even home for that matter), how would I go by using the table "news" for the first link (pageid=1) and for all others, the table "main".

I cannot think straight how one would do making a page like this... what I want to do is kinda like a blog:

- Home -> with the descriptions of the page and it's updates
- Pictures -> the actual blog. Peoples are supposed to also leave messages in here
- About me -> just a text description of me
- Contact -> my ICQ, e-mail and stuff.

Should I do one table for every link since they have different data on it? I cannot really think straight (or think at all for that matter). I could simply use a finished blog out there, but they are all bad, and I want to learn PHP too :)

Thanks in advance once again!

Cedrik 08-27-2004 06:29 PM

There are many ways...
PHP Code:

// default page
$page "News";

//default text
$text_id 1;

// if the Page variable is set in url
if(isset($_GET["Page"])) { $page $_GET["Page"]; }

// if the Text ID variable is set in url
if(isset($_GET["Text"])) { $text_id $_GET["Text"]; }

// select the corresponding table
$table = ($page=="New") ?  "news" "main";

// example of query
$sql "SELECT text FROM $table WHERE id='$text_id'"

so the link to the news text (id 1) looks like :http://site.com/index.php?Page=News&Text=1

Mega Man X 08-27-2004 11:17 PM

Thanks Cedrik!

I really appreciate it. I will give it a whirl this weekend. I was taking a different approuch though, not a very wise one, mind you. Instead of having only one file on the server called index.php which updates itseft, I've created another one called send.php which acts as a Visit Book. It works, more or less, with two bugs. One being a simple HTML bug (I've not really used HTML for at least 3 years, so I need some help :)) and another being my horrible coding skills. Here is the situation:

1 - I've create a new table into databas, called "book". This table holds four values: ID, Name, Location and Message. It looks like this:

Code:

mysql> select * from book;
+----+-------+----------+------------------------------------+
| id | name  | location | message                            |
+----+-------+----------+------------------------------------+
|  1 | angel | sweden  | Testing again...                  |
|  8 | angel | sweden  | This page really looks horrible... |
+----+-------+----------+------------------------------------+
2 rows in set (0.00 sec)

and the page itself:

PHP Code:

<HTML>
<HEAD>
<TITLE>Post a Message</TITLE>
</HEAD>
<BODY>

<TABLE width="800" border="1" align="center">
  <TR valig="top" align="left">
    <TD width="150" bgcolor="#5495ED">
      <P align="center"><FONT color="white">..:: main ::..</FONT></P>
        <LI><A href="index.php?pageid=1"><FONT size="-1">home</FONT></A></LI>
        <LI><A href="index.php=pageid=2"><FONT size="-1">images</FONT></A></LI>
        <LI><A href="send.php"><FONT size="-1">about</FONT></A></LI>
    </TD>
    <TD width="650" valign="top" height="300">

    <?php
        $conn 
mysql_connect("localhost""root");
        
$rs   mysql_select_db("databas"$conn)
                   or DIE (
"Could not connect to database");
            
        
$rs   mysql_query("SELECT * FROM book ORDER BY id desc"$conn);
    
        
// display results
        
while ($row mysql_fetch_array($rs)) {
            echo (
"<P><I><FONT size=\"-1\">Posted by: </I>" .$row['name']     ."</FONT><BR>");
            echo (
"<P><I><FONT size=\"-1\">Location: </I>" .$row['location'] ."</FONT><BR>");
            echo (
"<P><I><FONT size=\"-1\">Message: </I>"   .$row['message']  ."</FONT><BR><HR>");
        }
    
        if (
$_POST['submit']){
            
$sql sprintf("INSERT INTO book (name, location,  message)
                   VALUES ('%s', '%s', '%s')"

                   
$_POST['name'], $_POST['location'], $_POST['message']);
        
            
$rs mysql_query($sql);
        
            echo 
"Message sent!\n";
        
        } else {
    
        
// display form
    
    
?>

    <FORM METHOD="post" ACTION="<?php echo $PHP_SELF ?>">
    <FONT size="-1">Name: </FONT><INPUT TYPE="text" NAME="name" SIZE="40"><BR>
    <FONT size="-1">Location: </FONT><INPUT TYPE="text" NAME="location" SIZE="40"><BR>
    <FONT size="-1">Message:</FONT><BR> <TEXTAREA COLS="35" ROWS="8" NAME="message">Leave a message :)</TEXTAREA><BR>
    <INPUT TYPE="submit" name="submit" value="Post Message">
    </FORM>
    
    <?php
    
// end if
    
}
    
?>

    </TD>
  </TR>
</TABLE>
</BODY>
</HTML>

Now to the bugs:

1 - It's an HTML problem that I cannot fix with all my lack of wisdom. The TD on the left (where the menu is) is wrapping, meaning that if too many posts are posted on the right (second TD), the menu gets all hippie, instead of staying on the top. the tag "nowrap=true" does not seems to work in this case...

2 - It's a PHP thing. If the user posts a message, "Message Sent" is displayed. No problems there. Problem is, the page won't update. If the refresh button is used, it will double post. The only way is to type again "send.php" on the browser...

Any ideas?

Thanks again, and sorry to annoy you all :). I just want to make a simple page to hear comments from friends and stuff. Well, now to the admin tools :'(.

Regards!

kev82 08-28-2004 06:24 AM

by Megaman X
I'm still not quite sure when or how I've to use the variables of type $_POST and $_GET, or how different they are from ordinary variables.

$_POST and $_GET are associative arrays, they contain the http post and http get input respectivly, you should use them ALWAYS well actually only when you want post/get data. read about the register globals directive for examples of why you should use them.

1) not sure what you mean but the menu doesnt stay at the top because of a typo

by Megaman X
<TR valig="top" align="left">

note the missing n

2) i would send an http refresh header but you cant do this as you produce output before any code is run, so ive changed the order of your code, ive left it in a mess though and it could use some tidying up.

PHP Code:

<?php
        $conn 
mysql_connect("localhost""root","uju5771983hy");
        
$rs   mysql_select_db("databas"$conn)
                   or DIE (
"Could not connect to database");

        if (
$_POST['submit']){
            
$sql sprintf("INSERT INTO book (name, location,  message)
                   VALUES ('%s', '%s', '%s')"
,
                   
$_POST['name'], $_POST['location'], $_POST['message']);
        
            
$rs mysql_query($sql);
        
         
//new code, causes page to reload immediatly
                 
header(sprintf("Refresh: 0;%s"$_SERVER['PHP_SELF']));
        
        }
?>
<HTML>
<HEAD>
<TITLE>Post a Message</TITLE>
</HEAD>
<BODY>

<TABLE width="800" border="1" align="center">
  <TR valign="top" align="left">
    <TD width="150" bgcolor="#5495ED">
      <P align="center"><FONT color="white">..:: main ::..</FONT></P>
        <LI><A href="index.php?pageid=1"><FONT size="-1">home</FONT></A></LI>
        <LI><A href="index.php=pageid=2"><FONT size="-1">images</FONT></A></LI>
        <LI><A href="send.php"><FONT size="-1">about</FONT></A></LI>
    </TD>
    <TD width="650" valign="top" height="300">

    <?php
        $conn 
mysql_connect("localhost""root","uju5771983hy");
        
$rs   mysql_select_db("databas"$conn)
                   or DIE (
"Could not connect to database");
            
        
$rs   mysql_query("SELECT * FROM book ORDER BY id desc"$conn);
    
        
// display results
        
while ($row mysql_fetch_array($rs)) {
            echo (
"<P><I><FONT size=\"-1\">Posted by: </I>" .$row['name']     ."</FONT><BR>");
            echo (
"<P><I><FONT size=\"-1\">Location: </I>" .$row['location'] ."</FONT><BR>");
            echo (
"<P><I><FONT size=\"-1\">Message: </I>"   .$row['message']  ."</FONT><BR><HR>");
        }
    
    
        
// display form
    
    
?>

    <FORM METHOD="post" ACTION="<?php echo $PHP_SELF ?>">
    <FONT size="-1">Name: </FONT><INPUT TYPE="text" NAME="name" SIZE="40"><BR>
    <FONT size="-1">Location: </FONT><INPUT TYPE="text" NAME="location" SIZE="40"><BR>
    <FONT size="-1">Message:</FONT><BR> <TEXTAREA COLS="35" ROWS="8" NAME="message">Leave a message <img src="http://images.linuxquestions.org/questions/images/smilies/smile.gif" border="0" alt=":)"></TEXTAREA><BR>
    <INPUT TYPE="submit" name="submit" value="Post Message">
    </FORM>
    
    </TD>
  </TR>
</TABLE>
</BODY>
</HTML>


Mega Man X 08-28-2004 07:49 AM

kev82, you are just amazing :). Thanks once again for the great help mate. The code now runs without a glitch ;). I need to get a PHP book and start learning this for real. It's by far the coolest thing I've ever played with, sadly, I don't quite follow a lot of things yet :(.

Nonetheless, thanks a lot mate! I simply could not make without your help. Thanks all others who answered this thread or that have simply looked at it. More questions to come :)

Mega Man X 09-11-2004 06:00 AM

Ok, resurrection of an old thread. I've just figured one thing out. If somebody visits my page and fill it it with HTML code, specially forms, it gets really, really screwed. How would be the wisest (and easiest) way of replacing HTML tags from a form? I think "< and >" would be enough?... hopefully...

Thanks in advance!

Cedrik 09-11-2004 06:14 AM

try strip_tags(), works more or less good
PHP Code:

$string "<a href=\"link\"><b>hello</b></a>";
$string strip_tags($string);
echo 
$string


Mega Man X 09-11-2004 06:26 AM

Thanks a lot Cedrik for the quick reply!. I so have to buy a PHP book :(

Cedrik 09-11-2004 06:45 AM

just got to www.php.net to get help with the famous function field search ;)

Mega Man X 09-11-2004 06:52 AM

Hey thanks again mate ;). Have totally forgotten to check it's official site :)


All times are GMT -5. The time now is 07:32 AM.