PDA

View Full Version : mySQL and PHP linking problems



bradenkeithcom
01-05-2008, 05:29 PM
***I rewrote this in a reply below.


Here I am again with another problem.

I have a shoutbox that I need to connect to 2 different databases. I've got the 2 connections working. I'm grabbing a part of my table with the information "username". But the most I get out of it is Resource 2.... which would be me having the 2nd account. I need the name to show up as the username (bradenkeithcom) individually for everyone who writes on the shoutbox.

Here's the code:


<?php
// You just need to configure these 4 variables to match your server.
$db_host = "XXXXX"; // mySQL database host
$db_user = "braden_gaming"; // mySQL database user
$db_password = "XXXXX"; // mySQL database password
$db_name = "braden_gaming"; // the name of your mySQL database
// If a user has submitted a post, we want to :
// 1. Validate it
// 2. Strip unwanted html
// 3. Make sure messages and names aren't too long
// 4. Add it to our database.
if($_POST['submit']) {
// 1. Validate it, by checking all the form inputs were filled in
if(!$_POST['message']) {
echo 'Error ! : No message entered';
die;
}
// 2. Strip unwanted HTML
// Look up the strip_tags() function at
// http://www.php.net/manual/en/function.strip-tags.php for more info
$message = strip_tags($_POST['message'], '');
$author = strip_tags($_POST['author'], '');
// 3. Make sure messages and names aren't too long
// We will use the strlen() function to count the length.
$message_length = strlen($message);
$author_length = strlen($author);
if($message_length > 300) {
echo "Error ! : Your message was too long, messages must be less than 300 chars";
die;
}
if($author_length > 300) {
echo "Error ! : Your name was too long, names must be less than 300 chars";
die;
}
// 4. Add it to our database.
// If the script hasn't died yet due to an error in the inputted data
// we need to add the data to the database
// Lets connect to our database.
mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
// Select the database.
mysql_select_db($db_name) or die(mysql_error());
// Lets define the date format we want to enter to our database
// go here for more details
// http://www.php.net/manual/en/function.date.php
$date = date("h:i A dS M");
// This will produce 11:02 25th Aug
// Set the query as $query
$query = "INSERT INTO shoutbox (message, author, date, ip)
VALUES ('$message','$author','$date','$_SERVER[REMOTE_ADDR]')";
mysql_query($query);
mysql_close();
// Show thanks message and take them back to the main shoutbox
echo "Thanks for your post<BR>";
echo "<A HREF='shoutbox.php'>View the shoutbox</A>";
// If they haven't submitted a post, we want to :
// 1. Show the latest shouts
// 2. Show the shout post form
} else {
// 1. Show the latest shouts
// Lets connect to our database.
mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
// Select the database.
mysql_select_db($db_name) or die(mysql_error());
// Set the query as $query, and get the last 10 posts.
$query = "SELECT message, author, date, ip
FROM shoutbox order by id DESC LIMIT 10";
$result = mysql_query($query);
echo "<TABLE>";
while($r=mysql_fetch_array($result))
{
// To modify the appearance, edit this :
echo "<TR>
<TD><font size='1'>
Posted $r[date] by
$r[author]</font></TD>
</TR>
<TR>
<TD><font size='1'>$r[message]</font></TD>
</TR>
<TR>
<TD><HR></TD>
</TR>";

}
echo "</TABLE>";
$username = mysql_query("SELECT * FROM users WHERE username = '$username'") or die (mysql_error());
// 2. Show the shout post form
?>
<FORM METHOD=POST ACTION="shoutbox.php">
<TABLE>
<TR>
<TD></TD>
<TD><INPUT TYPE="hidden" NAME="author" value="<?php echo $username; ?>"></TD>
</TR>
<TR>
<TD>Message :</TD>
<TD><INPUT TYPE="text" NAME="message"></TD>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE="submit" name="submit" value="post"></TD>
</TR>
</TABLE>
</FORM>
<?php
}
?>


The highlighted part is what's calling the username. Thanks for any help you can send my way!

bradenkeithcom
01-05-2008, 11:16 PM
Maybe I was a little confusing in my explanation, and that's why no one is helping me(?). So here's a little better explanation.

So I have two tables with in the database braden_gaming. One is for my shoutbox and one is for my Users... where people login, etc. Now on my shoutbox page I have the messages going to shoutbox database (as well as some other information). I'm wanting to get the "Author" to be the user's login username and to be automatically inserted without them typing it in or having the option to change it. Look below to see what I did.

<INPUT TYPE="hidden" NAME="author" value="<?php echo $username; ?>">

That's not a problem. The problem is instead of writing the name is rights, Resource #X, replace X with the number that corresponds to there signup order.

So where's my problem? This is how I'm calling the information from table "users"
$username = mysql_query("SELECT * FROM users WHERE username = '$username'") or die (mysql_error());

Please help in whatever way you can.