/    Sign up×
Community /Pin to ProfileBookmark

simple php/mysql driving me insane…can anybody help?

Hi, I am trying to design my own forum/guestbook type thing (very simple for now) but I am having some php/mysql difficulties…

So, I have my MySQL database set up (with the files uploaded to the server), and a simple submit reply (which seems to work) and display (which doesn’t work) script going on, but the problem is displaying the user name and message (right now, all that is supposed to show up is the posters name and message, they can enter whatever they want to both and there is no registering yet). Whenever I submit a reply via the posting page, it doesn’t show up on the index page…

I already tried the script from various php/mysql guide web sites (an even the script on the php web site) to no success. (I hope this may not be due to the fact of a not so recent php version on my server! ๐Ÿ˜ฎ ) So, anyway, here is my script for both pages so far (don’t worry about the database connecting part of the script (The part with the dbhost , dbun, and dbpass)):

The Login Page:

[code=php]<?php
$dbhost=”hostname”;
$dbun=”username”;
$dbpass=”hostpass”;
$db=mysql_connect($dbhost, $dbun, $dbpass) or die(mysql_error());
$database=”databasename”;
mysql_select_db($database) or die(mysql_error());
?>
<html><head><title>AoABB >> Post Reply</title></head><body>
<form method=”POST” action=”index.php”>
<center><a href=”index.php”>Index Page</a> | Post Reply<br>
<table width=”50%” border=”2″ cellpadding=”5″ cellspacing=”5″><tr><td><p><b>Post a Reply</b><br><br>
Name:<br><input type=”text” name=”username” maxlength=”20″><br>
Message:<br><textarea rows=”8″ cols=”40″ name=”message” wrap=”hard”></textarea><br></p>
<input type=”submit” value=”Submit!”><input type=”reset” value=”Clear!”>
<?php
$insertinfo = “INSERT INTO userinfo (username, message) VALUES (‘$username, $message’)”;
$query = mysql_query($insertinfo);
?>
</form></td></tr></table></body></html>
<?php
mysql_close($db);
?>[/code]

And the Index Page:

[code=php]<?php
$dbhost=”hostname”;
$dbun=”username”;
$dbpass=”userpass”;
mysql_connect($dbhost, $dbun, $dbpass) or die(mysql_error());
$database=”aoatech_aoabb”;
mysql_select_db($database) or die(mysql_error());
echo(“<html><head><title>Forum Index</title></head><body>”);
echo(“<center>Index Page | <a href=post.php>Post Reply</a><br>”);
echo(“<table border=2 cellpadding=5 cellspacing=5 width=75%>”);

$query = ‘SELECT * FROM userinfo’;
$result = mysql_query($query) or die(‘Query failed: ‘ . mysql_error());
while( $row = mysql_fetch_array( $result ) )
{
echo(“<tr><td width=25%>”);
print(“$row->username”);
echo(“</td><td width=75%>”);
print(“$row->message”);
echo(“</td></tr>”);
}
echo(“</table></body></html>”);
mysql_close();
?>[/code]

Ok, so a few questions:

  • 1. Do I need the database connect things on the Login Page?

  • 2. I think that the code for displaying the username and message is wrong…if so, how can I fix it?

  • 3. Are there any other mistakes in my code? All this changing around by using guides and samples has most likly messed up my code somewhere because I haven’t been very organized… :rolleyes:
  • Also, I am trying to keep it simple, so don’t add any new features to the code unless it is needed for the entire script to work.

    All help is needed and thanks in advance!
    ~Testinging

    to post a comment
    PHP

    22 Comments(s) โ†ด

    Copy linkTweet thisAlerts:
    @rincewind456Dec 11.2005 โ€”ย 
  • 1. Yes if you are going to connect to the DB.

  • 2. Try this:[code=php]
    <?php
    $dbhost="hostname";
    $dbun="username";
    $dbpass="userpass";
    mysql_connect($dbhost, $dbun, $dbpass) or die(mysql_error());
    $database="aoatech_aoabb";
    mysql_select_db($database) or die(mysql_error());
    echo("<html>n<head>n<title>Forum Index</title>n</head>n<body>n");
    echo("<center>Index Page | <a href=post.php>Post Reply</a>n<br>n");
    echo("<table border=2 cellpadding=5 cellspacing=5 width=75%>n");

    $query = 'SELECT * FROM userinfo';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    while( $row = mysql_fetch_array( $result ) )
    {
    echo("<tr>n<td width=25%>");
    print("$row[username]");
    echo("</td>n<td width=75%>");
    print("$row[message]");
    echo("</td>n</tr>");
    }
    echo("</table>n</body>n</html>");
    mysql_close();
    ?>[/code]

    3. Not that I can see with a quick look.



    Just a tip add line breaks "n" to you html code it makes it a lot easier to debug your html pages rather than trying to see whats gone wrong with the entire page in one line ?
  • Copy linkTweet thisAlerts:
    @chazzyDec 11.2005 โ€”ย hi, you have it a little backwards (I think)

    If you post to the index page, you need to have your processing going on there. you seem to have it scripted in a javascript sense (this is the order of things to run) but php is a server side, you send data back to the server. so your index.php should look like this:

    [code=php]
    <?php
    $dbhost="hostname";
    $dbun="username";
    $dbpass="userpass";
    mysql_connect($dbhost, $dbun, $dbpass) or die(mysql_error());
    $database="aoatech_aoabb";
    mysql_select_db($database) or die(mysql_error());
    if(isset($_POST['username']){
    $insertinfo = "INSERT INTO userinfo (username, message) VALUES ('$_POST['username']','$_POST['message']')";
    $query = mysql_query($insertinfo);
    }
    echo("<html><head><title>Forum Index</title></head><body>");
    echo("<center>Index Page | <a href=post.php>Post Reply</a><br>");
    echo("<table border=2 cellpadding=5 cellspacing=5 width=75%>");

    $query = 'SELECT * FROM userinfo';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    while( $row = mysql_fetch_array( $result ) )
    {
    echo("<tr><td width=25%>");
    print("$row->username");
    echo("</td><td width=75%>");
    print("$row->message");
    echo("</td></tr>");
    }
    echo("</table></body></html>");
    mysql_close();
    ?>
    [/code]


    and the first page should just a simple form, you don't need any php on it at all actually.
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 12.2005 โ€”ย I seem to get an error with that code chazzy...

    I get this error
    [code=php]Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ***** on line 19[/code]
    Line 19 is
    [code=php]$insertinfo = "INSERT INTO userinfo (username, message) VALUES ('$_POST['username']','$_POST['message']')";[/code]

    Does anybody know how to solve this?
    Copy linkTweet thisAlerts:
    @chazzyDec 12.2005 โ€”ย try adding this after if condition:
    [code=php]
    $username = $_POST['username'];
    $message = $_POST'message'];
    [/code]


    and change the insert to this:
    [code=php]
    $insertinfo = "INSERT INTO userinfo (username, message) VALUES ('$username','$message')";
    [/code]

    should work.
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 12.2005 โ€”ย Well, that fixes the errors, but it still doesn't display the text, or even the td's...all that displays is the table border, which grows a little when you submit a new reply (which also doesn't show up)...
    Copy linkTweet thisAlerts:
    @chazzyDec 12.2005 โ€”ย well what happens with all the data in the table? is it actually there, can you verify it?
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 12.2005 โ€”ย Yes I believe the data is in the database because MySQL allows me to empty the fields where the information is stored (is that the proper way to check? :rolleyes: )...
    Copy linkTweet thisAlerts:
    @chazzyDec 12.2005 โ€”ย what do you mean by "empty the fields where the information is stored"?

    do you have access to anything like phpMyAdmin or another client utility where you can just run the queries against the database?
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 12.2005 โ€”ย Oh, yes sorry...I meant phyMyAdmin, not MySQL... ? Silly me...wasn't thinking...
    Copy linkTweet thisAlerts:
    @rincewind456Dec 12.2005 โ€”ย Assuming that all the other code is correct and that there is something in the DB to display ? , the reason your page is not displaying is this block of code[code=php]
    echo("<tr><td width=25%>");
    print("$row->username");
    echo("</td><td width=75%>");
    print("$row->message");
    echo("</td></tr>");[/code]


    it should be[code=php]
    echo "<tr><td width=25%>";
    print $row['username'];
    echo "</td><td width=75%>";
    print $row['message'];
    echo "</td></tr>";
    [/code]

    BTW you don't need all the brackets around your print or echo statements.
    Copy linkTweet thisAlerts:
    @chazzyDec 12.2005 โ€”ย Assuming that all the other code is correct and that there is something in the DB to display ? , the reason your page is not displaying is this block of code[code=php]
    echo("<tr><td width=25%>");
    print("$row->username");
    echo("</td><td width=75%>");
    print("$row->message");
    echo("</td></tr>");[/code]


    it should be[code=php]
    echo "<tr><td width=25%>";
    print $row['username'];
    echo "</td><td width=75%>";
    print $row['message'];
    echo "</td></tr>";
    [/code]

    BTW you don't need all the brackets around your print or echo statements.[/QUOTE]


    both of those statements should be made with a grain of salt.

    brackets are very useful. php has poor formatting because it doesn't require you to use them, i would argue.

    and the combination of print and echo's displays the same results in both pieces of code posted above.

    Testinging, what happens when you view the source of the page? do you see the values there? and are you sure they're in the database? I'm not sure you know what phpMyAdmin is and what MySQL actually is. phpMyAdmin is a tool for connecting to a database.

    What happens when you just run this query in phpMyAdmin?
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 13.2005 โ€”ย Testinging, what happens when you view the source of the page? do you see the values there? and are you sure they're in the database? I'm not sure you know what phpMyAdmin is and what MySQL actually is. phpMyAdmin is a tool for connecting to a database.

    What happens when you just run this query in phpMyAdmin?[/QUOTE]


    Don't underestimate me ?

    Believe it or not, but I do in fact know what phpMyAdmin and MySQL are, it's just that I'm not as skilled as I'd like to be with them (I do know the basics (the very basic basics)).

    EDIT: So, one last problem, it works fine in adding the new replies, except that when you reload the page the reply you added appears again. It appears an extra time for each time you reload the page...can you help me solve this?
    Copy linkTweet thisAlerts:
    @chazzyDec 13.2005 โ€”ย no, that's the way it works. the post data remains in your browser cache and there's no fool proof way to stop it.

    you could, theoretically, have a 3 page system where pagea.html posts to pageb.php that processes pagea's post data and after it does its processing uses header to redirect to another pagec.php that shows the full data.
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 13.2005 โ€”ย EDIT: Spoke too soon! I found a problem...

    If you add a new post, then reload the page, the post appears again under the original post you made...how can i fix that?
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 13.2005 โ€”ย Anybody? This is a major problem...
    Copy linkTweet thisAlerts:
    @chazzyDec 14.2005 โ€”ย other than passing around some id's to avoid duplicate entries, i don't see any way to work around this. you're describing the default behavior of a web application.
    Copy linkTweet thisAlerts:
    @rincewind456Dec 14.2005 โ€”ย But surely your not intending to use this script on a web server!

    I would have thought (as you stated in your first post) that this was just a part of the learning process, because if you put this up you are inviting an SQL injection attack, see http://www.unixwiz.net/techtips/sql-injection.html for an informative explanation.

    You need to consider what you want from your forum, but I would have thought that at the least you have to have in your:

    database the following:

    id

    date/timestamp (preferably timestamp to aid sorting of posts by time entered)

    username

    comment

    This is the bare minimum, and should in my opinion be added to.

    On your php posting page you HAVE to have :

    Validation of ALL input to the database (see SQL injection attacks)

    These are necessities and I am sure others would add more (I know I would).

    So my point is that by extending the functionality of the page you will not get some of the problems you are having now, such as the duplicate entry's on your display page.
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 16.2005 โ€”ย Sorry for such a late response to your responses. I've been quite busy with other more important things at the moment.

    So, right now my forum isn't available to the public, right now it's just there for private testing. Once the forum is secure enough, it will be made available to the public.

    Now about the duplicate posts...How can I make it so posts are validated? Those suggestions that you recommended are nice (Id, date/timestamp, username, and comment), but since I have very limited knowledge of php and MySQL, can you help me out?
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 17.2005 โ€”ย Can somebody please help me with this? This problem makes my "forum" unusable...
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 19.2005 โ€”ย Anybody? Somebody? Nobody?

    Is the validation and removal of this bug possible with php code?

    (If anybody has an objection to me triple bump posting, feel free to flame me or do whatever you normally do ? )
    Copy linkTweet thisAlerts:
    @rincewind456Dec 19.2005 โ€”ย If you checked your private messages you would see that I sent one offering help on friday!!
    Copy linkTweet thisAlerts:
    @TestingingauthorDec 19.2005 โ€”ย If you checked your private messages you would see that I sent one offering help on friday!![/QUOTE]

    Heh... :rolleyes:

    I don't really check private messages on any forum that I visit, because I don't expect to get any. And if there is some popup message or something, I still wouldn't know if I got a new private message, 'cause I've blocked javascript on bascially every web site in exsistance...

    ...anyway...I sent you a PM.
    ร—

    Success!

    Help @Testinging spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 โ€” BETA 5.18,
    whats_new: community page,
    up_next: more Davinciโ€ข003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinciโ€ข003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...