I have a database that consists of 3 tables.
user: 4 columns userId [primary key], name, emailId, password
residentController: 3 columns userId [foreign], departmentId [foreign], checkDefault
checkDefault will be set to 1.
department with 6 columns: departmentId [primaryKey], departmentName, departmentDesc, departmentLink, checkDefault, departmentCategory
Here is the sequence that follows when a user registers for an android application. The following php script will be called.
It will add a record of the user into the database. After insertion of the data in the user table, I want to get all the "departmentNames" from department table and insert it into residentController. In the end, after particular user registers, then residentController must have these values populated into the residentController table.
userId departmentId checkDefault
---------------------------------
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
Here is my php script.
<?php
require "init.php";
$fullname = "Fahman Khan";//$_POST["user"];
$emailid = "fxk120#hotmail.com";//$_POST["user_name"];
$userpass = "Windows";//$_POST["user_pass"];
$sql_query = "insert into user values(NULL,'$fullname','$emailid','$userpass')";
if(mysqli_query($connection, $sql_query))
{
echo "<h2> Data insertion success</h2>";
//Get the user id of the the registered user
$sql_queryTwo = "SELECT userId FROM user WHERE emailId='".$emailid."'";
//Store the result of the query in a variable
$result= mysqli_query($connection,$sql_queryTwo);
$row = mysqli_fetch_array($result);
//Retrive the user id
$data = $row[0];
//Insert default departments into residentController
}
else
{
//echo "Data insertion error".mysqli_error($connection);
}
?>
My question is that in my current php script, I don't know how I can insert departmentID into residentController table. departmentId is a foreign key and in my current department table there are 5 departments. I just need help writing the part in my php where I can insert in values into my residentController table. As of now, the current php script only adds a new user, but I need help inserting values into my residentController table.
Related
Insert New Row (register) , in the meantime insert "Auto Increment ID" into another column
I have no idea how to do this (able to do normal registration)
$query = "insert into usertable (name,email,password) values('$username','$email','$password')";
In database table:
ID(AI) name email password nameID
-----------------------------------------------------------------------------
1 test1 test1#gmail.com 1234 NULL (what I want: 1)
2 test2 test2#gmail.com 1234 NULL (what I want: 2)
3 test3 test3#gmail.com 1234 NULL (what I want: 3)
Yes. you could do with last inserted id mysqli_insert_id($conn)
$query = "insert into usertable (name,email,password) values('$username','$email','$password')";
if (mysqli_query($conn, $query)) { // after you insert executed successfully
$last_id = mysqli_insert_id($conn); //your last inserted auto increment value
//update with same row another column
mysqli_query($conn,"UPDATE usertable SET nameID='$last_id' WHERE ID='$last_id'");
}
I have two tables a user table and product
the user is for registering users and login and the product is for allowing the user to post his product. just for listing.
The user table would be like this
user_id (int)
user name (varchar)
user_email (varchar)
user_password)
now i can insert data into and from this table with easily.
In the second table (product table)
product_id (int)
product_name (varchar)
product_description(varchar)
etc.
i have extra field that says user_id
what i basiclly i want to do is, i want insert the user_id from table 1 into a field in table table.
so lets say
i have a form for entering product details
the form include
product_id, name, desc etc all as a textfields
when the user fill all field in that forms, considering that the user is already login, so i want know who inserted that data.
i hope that make sense
The code i used for inserting data into product table is
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$name = $_POST['name'];
$price = $_POST['price'];
$description= $_POST['description'];
$userphone = $_POST['userphone'];
//Creating an sql query
$sql = "INSERT INTO list (name,price,description userphone) VALUES ('$name','$price','$description','$userphone')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Employee Added Successfully';
}else{
echo 'Could Not Add Employee';
}
//Closing the database
mysqli_close($con);
}
So to emphasis
i want the user_id from table 1 to be inserted into table 2.
thanks
I'm trying to build a status/post. I have 4 fields in my table(post) id, user_id, status_time and status_content. I want to get the data on my status_content of 1 user_id only, can someone help me?
For example:
I have 3 users
user 1 = posted 3times
user 2 = posted 2times
user 3 = posted 5times
How can i only get all the data posted by user 3?
Sorry for my english.
Code to send data to my database
profile.php
if(isset($_POST['status_content'])){
$status_content = $_POST['status_content'];
if(!empty($_POST['status_content'])){
add_status($_SESSION['accts_id'], $status_time, $status_content);
}
}
core.php
function add_status($accts_id, $status_time, $status_content){
$sql = "INSERT INTO `post` (user_id, status_time, status_content) VALUES ('$accts_id', '$status_time', '$status_content')";
mysql_query($sql);}
you can use this as follows
to get data particularly for user 3 then
select status_content from post where user_id = '3'
to get data for any users
select status_content from post where user_id = $_SESSION['accts_id'];
[UPDATED]
Im trying to store the result of a secondary, sequential post into the results of a first query already stored in my table.
First query:
// Get values from form
$monto=$_POST['monto'];
$personas=$_POST['personas'];
$ciudad=$_POST['ciudad'];
$giro1=$_POST['giro1'];
$giro2=$_POST['giro2'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(monto, personas, ciudad, giro1, giro2)VALUES('$monto', '$personas', '$ciudad', '$giro1', '$giro2')";
$result=mysql_query($sql);
The value of giro2 is to be captured based on the selection if giro1 but should all end up in same row in same table as:
uid | monto| personas| ciudad| giro1| giro2
--------------------------------------------
1 | 5 | 1 | NY | food | Null
second form POST:
// Get values from form
$giro2=$_POST['giro2'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name (giro2) VALUES ('$giro2') SELECT uid FROM $tbl_name WHERE uid = 1";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.html'>Back to main page</a>";
} else {
echo "ERROR";
}
In the end I get ERROR or if I remove SELECT ... I get giro2 stored in uid=2
How can I save the second post into same row and correct column?
This line has 2 sql statements in the same line
$sql="INSERT INTO $tbl_name (giro2) VALUES ('$giro2') SELECT uid FROM $tbl_name WHERE uid = 1";
you have to divide it in two. First you get the uid, the you use the value to update the table with UPDATE, not with INSERT. INSERT inserts new values in the table, UPDATE, as the name says, updates old ones.
However you already seem to know that the number of the uid you want to update is 1. Then the only thing you need is:
$sql = "UPDATE TABLE "$tbl_name" SET giro2 = "$giro2" WHERE uid = 1";
$result=mysql_query($sql);
I want generate a registration no. of a user, but the registration no. should be:
the first two digit no. should be year as - 15
the second two digit no. should be month as - 06
the third two should be characters which are already assigned as - SY
the fourth should be a number as - 012
The last number should be incremented when user clicks the button
So in this case my registration numbers would be "1506SY012", then "1506SY13", etc. How to generate it in PHP code?
You need some place, where your users will be saved. For example: MySQL table. Create table with fields, for example: ID, SID, NAME, PASSWORD; ID field and index Primary Key, Auto increment. It gives you unique ID for every new user (just INT: 1, 2, 3, 4....).
Than, after you add new line to this table, you need to update it with:
$query = "insert into users values (NULL, '0', :name, :pass)";
$mysqli->query($query);
$lastId = $mysqli->insert_id;
$sid = date(dm, time()).'SY'.sprintf('%010s',$lastId);
$query = "update users u set u.sid = :sid where user_id = :id";
$mysqli->query($query);
And you'll get SID with needed value.