Creating a comments system with showing authors - php

I want to create a simple comments system with PHP. I have a registration system so only logged users can add comment.
I have a row in my table comments called idauthor. I using this sql query to add a new comment:
$sql = 'INSERT INTO kawaly SET
tekst="' . $tekst . '",
autor="' . $autor . '",
datapasty=NOW(),
tytul="' . $tytul . '",
idauthor="' .$id.'"';
my variable $id in this query cames from my logging system:
$ilu_userow = $rezultat->num_rows;
if($ilu_userow>0)
{
$_SESSION['zalogowany'] = true;
$wiersz = $rezultat->fetch_assoc();
$_SESSION['id_user'] = $wiersz['id'];
Okey then. When i adds a new comment, i can see in row idauthor a correct value.
Now i can show it by simple relation query using INNER JOIN user ON user.id = idauthor.
But when i have two people logged in, it`s does not sending correctly value for idauthor. How can i modify it on correct way? How can i insert into my database ID of user which actually wrote a comment for a proper method? Thank you for any tips! Regards.

Related

Php query for logged in users and not logged in users between two dates

i am trying to query which shows all active users (Logged in Users) between two dates. When ever a user logs in the date is recorded in last_login field. The query is below and its working fine;
select * from users where (users.last_login BETWEEN '" . $fromdate . "' AND '" . $todate . "')
Now i am trying to query which shows all Inactive Users (Not Logged in Users) between two dates. I am not sure how i will modify the existing query to achieve this condition. Can anyone pls help me on this.
If you are asking how to write the inverse logic, I think this will do the job:
"SELECT * FROM users WHERE last_login NOT BETWEEN '" . $fromdate . "' AND '" . $todate . "'"
*note if these variables are coming from user input, you should be using prepared statements and placeholders for security/stability.

Create Custom Wordpress Login Credentials Programmatically

I am have a membership directory for physicians using gravity forms for the registration page and gravity view to display the members in the directory..
The site owner informed me after-the-fact that the login credentials are to be set to specific unique criteria that are specific to each member..
For example the username is to be that individuals members license number and the password is to be the last name of the member..
Is there a way to run a script that will automatically update all of the usernames and passwords for the existing members? Keep in mind that there are over 900 active members in this directory. Or am I going to have to manually update all of the usernames and passwords in the database?
You will need to create a script in SSMS then to update all the username and password pairs. Maybe something like this:
update dbo.Users
set Password = LastName,UserName = LicenseNumber
from dbo.User
where LastName is not null and LicenseNumber is not null
Obviously, I cannot test this as I don't know the structure of your DB or the names of the tables involved. If there are more than one table and you have to join them it could look like this:
update dbo.UserData
set UD.Password = U.LastNamen UD.UserName = U.LicenseNumber
from dbo.UserData UD
LEFT JOIN dbo.User U on U.Id = UD.UserID
where U.LicenseNumber is not null and U.LastName is not null
I am using the not null for both the LicenseNumber and LastName since the user will need both to have a username and password set by this script.
If you provide more information I could give a more specific answer.
Try using the wp_create_user function:
<?php
// make an array of all the users you want to add
$allusers = array();
$allusers[0]['username'] = 'sally_sparrow';
$allusers[0]['email'] = 'sally#example.com';
$allusers[0]['password'] = '26aWpfRFcNhcZjBLLj3d';
$allusers[1]['username'] = 'sam_smith';
$allusers[1]['email'] = 'sam#example.com';
$allusers[1]['password'] = '9VF4qTXLhTJdCWzbbL9s';
// etc...
foreach ($allusers as $user) {
$result = wp_create_user( $user['username'], $user['password'], $user['email'] );
if ( is_wp_error( $result ) ) {
echo '<div class="error notice">' . $return->get_error_message() . '</div>';
}
else {
echo '<div class="updated notice">Added user id #' . $result . '</div>';
}
}
?>

How to search first level user record from his parent user id

I have created one employee's table and all employee are connected with other's and for that, I have created one parentUserId field to know his parent user.
Here is my table screenshot:
Now I have just started learning PHP & MySQL so not getting how to get the first user record with the SELECT query. For example, If I am Stephen Lopez user I want to know who is the first user of all connect users.
I am trying to find Phyllis Hinton user. Is it possible with SQL query or I need to make for loop with PHP?
as someone that starting with PHP and mysql, here is a complete code example to get you what you want. there is no magic SQL here but a PHP loop (with a count limiter) to repeat asking the DB for the parent in the connection.
<?php
/* setup your database information */
$host = 'localhost';
$user = 'db_username';
$pass = 'db_password';
$db = 'db_name';
$link= mysqli_connect($host,$user,$pass,$db); // connect to the database
if (mysqli_connect_errno()) { // if fail connection... exit.
die(mysqli_connect_error());
}
/* the function to locate the first user in the chain of connections */
function find_first_user($link,$first,$last,$max_count=10){
$cnt=0; // local counter as prtection to never go over the $max_count
$parent=0;
$tbl='myTableName';
// find info about the person in question via the first & last name
$q="select parentUserId from $tbl where firstName='" . mysqli_real_escape_string($link, $first) .
"' and lastName='" . mysqli_real_escape_string($link, $last) . "'";
if($res=mysqli_query($link, $q)){
list($parent)=mysqli_fetch_row($res);
}
// if we found a parentId, repeat looking in the chain of connections
while($parent && ++$cnt<$max_count){
$q="select parentUserId,firstName,lastName from $tbl where userId=".intval($parent);
if($res=mysqli_query($link, $q)){
list($parent,$first,$last)=mysqli_fetch_row($res);
}
}
return "$first $last";
}
// example usage
print find_first_user($link,'Stephen','Lopez') . "\n";
?>
I think you can use this query if you know maximum deep of user levels for example 5 is maximum deep :
SELECT
user.firstName,
parent.firstName AS parent_firstName,
super_parent.firstName AS super_parent_firstName,
s_super_parent.firstName AS s_super_parent_firstName,
s2_super_parent.firstName AS s2_super_parent_firstName,
FROM
`user`
LEFT OUTER JOIN user AS parent ON parent.id = user.parentuserId
LEFT OUTER JOIN user AS super_parent ON super_parent.id = parent.parentuserId
LEFT OUTER JOIN user AS s_super_parent ON s_super_parent.id = super_parent.parentuserId
LEFT OUTER JOIN user AS s2_super_parent ON s2_super_parent.id = s_super_parent.parentuserId

calling a row form MYSQL

I dont know much about MYSQL. But i need to fetch the number of review articles that a user uploaded.
To call this data, I have my database named "abc", inside I have a table named zxc_review_stats .... inside this table there are several columns, but the one I need to fetch is named "reviews"
So how can I fetch the number from the column reviews? this is what ive got so far:
SELECT COUNT(*) from `zxc_review_stats` WHERE `owner` = <<userid>>
That should work.
<?php
// ... your code
// Assuming $user_id is defined and holds the user's id.
$query = "SELECT reviews"
. " FROM zxc_review_stats"
. " WHERE user_id='" . $user_id . "'";
// do something with this query
?>
If all you need to pull is the value of the reviews field for that user:
Select `reviews` FROM `zxc_review_stats` WHERE `user_id`=user //replace user with the actual number

Sorting using php, mysql

.Hi guys, i need a little help with a project that i'm currently working on.
I have this database where i have a table containing records of students from different departments namely:
Dept1
Dept2
Dept3
Dept4
Dept5
in my batch.php i have two dropdown menus for Batch(which is the name of the tables) and Department(a field used for sorting).
what i want to do is that when the user chooses a Batch and a Department and then clicks the submit button, it will automatically assign the values chosen to a MySQL query for the SELECT method. the SELECT query should now retrieve all records from the table sort it by department but will display first the Department that was chosen above.
.Any help would be much appreciated. Thanks in advance and more power!
You can sort by a boolean expression, which will return 1 when true and 0 when false.
<?php
$batch = mysql_real_escape_string($_POST['batch']);
$department = mysql_real_escape_string($_POST['department']);
$query = "SELECT * FROM `" . $batch . "`" .
" WHERE department = '" . $department . "'" .
" ORDER BY department = '" . $department . "' DESC, department;
?>
Update: You should validate the $batch argument that it is a valid table expected, the code as is is vulnerable to SQL injection.

Categories