I want to generate unique username for every user during registration based on his/her name, user can change it later. If user entered 'smith doe' as his name in registration form, the auto generated username should be 'smith', but if 'smith' is already someone's username(u_name) then it should add any available number at the end of 'smith' : like- 'smith1' or 'smith2' or 'smith3' and so on...
Here is what i have tried :
$usnm = $_POST['name'];
$first_nut = explode(' ', $usnm);
$usnm3 = $first_nut[0];
$usnm5 = $usnm3;
function generateUsername($usnm3,$iteration = 0)
{
$generated = $iteration > 0 ? ($usnm3 . $iteration) : $usnm3; //Increment username
$query_usnm = mysqli_query($con, "SELECT * FROM user WHERE (u_name = '$generated' or u_name = '$usnm3') ");
if(mysqli_num_rows($query_usnm) > 0)
{
return generateUsername($usnm3,$iteration + 1);
}
return $generated;
}
$usnm4 = generateUsername($usnm5);
$sql = mysqli_query($con, "insert into user (name, u_name) values ('$name', '$usnm4');");
Related
I am trying to update the record of Employee.MY query shows the message "Employee record updated Successfully" but it is not updating in table My code goes like this
{
$eid=intval($_GET['uin']);
$uin=$_POST['uin'];
$fname=$_POST['firstName'];
$lname=$_POST['lastName'];
$email=$_POST['email'];
$department=$_POST['department'];
$recoffr=$_POST['recoffr'];
$mobileno=$_POST['mobileno'];
$sql="
update tblemployees
set FirstName = :fname
, LastName = :lname
, email = :email
, department = :department
, recoffr = :recoffr
, Phonenumber = :mobileno
where uin = :eid
";
$query = $dbh->prepare($sql);
$query->bindParam(':uin',$uin,PDO::PARAM_STR);
$query->bindParam(':fname',$fname,PDO::PARAM_STR);
$query->bindParam(':lname',$lname,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':department',$department,PDO::PARAM_STR);
$query->bindParam(':recoffr',$recoffr,PDO::PARAM_STR);
$query->bindParam(':mobileno',$mobileno,PDO::PARAM_STR);
$query->execute();
$msg="Employee record updated Successfully";
}
My table structure is Table structure
You have assigned Uin to wrong Id
$query->bindParam(':eid',$uin,PDO::PARAM_STR);
not
$query->bindParam(':uin',$uin,PDO::PARAM_STR);
you can try this:
if (isset($_GET['uin'])) {
$ID = $_GET['uin'];
} else {
$ID = "";
}
$tblemployees_data = array();
$sql_query = "SELECT firstName, lastName, email, department, recoffr, mobileno
FROM tblemployees
WHERE uin = ?";
if ($query_category->prepare($sql_query)) {
// Bind your variables to replace the ?s
$query_category->bind_param('s', $ID);
// Execute query
$query_category->execute();
// store result
$query_category->store_result();
$query_category->bind_result($previous_category_image);
$query_category->fetch();
$query_category->close();
}
I need help with update my column in database. I use InnoDB, probably problem is here
$sql_update_heslo = "UPDATE users SET u_password = $_noveHeslo WHERE u_name = '$_SESSION[username]'";
first I am checking if Button was clicke. If yes, then I am checking if there is only 1 user with this name who is logged in, then I am checking if MD5 password from the database is same as user input, if yes then update password based on the user entry.
if (isset($_POST['pass_aktualizovat'])) {
$_old_password = md5($_POST['o_pass']);
$sql_search_for_all_userss = "SELECT * FROM users WHERE u_name = '$_SESSION[username]' ";
$result = mysqli_query($connect_to_db, $sql_search_for_all_userss);
// ak sa najde jedna zhoda v databazy
if ($db_data = mysqli_num_rows($result) == 1) {
while (mysqli_fetch_assoc($result)) {
$_aktualneHeslo = $db_data['u_password'];
}
if (md5($_POST['o_pass'])==$_aktualneHeslo) {
$_noveHeslo = md5($_POST['n_pass']);
$sql_update_heslo = "UPDATE users SET u_password = '$_noveHeslo' WHERE u_name = '".$_SESSION['username']."'";
mysqli_query($connect_to_db, $sql_update_heslo);
echo "treti";
}
echo "druhy";
}
echo "prvy";
}
?>
I have a list of users ids and my goal is to get the name of each user using the id
the sql variable prints: SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 '
If i copy paste the query the name is returned and works as well with other ids, but i don't want the function to allways return the same name, i want to add the variable $userID to my query and return the name
But this only works when i hardcode de id
public function returnNameByID($userID){
//User ID: 56d4814fb37cf3.17691034
$sql = "SELECT name FROM users WHERE unique_id = '$userID'";
echo $sql; // Prints SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 '
//Doesn't work $name returns Null
// $stmt = $this->conn->prepare("SELECT name FROM users WHERE unique_id = '$userID'");
//Doesn't work $name returns Null
$stmt = $this->conn->prepare($sql);
// Works $name returns name of the user
$stmt = $this->conn->prepare(" SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 ' ");
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$name = $result["name"];
return $name;
EDIT
require_once 'db_functons.php';
$db = new db_functions();
$userID = $_GET['userID'];
$mArray = array();
$mArray = $db->getFriendsList($userID);
//Printing the array Prints the correct id's
$name = $db->returnNameByID($mArray[0]);
echo $name;
Seems you have some space around your code try using a trim
$sql = "SELECT name FROM users WHERE trim(unique_id) = '" . trim($userID) . "';";
I have 2 pages, "signup.php" and "globalfunctions.php". On signup.php, I get all of the info from the form submission, I hash the password (by appending a random string generated in globalfunctions.php), and I use the function executeSQL that I defined.
signup.php:
include('/home/www/portaldev.samgoodman.co/processes/globalfunctions.php');
$singleAppendString = generateRandomAppend(16);
$form_email = $_POST['email'];
$form_password = $_POST['password'];
$form_name = $_POST['name'];
$form_school = $_POST['schoolid'];
$form_grad = $_POST['gradyear'];
$form_ip = $_SERVER['REMOTE_ADDR'];
$password_with_hash = $form_password.$singleAppendString;
$hashedPassword = sha1($password_with_hash);
executeSQL("$nextUserQuery", "SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1");
Here is where I would like to get the value of $nextUserQuery from the database, but I need to return the value in the function.
executeSQL("$insertUser", "INSERT INTO users (id, name, email, password, school_id, grad_year, lvl, signup_ip) VALUES ('".$calc_userid."', '".$form_name."', '".$form_email."', '".$hashedPassword."', '".$form_school."', '".$form_grad."', '0','".$form_ip."')");
executeSQL("$insertHash", "INSERT INTO vault (id, hash) VALUES ('".$calc_userid."', '".$singleAppendString."')");
globalfunctions.php
function generateRandomAppend($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function executeSQL($varName, $query) {
global $varName;
$con=mysqli_connect("localhost", "hugopak1_spm", "Massavailable1", "hugopak1_spm");
$varName = mysqli_query($con, $query);
return $varName;
}
What you currently do is not the correct way to get the value returned by executeSQL function. Remove $varName parameter from executeSQL function
function executeSQL($query) {
$con = mysqli_connect("localhost", "hugopak1_spm", "Massavailable1", "hugopak1_spm");
$varName = mysqli_query($con, $query);
return $varName;
}
and declare a new variable that will hold the returned value
$nextUserQuery = executeSQL("SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1");
In the above example, the value of $nextUserQuery is the value returned by executeSQL("SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1"). You should apply the same thing to the other two lines as below
$insertUser = executeSQL("INSERT INTO users (id, name, email, password, school_id, grad_year, lvl, signup_ip) VALUES ('".$calc_userid."', '".$form_name."', '".$form_email."', '".$hashedPassword."', '".$form_school."', '".$form_grad."', '0','".$form_ip."')");
$insertHash = executeSQL("INSERT INTO vault (id, hash) VALUES ('".$calc_userid."', '".$singleAppendString."')");
Been tinkering with my website, it is a seat booking website. Still in alpha testing really so not live to the public yet for obvious reasons.
However, I'm having a few problems with updating the values in my database.
I'll post the code and then explain the problem..
else {
$seatID = $_POST['form_submitted'];
$query1 = "SELECT seatTaken FROM SEATS WHERE seatNo = '$seatID'";
$result = mysql_query($query1);
while($row = mysql_fetch_array($result))
{
$taken = $row['seatTaken'];
}
$query2 = "SELECT passNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query2);
while($row = mysql_fetch_array($result))
{
$passno = $row['passNo'];
}
$query3 = "SELECT groupID FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$groupno = $row['groupID'];
}
$query4 = "SELECT flightNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$flightno = $row['flightNo'];
}
// if ($taken = 0) {
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
// AND flightNo = '$flightno'"
echo '<meta http-equiv="refresh" content="5;url=http://www.mywebsite.com/">';
echo mysql_error();
//}
}
?>
Now the user will have selected their seat in the previous form hence the:
$seatID = $_POST['form_submitted'];
However, at the bottom in my queries, the only value that actually changes in the database when this PHP code is run is the boolean value of 'seatTaken', in that it does change from 0 (not occupied) to 1 (occupied).
The field passNo and groupID in my database DO NOT UPDATE as referenced here in these queries:-
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
Is anyone able to help? Many thanks!
Tom
Watch your variable naming and string quotation
When your looking for values in mysql, they usually need to be a string literal (add quotes).
And your other problem is your variable names:
$update = mysql_query("UPDATE PASSENGER SET seatNo = '$seatID' WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passno', groupID = '$groupno' WHERE seatNo = '$seatID'");
$passno vs $passNo
$groupid vs $groupno
You should also make sure you properly escape any input coming from the user http://php.net/manual/en/function.mysql-real-escape-string.php
One can't see in your code how do you generate the values of $groupid, $passNo, $seatID. Are those varaibles set when you do your update? (just echo the SQL code to see what query is being sent to your database)
Maybe you should try getting the variables from your post request, like $_POST['groupid'], if groupid is the name of the field in the form.