I´m trying to insert data into diferent tabels with php, but dosent get it to work.
Heres my db structure: 1
Table: event
Structure: FromDate | ToDate | locationID
Heres my db structure: 2
Table: jointable
Structure: EventID | locationID
The thing i want to do more specific, i have inputs for "Fromdate" and "todate" and "locationid". I want to input fromdate and todate into table1, and locaionid to table2
Here is my sql query:
mysql_query("INSERT INTO event (FromDate, ToDate, locationID)
VALUES ('$_POST[startdate]','$_POST[enddate]','$_POST[locationID)");
Any idea how i can "sort out" locationID to input it on my jointable instead?
Excuse for my bad enlish, i hope you understand what im trying to do.
Try this:
mysql_query("INSERT INTO `events` VALUES('".$_POST["startdate"]."','".$_POST["enddate"]."','".$_POST["locationID"]."')");
Call another query for inserting into other table:
mysql_query("INSERT INTO `jointable` VALUES('','".$_POST["locationID"]."')");
Write 2 different queries
Something Like This :
$startDate = $_POST[start_date];
$endDate = $_POST[to_date]
$locationId = $_POST[location_id];
mysql_query("INSERT INTO event VALUES ('$startDate','$endDate','$locationId");
mysql_query("INSERT INTO jointable VALUES ('','$locationId')");
Note : mysqL_* functions are being depreciated . Avoid the,
Related
I have 4 mySQL tables with the following entries:
user
-user_id PK,AI
-user_name
-user_mobil
-user_passw
-user_email
bookingdetails
-booking_id PK,AI
-booking_date
-booking_time
-person_number
booking
-booking-_id FK
-restaurant_id CK
-user_id CK
restaurant
-restaurant_id PK
-restaurant_name
-restaurant_address
-restaurant_description
I would like to make a booking, I insert all the bookingdetails data, which gives me a AI booking_id, and after I would like to make my booking table and insert the restaurant_id and the user_id With the same booking_id which was given by the bookingdetails table.
I made the following code for achieve that in php on a localserver:
$booking_date=$_POST["booking_date"];
$booking_time=$_POST["booking_time"];
$number_of_place=$_POST["number_of_place"];
$customer_id=$_POST["customer_id"];
$restaurant_id=$_POST["restaurant_id"];
$res;
$sql_query = "INSERT INTO bookingdetails(booking_date, booking_time, number_of_place) VALUES ('$booking_date','$booking_time', '$number_of_place')";
$sql_query2 = "INSERT INTO `booking`(`booking_id`, `customer_id`, `restaurant_id`) SELECT booking_id, '$customer_id', '$restaurant_id' FROM bookingdetails ORDER BY booking_id DESC LIMIT 1 ;";
if(mysqli_query($con,$sql_query))
{
}
else
{
}
if(mysqli_query($con,$sql_query2))
{
}
else
{
}
?>
Is that a legit solution on a server which joining to an Android app? Is there any case, that i don't get the good id on the second query? What would be a better solution?
Answer given in comment by #Mark Ng
Use last insert id, criteria is that your pk has to be AI.
The mysqli_insert_id() function returns the id (generated with AUTO_INCREMENT) used in the last query.
Source: w3schools.com/php/php_mysql_insert_lastid.asp
To elaborate
you have to execute the query from which you need the last inserted id, then you can access that by using
$last_id = $conn->insert_id;
which in turn you can use for your following query.
Note:
I see you use a query to use the results for your insert query, but your syntax is incorrect (your missing values)
so I'm storing order in my table like this:
ID, NAME, ORDER
1, name_1, 1000
2, name_2, 2000
3, name_3, 3000
so basically:
ORDER = ID*1000
and what I do is after inserting row into this table like this
INSERT INTO table(name) VALUES('name_x');
I update order value in this row like this:
UPDATE table SET order = mysql_insert_id()*1000 WHERE id = mysql_insert_id();
Is there any other way to not make this into two statements? Like some magical mysql function ACT_ID()
so it would be like
INSERT INTO table(name, order) VALUES('name_x',ACT_ID()*1000);
Thanks in advance for all your responses.
Yes there is:
INSERT INTO tbl2 (id,text) VALUES(LAST_INSERT_ID(),'text');
See MySQL Docs here
I've two tables in my joomla 2.5 site component that trying to inset two tables and first table ID should be second table foreign-key.
Table: MYTABLE
-------------------------------------------------------------
| mytab_id | mytab_name | mytab_country | mytab_city |
-------------------------------------------------------------
Table: YOURTABLE
-------------------------------
| yourtab_id | group_name |
-------------------------------
mytab_id is auto increment value and that should insert to second table yourtab_id as foreign-key.
How should I insert data to both tables as first table id value to second table id.
I've tried below code but it doesn't work.
$insert_query = "INSERT INTO #__mytable (mytab_name, mytab_country, mytab_city) VALUES ('". $mytab_name."',".$mytab_country.",'".$mytab_city."'); ";
$db->setQuery( $insert_query );
$db->query();
$insert_query2 = "INSERT INTO #__yourtable (yourtab_id, group_name) VALUES (".LAST_INSERT_ID().", 2);";
$db->setQuery( $insert_query2 );
$db->query();
Thanks,
Try This,
In Joomla you can get the last insert id using
$db->insertid();
For details about Joomla DB Queries, Also try to use more standards in your query.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Insert columns.
$columns = array('mytab_name', 'mytab_country', 'mytab_city');
// Insert values.
$values = array($db->quote($mytab_name), $db->quote($mytab_country), $db->quote($mytab_city));
// Prepare the insert query.
$query
->insert($db->quoteName('#__mytable'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
// Set the query using our newly populated query object and execute it.
$db->setQuery($query);
$db->query();
You can make your query standards like here
Hope it helps..
You can use the available php function to achieve this
mysql_insert_id()
you can refer Here For Documentation in php
I am using Php to insert values into MySQL table.
What i am trying to do is:
There are three columns that i have to check. 'namel1', 'namel2' and 'namel3'.
Conditions:
If '$name' does't exist in any of the three column then put value in 'namel1'.
If '$name' exist in 'namel1' then put value in 'namel2' and if 'namel2' contains the value then put it in 'namel3'.
My current MySQL query to insert name and image path is this i want to modify it to meet above conditions:
$chk_img_db = mysql_query("select * from cvapptable where img_path='$cvh_myimg_url'");
if(mysql_num_rows($chk_img_db)<1) {
mysql_query("insert into cvapptable(namel1,img_path) values ('$name','$cvh_myimg_url')");
}
I unable to get any solution from web.
Please help. Thank you.
It's not easy to find on the net because it's a situation you shouldn't get yourself into.
You should consider normalizing the table.
Instead of having a table with the columns:
cvapp: id | img_path | namel1 | namel2 | namel3
Consider changing it to two tables:
cvapp: id | img_path
names: id | cvapp_id | name
To then select every name, you just do a query like so:
SELECT name
FROM cvapp INNER JOIN names on cvapp.id = names.cvapp_id
WHERE <condition>
That way, you can have as many names as you want, and it's much easier to insert a new one:
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 1");
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 2");
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 3");
you can try self join and search column of you tables
I have some insurance information in a website, and I'd like to only edit certain fields the user wants to change like for example:
user, id, phone, address, city
and the user wants to change his city and phone...do i have to make a query for each specific case or is there a code that can help me retrieve the key(phone) and value (9397171602)??
to then send it in a query
Basic update would take the form of:
UPDATE table_name SET column_1 = value_1, column_2 = value_2 WHERE column_3 = value_3
Where col1, col2 would be your city and phone, and col3 would be the user id. Check out the MySQL website http://dev.mysql.com/doc/refman/5.0/en/update.html for more info
There are number of ways to update a record safely. Conside the following pseudo code + php program.
class Example
{
function UpdateRecord($editedRecord)
{
//Fetch existing record from the database
$originalRecord=DB::fetchExample($editedRecord->getId())
//validate each edited field and it is valid then assign its value
//to the originalRecord's field
if(IsValid($editedRecord->getName())
{
$originalRecord->setName($editedRecord->getName());
}
.....
//update the record.
$originalRecord->Update();
}
}
Just add some sql to it:
$sql = "UPDATE example SET col_1 = val_1, col_9 = val_9 WHERE col_7 = val_7";
mysql_query($sql);
Then replace the columns and values with you stuff. For further info: PHP MySql Update