I tried to catch two variable ($phone,$key) and if $key exists in the random table from the first DB then check if &phone exists in users table in second DB return id else if doesn't exist make a row and then echo id. here is my code.where is my problem?
$Key = $_GET['key'];
$phone = $_GET['phone'];
$dublicate = "SELECT id,phone FROM users WHERE phone = $phone;";
$valid = "SELECT id,phone,random FROM random WHERE random = $Key;";
$verifyresult=mysqli_query($con,$valid);
$dublicateresult=mysqli_query($con2,$dublicate);
if($verifyresult == true )
{
if($dublicateresult == true){
$response=array();
while($row=mysqli_fetch_array($dublicateresult)){
array_push($response,array("id"=>$row[0],"phone"=>$row[1]));
echo $row[0];
}
}else{
$response=array();
while($row=mysqli_fetch_array($result)){
array_push($response,array("id"=>$row[0],"phone"=>$row[1],"random"=>$row[2]));
$result2 = mysqli_query($con2,"INSERT INTO users (phone)
VALUES ('$row[1]')");
$uniq = "SELECT id FROM users WHERE phone = $row[1];";
$result3=mysqli_query($con2,$uniq);
if($result3==true){
$response2=array();
while($row=mysqli_fetch_array($result3)){
array_push($response2,array("id"=>$row[0]));
echo $row[0];
}
}
}
}
}else echo "null";
Related
Am working on a project where i got two tables
1. users table | (id is primary key)
2. movie table | (movie_id is primary key, id is foreign key)
I want to get user_id from users table and want to insert movie related info when that user has logged in
am using session_variables for this but i don't know if my code is correct or not i have tried following code but didn't get any result.
This is my process.php
$errors = [];
$fav = "";
$rank = "";
$rewatched = "";
$status = "";
$recommend = "";
if (isset($_POST['submit'])) {
if (empty($_POST['fav'])) {
$errors['fav'] = 'Favourite required';
}
if (empty($_POST['rank'])) {
$errors['rank'] = 'Rank required';
}
if (empty($_POST['rewatched'])) {
$errors['rewatched'] = 'Rewatched required';
}
if (isset($_POST['status'])) {
$errors['status'] = 'Status required';
}
if (isset($_POST['recommend'])) {
$errors['recommend'] = 'Recommend required';
}
$fav = $_POST['fav'];
$rank = $_POST['rank'];
$rewatched = $_POST['rewatched'];
$status = $_POST['status'];
$recommend = $_POST['recommend'];
// Select id from users and insert into movie
if (count($errors) === 0) {
$sql = "SELECT * FROM movie WHERE id = (SELECT id FROM users WHERE id = '".$_SESSION['id']."')";
$query = "INSERT INTO movie SET fav=?, rank=?, rewatched=?, status=?, recommend=? WHERE id = '".$_SESSION['id']."'";
$stmt = $conn->prepare($query);
$stmt->bind_param('siiss', $fav, $rank, $rewatched, $status, $recommend);
$result = $stmt->execute();
if ($result) {
$anime_id = $stmt->insert_id;
$stmt->close();
$_SESSION['id'] = $movie_id;
$_SESSION['fav'] = $fav;
$_SESSION['rank'] = $rank;
$_SESSION['rewatched'] = $rewatched;
$_SESSION['status'] = $status;
$_SESSION['recommend'] = $recommend;
$_SESSION['message'] = 'Details have been submitted successfully!';
$_SESSION['type'] = 'alert-success';
} else {
$_SESSION['error_msg'] = "Database error: Could not update details";
}
}
}
In simple words i want to get id from users table and when that particular user has logged in and filled the form related to movie and submitted the results it should be stored in movie table with his/her user id.
Thank you
<?php
// Assuming $_SESSION['id'] is the User ID of the User Who Currently Logged in.
$errors = [];
$fav = "";
$rank = "";
$rewatched = "";
$status = "";
$recommend = "";
if (isset($_POST['submit'])) {
if (empty($_POST['fav'])) {
$errors['fav'] = 'Favourite required';
}
if (empty($_POST['rank'])) {
$errors['rank'] = 'Rank required';
}
if (empty($_POST['rewatched'])) {
$errors['rewatched'] = 'Rewatched required';
}
if (isset($_POST['status'])) {
$errors['status'] = 'Status required';
}
if (isset($_POST['recommend'])) {
$errors['recommend'] = 'Recommend required';
}
$fav = $_POST['fav'];
$rank = $_POST['rank'];
$rewatched = $_POST['rewatched'];
$status = $_POST['status'];
$recommend = $_POST['recommend'];
$id = $_SESSION['id'];
// Insert into Movie Table
if (count($errors) === 0) {
$query = "INSERT INTO movie SET fav=?, rank=?, rewatched=?, status=?, recommend=?, id =?";
$stmt = $conn->prepare($query);
$stmt->bind_param('siissi', $fav, $rank, $rewatched, $status, $recommend, $id);
$result = $stmt->execute();
if ($result) {
// If Insertion Successful
} else {
// If Something Went Wrong
}
}
}
In the above code, when the user submits the form it will capture the form details and store it in movie table with Foreign key as User Id. I assume that $_SESSION['id'] is the User Id of the currently Logged In User.
While creating a group with user ids, I am checking if the entered id exists in database. If it exists, group should be created, if not it should pop out an error.
I am passing array with user ids to a function user_check, but it is not getting checked. Here is my code:
<?php
function user_check($user_id)
{
$u1 = mysqli_query($con, "SELECT uid from users where uid<>'" . $_SESSION['user_id'] . "'");
while ($row = mysqli_fetch_array($u1)) {
$users = $row['uid'];
echo "users" . $users;
$users1 = sort($users);
$users2 = sort($user_id);
printf($users1);
if ($user1 == $user2) {
echo "same";
} else {
echo "not same";
}
}
}
Given a list of user_ids (that you have validated/filtered already) you could write a function something like the following (untested):
function all_users_exist(array $user_ids, Mysqli $con)
{
$result_uids = [];
$ids = implode(',', $user_ids);
$sql = "SELECT uid from users WHERE uid IN ($ids)";
$result = $con->query($sql);
if($result)
while ($row = $result->fetch_array(MYSQLI_ASSOC))
$result_uids[] = $row['uid'];
sort($user_ids);
sort($result_uids);
return $user_ids == $result_uids;
}
I'm trying to create a voting system for artists played on my radio station. I'm using the source code from: http://dl.howcode.org/download/97ff383c7d4dc9939c65c9e6fab2a5dc
The problem I have found is that the votes update using the number from the first row in the database no matter which option is selected, thus if for instance the first row has 3 votes in and the user tries to vote on someone with 0 votes, it will change the votes for the correct artist to 4 instead of 1... I hope that makes sense?
The code I have is:
[EDIT] I have changed the queries to fetch assoc to make it easier to understand.
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}else{
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>
I could be missing something obvious, in my mind I'm thinking that I somehow need to iterate through the rows before setting the new value, if so, how and where?
It looks like you are always looping over all rows and updating the relevant row with the first value found. Adding a check on the ID should do:
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}elseif ($voteOption === $artist) { // Don't run UPDATE when we're on the wrong row.
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>
I want show my message system user Profile name(If have) else show user name.
In my every code I used as below, which work well.
if (empty($pname)) $pname = $username;
But in below I cannot understand how to Return 'profile name else user name' in my "function getusername($userid)".
Here if I use return $row[0] at my "function getusername" Its show username, But I want to show Profile name and If profile name empty then show user name.
Get profile name/user name code:
function getusername($userid) {
$sql = "SELECT username,pname FROM users WHERE `id` = '".$userid."' LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
$username = $row['username'];
$pname = $row['pname'];
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $row[0];
} else {
return "Unknown";
}
}
This code fetch a specific message
function getmessage($message) {
$sql = "SELECT * FROM mail WHERE `id` = '".$message."' && (`from` = '".$this->userid."' || `to` = '".$this->userid."') LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
// reset the array
$this->messages = array();
$row = mysql_fetch_assoc($result);
$this->messages[0]['id'] = $row['id'];
$this->messages[0]['title'] = $row['title'];
$this->messages[0]['message'] = $row['message'];
$this->messages[0]['from'] = $this->getusername($row['from']);
$this->messages[0]['to'] = $this->getusername($row['to']);
} else {
return false;
}
}
Use this
function getusername($userid) {
$sql = "SELECT username,pname FROM users WHERE `id` = '".$userid."' LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
$username = $row['username'];
$pname = $row['pname'];
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $pname;
} else {
return "Unknown";
}
}
The problem is with how you are returning the value.
Change:
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $row[0];
To:
if(empty($pname)) return $username;
else return $pname;
Also, it is suggested you use mysqli instead of mysql.
Can't you use something like this?
return isset($pname) ? $pname : $username;
This basically is: if $pname is set, then return $pname, else return $username
I am not getting any errors but the database does not get updated. Please render assistance. Here is my code.
<?php
ob_start();
include("dbinc.php");
$msg = "";
if($_SESSION['usertype'] != "admin"){
header("Location: index.php");
exit;
}
$pagetitle = "Sync Job Details";
include("header.php");
if(isset($_POST['Submitaccount'])){
$allowedusers = $_POST['users'];
$accountid = trim($_POST['accountid']);
if(!$_POST['copyperms']) $_POST['copyperms']='N';
if(!$_POST['allusers']) $_POST['allusers']='N';
if(!$_POST['enabled']) $_POST['enabled']='N';
if(!$_POST['servertime']) $_POST['servertime']='N';
if(!$_POST['delremovals']) $_POST['delremovals']='N';
unset($_POST['Submitaccount']);
unset($_POST['accountid']);
unset($_POST['users']);
// $qpart = "";
$notmust = array("email" , "skip" , "comments" , "firstmod");
foreach($_POST as $key=>$val){
if(!trim($val) && !in_array($key , $notmust)) {
$err = 1;
$empty = "$key";
break;
}
$qpart .= "`$key` = '".mysql_escape_string($val)."' , " ;
}
if($qpart) $qpart = substr($qpart , 0 , -2);
if(!$err){
$chk = mysql_num_rows(mysql_query("SELECT * from accounts WHERE name = '".mysql_escape_string($_POST['name'])."' and id <> '$accountid'"));
if($chk >0){
$err = 2;
}
}
if(!$err){
if(!$accountid){
$q = "INSERT into accounts SET $qpart ";
mysql_query($q) or die("Error inserting the record :".mysql_error()."<br>".$q);
$accountid = mysql_insert_id();
}else{
$q = "UPDATE accounts SET $qpart WHERE id = '$accountid'";
mysql_query($q) or die("Error updating the record :".mysql_error()."<br>".$q);
}
if($_POST['allusers']!= "Y" && is_array($allowedusers)){
mysql_query("DELETE from accountusers WHERE accountid = '$accountid'");
foreach($allowedusers as $userid){
list($alljobs) = mysql_fetch_row(mysql_query("SELECT alljobs from users WHERE id= '$userid'"));
if($alljobs != "Y") {
mysql_query("INSERT into accountusers SET userid = '$userid' , accountid = '$accountid'");
}
}
}else{
mysql_query("DELETE from accountusers WHERE accountid = '$accountid'");
}
header("location: accountslist.php?done=1");
exit;
}
}
// if(isset($_GET['id'])){
// $record = mysql_fetch_assoc(mysql_query("SELECT * from accounts WHERE id = '$_GET[id]'"));
// foreach($record as $key=>$val) $record[$key] = stripslashes($val);
// }
if(isset($_GET['id'])){
$record = mysql_fetch_assoc(mysql_query("SELECT * from accounts WHERE id = '$_GET[id]'"));
foreach($record as $key=>$val) $record[$key] = stripslashes($val);
}
// if($err ==1)
// {
// $record = $_POST;
// foreach($record as $key=>$val)
// $record[$key] = stripslashes($val);
// $msg ="Please fill the empty field";
// }
if( isset($err) && $err == 1 )
{
$record = $_POST;
foreach($record as $key=>$val) $record[$key] = stripslashes($val);
$msg ="Please fill the empty field";
}
if( isset($err) && $err == 2)
{
$msg = "The name has already been used.";
}
?>
I feel your frustration. If i could assist you i would. People in this forum are scared to answer you as they don't want to lose reputation points as the question was marked down to -4. I suggest do some more research and post your question eslewhere.