Jump to content

Comments in Database


yddib

Recommended Posts

We are trying to send comments to the database where it recognises user id and the id of the person the comment is being sent to. This is just for a prototype so we know it's not perfect but we're trying! In the form we have a "To:" field as we thought this would help! This is the code! Please help!!

 

<?php 
session_start();
$id = $_SESSION['id'];
?>

<?php
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("database");


$fname=$_POST['fname'];
$lname=$_POST['lname'];
$comment=$_POST['comment'];

$result1 = mysql_query("SELECT u_id FROM gradinfo WHERE f_name = '$fname' AND l_name = '$lname'");
$result2 = mysql_query("SELECT f_name, l_name FROM gradinfo WHERE u_id = '$id'");
$f_name = mysql_insert_f_name();
$l_name = mysql_insert_l_name();
$uid = mysql_insert_uid();
$id = mysql_insert_id();
$result = mysql_query("INSERT INTO comments (from_u_id,f_name,l_name,u_id,comment,fname,lname) values ('$id','$f_name','$l_name','$uid','{$comment}','$fname','$lname')");


echo "Comment added successfully.";



?>

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/
Share on other sites

Well i can see a few things which are a bit weird...

 

You're attempting to set the ID for the comment to mysql_insert_id(); This will ONLY work after you have inserted a post, and will get the ID of the last post inserted. This is assuming of course you're using an auto_increment primary key field.

 

And what is mysql_insert_f_name() and mysql_insert_l_name() and mysql_insert_uid? Are these custom functions you have written?

 

It seems apparent to me if you're writing a message system that you have the following:

1) A users table, which consists of columns "id", "first_name", "last_name"

2) A comment table, which consists of columns "from_id", "to_id", and "message"

 

The ID column in the users table is a primary key auto_increment, meaning every time you add a new user it will increment by 1 (default). When a user sends a message to another user you need to supply the from_id, to_id and message into the comments table. Pulling this information back out is trivial.

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/#findComment-639643
Share on other sites

What is the actual problem you're encountering right now?

 

You shouldn't assume the precense of any element in the $_POST[] array. You should check each exists with a isset() call, and be able to deal appropriately with cases where it isn't set.

 

So it's actually impossible to work out what is going on here without a problem and the corresponding database schema (list of tables, list of columns for each). Also what are these functions:

 

$f_name = mysql_insert_f_name();

$l_name = mysql_insert_l_name();

$uid = mysql_insert_uid();

$id = mysql_insert_id();

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/#findComment-639646
Share on other sites

I know it's a bit of a mess!! It isn't inserting anything into the database. I will try and break down exactly what we're trying to do. It's all a bit new to us!! ???

 

  • People log into the system and use sessions with their user ID
  • When posting comments we want to use their user id (to and from) so that the correct comment is displayed on their page
  • We also want their names to show up so they know who it is from

 

The fields in the data base are:

comm_id (comment id), date, comment, from_u_id, f_name (firstname), l_name, u_id, fname (to), lname.

 

 

I'm confusing myself now! We know there is no need for f_name & l_name, and fname and lname. We're just trying to get it to work!!

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/#findComment-639652
Share on other sites

$comment=$_POST['comment'];

mysql_query("INSERT INTO comments (from_u_id, comment) VALUES ('$id', '$comment')");

echo "Comment added successfully.";

 

This code works to drag the comment and the senders id to the database but I don't know how to get the id of the person i'm sending it to!

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/#findComment-639660
Share on other sites

That needs to be another parameter you set.

 

The user it is coming from should be the user that is logged into your system, i.e. the information pertaining to user id kept in your session.

 

If you think about it logically the user you are sending the message to is selected from a link or a dropdown list, or some other format, either way, you will need to post this parameter through a form.

Link to comment
https://forums.phpfreaks.com/topic/123908-comments-in-database/#findComment-639713
Share on other sites

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.