Jump to content
xisto Community
KansukeKojima

Shout Box Help

Recommended Posts

Alright... so this is really my first time using databases at all. Anyways, I need some help.

http://forums.xisto.com/no_longer_exists/

I used a tutorial from spoono.com (http://forums.xisto.com/no_longer_exists/) to create this... everything seems to work, except that it won't show what is posted in the shoutbox.....

my code is below.... and since I hardly know anything about databases, I'm sure you'll find an error quickly.

Thanks!

<?php//host name passwordmysql_connect("localhost","name","password");//select databasemysql_select_db("shoutboxdatabase");if($submit){//date function$time=date ("h:ia d/j/y");//insert into shoutbox table$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)"."VALUES ('NULL','$name', '$message','$time')");}//last five messages$result = mysql_query("select * from shoutbox order by id desc limit 5");//while loopwhile($r=mysql_fetch_array($result)){//variables from table$time=$r["time"];$id=$r["id"];$message=$r["message"];$name=$r["name"];?><?php  $time ?><br /><?php echo $name ?><br /><?php echo $message ?><br /><?php } ?><form action="<?php echo $php_self ?>" method="post"><INPUT TYPE='TEXT' value='Name' NAME='name' SIZE=30 maxlength='100'><br /><INPUT TYPE='TEXT' value='Message' NAME='message' size=30 maxlength='100'><input type="submit" name="submit" value="Submit"></form>

Share this post


Link to post
Share on other sites

The first thing i notice is this line:

mysql_select_db("shoutboxdatabase");

Is this how it is in the code or have you changed the DB name to hide it?

The reason i ask is that with T17 you need to prefix the DB name with your hosting username EG: If my cpanel login name was "shadowx" (which it isnt by the way...) My DB name would be "shadowx_shoutboxdatabase"

I dont have time to look through the code so if this isnt the problem someone else will have to help!

Share this post


Link to post
Share on other sites

yeah... just never mind all the db connect stuff... I changed it to hide the info....

Share this post


Link to post
Share on other sites

Either check through cpanel if entries are being inserted into the database using myphpadmin or what ever its called. Then if entries are being inserted there check your actually receiving results from the query by using

$found = mysql_num_rows($result);echo $found;

if your getting the correct return entries then we can assume its correct and the error is on the loop where you try and output the results. How you have done it I never do

while($r=mysql_fetch_array($result)){//variables from table$time=$r["time"];$id=$r["id"];$message=$r["message"];$name=$r["name"];?><?php  $time ?><br /><?php echo $name ?><br /><?php echo $message ?><br /><?php } ?>

while($r=mysql_fetch_array($result)){//variables from table$time=$r["time"];$id=$r["id"];$message=$r["message"];$name=$r["name"]; echo $time."<br />"; echo $name."<br />"; echo $message."<br />";} ?>

I would just keep the while loop in side the same php tags. I dont know what the effect of it doing your way does but obviously it did not pick up the error when you tried to just do "$time" with no echo :lol:. just try it this way and see if it works.

Share this post


Link to post
Share on other sites

Can you get results from the Database using this Query in the phpMyAdmin using the SQL function?

select * from shoutbox order by id desc limit 5"

Also, add this after the query in your script to see what the script is getting for results:
print_r( $result );

Of course, set error reporting at the top of the script to see if the script is tossing an error:
ini_set("display_errors", 1);error_reporting(E_ALL);

Share this post


Link to post
Share on other sites

I think that this may be it.

Notice: Undefined variable: submit in /home/kansuke/public_html/shoutbox.php on line 11Resource id #3

... what is the undefined variable in the code?

and it rejects the query you said to use...
Edited by KansukeKojima (see edit history)

Share this post


Link to post
Share on other sites

you didnt say if that was your entire code you posted. Looking at it now $submit is not defined as when its submitted your checking if($submitrefer to it as $_POST['submit'];

Share this post


Link to post
Share on other sites

A couple of suggestions:

Replace:

while($r=mysql_fetch_array($result)){//variables from table$time=$r["time"];$id=$r["id"];$message=$r["message"];$name=$r["name"];?><?php  $time ?><br /><?php echo $name ?><br /><?php echo $message ?><br /><?php } ?>
With:
while($r=mysql_fetch_assoc($result)){//variables from tableecho $r["time"]."<br/>\n".$r["id"]."<br/>\n".$r["name"].":<br/>\n".$r["message"]."<br/>\n";}?>
to save some space, and to receive an associative array from the result.

And since the form is going to be submitted to the same page it's in, you can have simply this:
<form action="" method="post">


Share this post


Link to post
Share on other sites

Also, your output code reads this:

 

<br /><b>Notice</b>:  Undefined variable: submit in <b>/home/kansuke/public_html/shoutbox.php</b> on line <b>11</b><br />Resource id #3<form action="<br /><strong class='bbc'><b>Notice</b>:  Undefined variable: php_self in <b>/home/kansuke/public_html/shoutbox.php</b> on line <b>37</b></strong><br />" method="post"><INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100' /><br /><INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100' /><br /><input type="submit" name="submit" value="submit"></form>

The error is on the following line in the source code:

 

<form action="<?php echo $php_self ?>" method="post">

Which should read:

 

<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">

Share this post


Link to post
Share on other sites

you didnt say if that was your entire code you posted. Looking at it now $submit is not defined as when its submitted your checking if($submit
refer to it as $_POST['submit'];


I think that would maybe solve it... but, where am I supposed to place it? I'm not quite clear on where you want me to place $_POST['submit'].

Share this post


Link to post
Share on other sites

This whole block in your original code would never return true since its $submit is not defined in PHP yet.

if($submit){...}

So you can either change it so you define $submit before its used or just change $submit to $_POST['submit']; directly. Submit is coming from your button named 'submit'.

// this $submit = $_POST['submit'];if($submit){..}// or thisif($_POST['submit']){..}

Share this post


Link to post
Share on other sites

w00t! It works.

sonesay, what you said worked perfectly! But, an error said that the name and message variables were not defined... I almost posted another message saying that it would not work, but I added $name = $_POST['name']; and $message = $_POST['message']; and now it works perfectly! Thank you everyone that helped!

Now I just have to work on some styling and stuff...

thanks!

**EDIT**
ahh... crap... ok one problem left... whenever someone visits the page... it re-posts the previous message...... hmm... how would I fix that....

here is my code again...

<?ini_set("display_errors", 1);error_reporting(E_ALL);//the host, name, and password for your mysqlmysql_connect("localhost","not tellin you!","not tellin");//select the databasemysql_select_db("not tellin");$submit = $_POST['submit'];$name = $_POST['name'];$message = $_POST['message'];if($submit){   //use the PHP date function for the time   $time=date("h:ia d/j/y");      // inserting it into the shoutbox table which we made in the mysql statements before   $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".	  "VALUES ('NULL','$name', '$message','$time')");}?><?//returning the last 5 messages$result = mysql_query("select * from shoutbox order by id desc limit 5");//the while loopwhile($r=mysql_fetch_assoc($result)){//variables from tableecho $r["time"]."<br/>\n".$r["id"]."<br/>\n".$r["name"].":<br/>\n".$r["message"]."<br/>\n";}?><form action="" method="post"><INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100' /><br /><INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100' /><br /><input type="submit" name="submit" value="submit"></form>

Edited by KansukeKojima (see edit history)

Share this post


Link to post
Share on other sites
ini_set("display_errors", 1);error_reporting(E_ALL);//the host, name, and password for your mysqlmysql_connect("localhost","not tellin you!","not tellin");//select the databasemysql_select_db("not tellin");$submit = $_POST['submit'];$name = $_POST['name'];$message = $_POST['message'];if($submit){  

I think whats causing the error reporting for the first time you come to the page is the upper block of code where it says error reporting etc. Its catching and displaying the error when post vars submit, name, and message are assigned but are null I'm guessing. I would try and remove the error reporting statements in the first two lines and test it out. I think that way you can still try and assign the post variables and if they are null which would be in the case where someone first loads the page it shouldn't report the error. Not sure if this is good practice but if someone has a better way please post.

Share this post


Link to post
Share on other sites

well... this is retarded... the error messages are gone now... but it keeps reposting the last message you typed whenever you refresh teh page.... what the hell...

Share this post


Link to post
Share on other sites

Um Kansuke try this.

<?//the host, name, and password for your mysqlmysql_connect("localhost","not tellin you!","not tellin");//select the databasemysql_select_db("not tellin");$submit = $_POST['submit'];$name = $_POST['name'];$message = $_POST['message'];if($submit){  //use the PHP date function for the time   $time=date("h:ia d/j/y");      // inserting it into the shoutbox table which we made in the mysql statements before   $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".	  "VALUES ('NULL','$name', '$message','$time')");   $_POST['submit'] = 0;   $_POST['message'] = "";   $_POST['name'] = "";}?><?//returning the last 5 messages$result = mysql_query("select * from shoutbox order by id desc limit 5");//the while loopwhile($r=mysql_fetch_assoc($result)){//variables from tableecho $r["time"]."<br/>\n".$r["id"]."<br/>\n".$r["name"].":<br/>\n".$r["message"]."<br/>\n";}?><form action="" method="post"><INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100' /><br /><INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100' /><br /><input type="submit" name="submit" value="submit"></form>

EDIT: Fixed BBCode
Edited by coolcat50 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.