How to find TeacherId from teacher's full name - php

Below I have a $_POST variable for the teacher's firstname and surname:
if (isset($_POST['teacherforename'])) {
$_SESSION['teacherforename'] = $_POST['teacherforename'];
}
if (isset($_POST['teachersurname'])) {
$_SESSION['teachersurname'] = $_POST['teachersurname'];
}
Now the problem I have is that I want to insert this into the database but I don't want the firstname and surname to be inserted into the database, I want the teacher's username to be inserted into the database.
For example: John May has a username T1, Ben Watts has a username T2 and Jean Smart has a username T3.
So how can I get it so that it looks up the teacher's firstname and surname, find the teacher's username from that (field for username is TeacherId) and then insert the username in the 'TeacherId' field in the "Session" Table?
I have connection to database and all that. I just want to how to code it so that it finds a teacher's username (TeacherId) by its full name then insert it into the database?

Do you want Teacherid (like T1,T2,T2) by looking username which is obtained by selecting surname and forename??
SELECT TeacherId FROM (SELECT Username from teachers where forename='forename' and surname='surname')

Why not query database with WHERE name='$name' (combination of firstname.' '.lastname)
And use the returned ID in your script?

Related

How to insert data to another table by id

I want to complete user sign up.
firstly user will write username and password, table called users, I have id as primary key.
secondly user will write address, phone number and zipcode, table called userdata, I have userid as index(key).
I have did a relation between id from table user and userid from table userdata.
So now I want to insert data from php code I did two forms one for user and it has two input for username and password it works well and data inserted.
in the second form I have select option has id from table users and it works well.
then in the same form I have three inputs phone number, address and zipcode.
so the question is how I can insert the data to userdata by the same to the same id. so userid in table user will be the same id in table userdata.
I need sql code.
I used that :
"INSERT INTO userdata(address, phone, zipcode) VALUE (:address, :phone, :zip) SELECT 'id' FROM 'users';"
First :
$q = "NSERT INTO users(username, password) VALUE ('Adam', '12345678')";
$r = $connect->query($q);
//get the last id u inserted
$last_id = mysqli_insert_id($connect);
Second :
$q = "NSERT INTO userdata(address, phone, zipcode,user_id) VALUE ('California', '12345678', '1111','$last_id')";
$r = $connect->query($q);
and if you want to make the (id) not the (userid) the same just :
$q = "NSERT INTO userdata(id, address, phone, zipcode) VALUE ('$last_id','California', '12345678', '1111')";
$r = $connect->query($q);

php code for uploading/changing Image for an existed user from android after logging in

My table into the databases has 5 columns: id, username, email, password, image_path. id, username, email and password columns are filled while user registering but iamge_name column must be filled after logging in by uploading a picture from gallery. Also i need that user can change his/her picture.
for example the table is like this:
id------username------email------password------image_path
1----------alex--------a#g.com-----123456-------
2----------alex2------a#g2.com----123456-------
now i need to insert an image for alex2. please help me first to find where alex2 there is and then insert the picture to the image_path column of id=2(where username is alex2). So please help me to complete this code:
public function insertImageByUsername($username){
//here i want to find row related to alex2
$stmt = $this->con->prepare("SELECT id FROM my_table WHERE username = ?");
//here i want to insert image to the image_path column of alex2 related row
$stmt2 = $this->con->prepare("INSERT INTO `my_table` (`image_path`) VALUES ( ?);");
}
thanks a lot for any help.

Get data from other column details

My table looks like this:
check_in_id | amount | date | user_id (foreign key of user table )
In the interface it asks user to enter amount, date, username, password. When running the insert into query, how do I get user_id from that user table to insert into this table?
I suppose you have one main table (say: tbl_login ) to store username, password, user_id (primary key).
So, You first retrieve user_id with username and password like:
SELECT user_id FROM tbl_login WHERE username = '$username' AND password = 'password';
Where $username and $password is your entered username and password from interface.
So after getting user_id , now you have all needed values, so you can easily insert these in your given table.
Since the information in you query is incomplete, I will try to give an answer with certain assumptions.
check_in_id: an auto increment id in
You want to insert into table only if username and password matches.
The variables with '$' are the user inputs
insert into <your table>
(check_in_id, amount, date, user_id)
select null, $amount, $date, user_id from user
where username = $username and password = $password

Adding variable to database without replacing current value?

I'm trying to do a MySQL Query that adds information to a database in a format that doesn't overwrite it's current value, but instead appends it as in the friends column would have friend1, friend 2, friend 3 ... etc.
First I am unsure if INSERT INTO is right:
$username = $_SESSION['username'];
mysql_query("INSERT INTO members (friends) WHERE username = $username
VALUES ('$friendtoadd')");
What I want to do is have this do two things
1) Add the friend in a format mentioned so that it can later be called out that if the $username set in session is active then the results posted on the page are only from those other users contained in their friends column.
2) If the $friendtoadd already exists in their column for friends then it does nothing.
UPDATE members SET friends = CONCAT_WS(', ', friends, '$friendtoadd') where username = '$username'
I hope $friendtoadd as well as $username are sanitized through mysql_real_escape_string
Also, this design isn't very good. You should have a seperate friends table
so
table friend
id (auto increment), userID, name)
then do
Insert into friend SET userID = $userID, name = '$friendName'

save check box name and value into a table

please i need come help i have a form of check boxes i want to save the name and value of the boxes that registered user selects into a table ,now i have registered users information saved in a table ,how the table that saves the registered users selection is to be created to have a relation with the table of users????
my registered users table
<?php
$connect=mysql_connect("localhost","root") or die (mysql_error());
mysql_select_db("login") or die (mysql_error());
$query="CREATE TABLE users(
userid int not null auto_increment primary key,
username varchar(20),
password varchar(40),
usertype varchar(20),
firstname varchar(30),
lastname varchar(30),
street varchar(50),
city varchar(50),
country varchar(50),
postcode varchar(10),
gender varchar(6),
dateofbirth date,
telnumber varchar(50),
email varchar(50),
insertdate date,
vals text,
comment text
)";
$result=mysql_query($query,$connect) or die('Cannot Create User');
mysql_close();
print " DataBase created well" ;
?>
Let's say you have table of users like this:
**users**
------------------------------
userid username etc...
1 billy ...
2 joe ...
Now you have to create table like this:
**users_checkboxes**
------------------------------
id user_id name value
1 1 newsletter 1
2 1 send me spam 0
3 2 newsletter 0
4 2 send me spam 0
This information means that billy chosen to receive newsletter and no spam while joe does not want to receive anything.
The key is to have userid from the first table here, this is called foreign key. You can read more about it at Wikipedia.
You might like to reword your question so it's easier to read, here's what I think you meant:
How can I update user's data in the database using a form of checkboxes?
Checkboxes are inputs and will come up like text inputs in your GET or POST array (you're more likely to use POST for this).
E.g.:
<form action="update.php" method="post">
<p><input type="checkbox" name="showDate" /> Show the date on every page.</p>
<p><input type="submit" value="Update user record" /></p>
</form>
Then in your PHP you would use something a little like this:
<?php
// Check form has been submitted
if (isset($_POST['showDate'])) {
// Assuming the user is updating their own profile and their
// user id is registered in a session
$update = 'UPDATE users SET showDate = %d WHERE userid = %d';
// It's better to use prepared statements for this, see http://php.net/mysqli
mysql_query(sprintf($update, $_POST['showDate'], $_SESSION['userid']));
} else {
// show the form
echo $form;
}
Hope this makes sense (and is what you were looking for) - your question was unclear to me.

Categories