Jump to content

Newbie PHP/MySQL Question


igniteryan

Recommended Posts

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 ...

Link to comment
Share on other sites

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>";
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.