igniteryan Posted November 22, 2007 Share Posted November 22, 2007 I just started learning PHP and I'm not sure how to word this, but here goes. I have a simple chatbox on my site using mySQL. I want to have a postcount on a user's profile, so I need to add to the current post count in a different table when a message is inserted. <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $sql="INSERT INTO cbox_msgs (userimage, username, message) VALUES ('$_POST[userimage]','$session->username','$_POST[message]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Your message has been posted. <a href='cbox.php'>[back]</a>"; mysql_close($con) ?> How would I go about doing this? I'm sure that this will be one of the easiest questions you've answered ... Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 Well, are you deleting the data from your cbox_msgs table? If not, you could just count the number of posts by each user - no need for an extra field. Otherwise, yes, you will need a new field. Presumably you have a table with the user data. Simply add a new column into this table, perhaps called cbox_posts. Then, just after inserting the message into your cbox_msgs table, increase the number in this field by 1: <?php //the rest of your code if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_query("UPDATE `yourusertable` SET `cbox_posts`=`cbox_posts`+1 WHERE `username`= '$session->username'") or die(mysql_error()); echo "Your message has been posted. <a href='cbox.php'>[back]</a>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.