user3693698
user3693698

Reputation: 67

Make new users automatically follow a user

I am working on a php site with uses mysql as database, now my site is a social network like site where users follow each other, now if a user joins in he is following nobody so his stream remains empty so they leave the site quickly as well,i want users to be following my account account automatically when he joins in. Can you please tell me how to do it

here are the two tables

Table structure for table sn_users

CREATE TABLE IF NOT EXISTS `sn_users` (
`id` int(11) NOT NULL,
  `username` varchar(225) NOT NULL,
  `email` varchar(225) NOT NULL,
  `password` varchar(225) NOT NULL,
  `name` varchar(225) DEFAULT NULL,
  `picture` varchar(100) NOT NULL,
  `cover` varchar(100) DEFAULT NULL,
  `job` varchar(225) DEFAULT NULL,
  `address` varchar(225) DEFAULT NULL,
  `date` int(11) NOT NULL,
  `reg_id` text,
  `active` int(11) NOT NULL DEFAULT '1'
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;

Table structure for table sn_follows

CREATE TABLE IF NOT EXISTS `sn_follows` (
`id` int(11) NOT NULL,
  `from` int(11) NOT NULL,
  `to` int(11) NOT NULL,
  `date` int(11) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=74 DEFAULT CHARSET=utf8;

Upvotes: 0

Views: 100

Answers (2)

user4640949
user4640949

Reputation:

Run the query when the user get registerd.

<?
//Register query
$con = mysqli_connect("[HOST", "[USER]", "[PASS]", "[DB]");
$query1 = "INSERT INTO sn_users
          (username, email, password, name, picture, cover, job, address, date, reg_id, active)
          VALUES
          ('$username', '$email', '$password', '$name', '$picture', '$cover', '$job', '$address', 'date', 'reg_id', 'active')";
mysqli_query($con, $query);

//Auto follow query
 $query2 = "INSERT INTO sn_follows
            (`id`, `from`, `to`, `date`)
            VALUES
            ([ID], '[NEW REGISTERD ACCOUNT], '[YOUR ACCOUNT]', '[DATE]')";
 mysqli_query($con, $query2);

Hints:

  • Make of the field id in your database an auto_increment
  • Make sure you put a hashed password in the database

Upvotes: 1

kevin
kevin

Reputation: 300

you need to do some modifications in your script. 1) get last inserted id of new registered user 2) insert that last id in sn_follows table with your id

this may elaborate flow after your user register insert query get last id like below example:

$sql = "INSERT INTO sn_users (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
$sqlfloow = "insert into sn_follows (from,to,date) values("your id",$last_id,date("Y-m-d H:i:s"))";
}

Upvotes: 0

Related Questions