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
https://forums.phpfreaks.com/topic/78474-newbie-phpmysql-question/
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>";
?>

 

Archived

This topic is now archived and is closed to further replies.

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