im using mysql, these are two tables i have:
posts {id, user_id, post, date}
post_tags {id, tag, post_id(references id in post), user_id}
what im trying to do is if the post has a #tag , i insert the intial post in the POSTS table, and the data in the post_tags table, how could i do that simlateanously?
P.S. i already know how to check if a post has a tag!! i just want to undertand, how can insert data into both!! espcially the ID's because they are generated within mysql(autoincrement)!!
You can seperate these two queries and run them after each other. You can use mysql_insert_id() for the last inserted id in an table.
$query = "INSERT INTO posts (id, user_id, post, date)
VALUES (id, user_id, post, date)";
mysql_query($query) or die(mysql_error().$query); // run query
$lastid = mysql_insert_id(); // this will get the last inserted id.
$query = "INSERT INTO post_tags (id, tag, post_id, user_id)
VALUES (id, tag, ".$lastid.", user_id)";
mysql_query($query) or die(mysql_error().$query); // run query
Related
$query = "INSERT INTO `posts` (title, author, body, tags)
VALUES (`$title`, `$author`, `$body`, `$tags`)
(SELECT * FROM category WHERE name = $category)";
posts and category both are different tables?
If you have variables with the values:
$etitle = somehow_escape($title); ...
INSERT INTO posts
(title, author, body, tags)
VALUES
('$etitle', '$eauthor', '$ebody', '$etags');
If fetching the values from another table:
INSERT INTO posts
(title, author, body, tags)
SELECT title, author, body, tags
FROM category
WHERE ...
You must escape the values -- Think about what would happen if a ' were in the $title!
When using SELECT with INSERT, there is no VALUES cause.
No need for the parens around the SELECT subquery (in this situation).
I have a MYSQL table called 'Buyers'. I am taking form data and inserting it into the table on a page called insert.php.
Now, along with the column names for the form fields, I have an Auto-increment ID column in the table. What I want is; once I send the form data to the table, I want to then execute some SQL to get the ID number for the row into which I just inserted the data into.
E.g, I have the SQL:
INSERT INTO Buyers (name, email, job) VALUES ('$_POST[name]', '$_POST[email]', '$_POST[job]');
This will create a row in the 'Buyers' table with the data from form fields 'name', 'email' and 'job'.
This row will have an ID number generated by Auto Increment.
How, then, can I then select that exact ID number? Is there some way to select the most recent row in the table, since that is the row which contains the ID number I want?
Mysqli : mysqli_insert_id()
<?php
mysqli_query($con,"INSERT INTO Buyers (name, email, job) VALUES ('$_POST[name]', '$_POST[email]', '$_POST[job]')");
$last_id = mysqli_insert_id($con);
?>
PDO : PDO::lastInsertId()
<?php
$stmt = $con->prepare("INSERT INTO Buyers (name, email, job) VALUES (?,?,?)");
$stmt->bind_param("sss", $_POST['name'], $_POST['email'], $_POST['job']);
$stmt->execute();
$last_id = $con->lastInsertId();
?>
Assuming $con as connection.
Mysql : mysql_insert_id()
<?php
mysql_query("INSERT INTO Buyers (name, email, job) VALUES ('$_POST[name]', '$_POST[email]', '$_POST[job]')");
$last_id = mysql_insert_id();
?>
NOTE: [The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead ]
SELECT x.*
FROM messages x
JOIN
( SELECT from_id, MAX(time_sent) time_sent GROUP BY from_id ) y
ON y.from_id = x.from_id
AND y.time_sent = x.time_sent;
The last part of this puzzle is left as an exercise for the reader.
Use mysql_insert_id() to retrive ID. More details - http://php.net/manual/en/function.mysql-insert-id.php
I am an amateur hobbying a video site, where users can submit video urls. Everything works, but now I want multiple categories to be able to be selected for one same video. Categories are stored in seperate tables. I am sure you will understand from the code:
mysql_query("INSERT INTO videos
(title, source, thumb_large, author, email_by) VALUES('$title', '$video_url', '$thumb_url', '$author', '$email') ") or die(mysql_error());
mysql_query("INSERT INTO videos_categories
(category_id) VALUES('$category_name_id') ") or die(mysql_error());
What I want to do is to add another category for the same video ID the tables are both on AUTO INCREMENT synced with each other at the moment:
mysql_query("INSERT INTO videos_categories (category_id) VALUES('$category_name_id_two') ") or die(mysql_error());
The variable is currently registered as:
$category_name_id = mysql_real_escape_string(htmlspecialchars($_POST['category_id']));
And is currently selected through dropdown (select with name: category_id) and it works fine. However, I want to change this ofcourse into select boxes, so mulitple, or upto 3 categories can be selected, instead of having 3 dropdown menus.
I hope you can help me out with this, I am stuck here.
Edit:
I wanted to add that there are three tables, one from which categories are retrieved, one that stores the video_id+category_id which should make at possible to have multiple categories per video, and one with the videos.
Thanks
So you need to change the select box to something like this:
<select name="category_id[]" multiple="multiple">
<!-- Options -->
</select>
...
In backend, After this query:
mysql_query("INSERT INTO videos
(title, source, thumb_large, author, email_by) VALUES('$title', '$video_url',
'$thumb_url', '$author', '$email') ") or die(mysql_error());
Do:
$video_id = mysql_insert_id();
$categories = $_POST['category_id'];
$values = '';
foreach($categories as $category_id) {
$values .= "('$video_id', '$category_id'),";
}
$values = rtrim($values, ',');
$query = "INSERT INTO videos_categories(video_id,category_id) VALUES $values";
mysql_query($query);
Few Suggestions:
Since mysql_* apis are deprecated, please don't use them. Switch to mysqli.
Do the validation and sanitization of the data before using in sql queires.
Hello i want insert data into 2 different tables.
First table 'Events:'
id,
name,
desc,
date
Second table 'Events_sub_cat:'
id,
event_id,
name,
desc,
date
I want to insert 2 different data in one query like:
INSERT INTO Events (name, desc, date) VALUES ('name', 'desc', 'date')
INSERT INTO Events_sub_cat (event_id, name, desc, date) VALUES (event_id, 'name', 'desc', 'date')
And i want get in the same query the ID of the even what i just inserted in table 'Events'
to let know to another INSERT what is the 'event_id'
There you go a suggestion:
Insert records into two table at once
As said in the post, there's no way to insert without using mysql_insert_id()
functions such as lmysql_insert_id() return the last inserted id.
you can select the last id from Events and put it in the second insert
I want to insert data from one table into another where one field equals another in both tables. So far this works. The problem is, I also need to insert additional data into those same rows that is not included on the first table.
//enter rows into database
foreach($_POST['sku'] as $row=>$sku)
{
//this is the data that needs to be added to the table
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg";
$thumbnail="/$item_sku.jpg";
//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price)
SELECT PR_SKU, PR_Description, PR_UnitPrice
FROM products
WHERE PR_SKU = '$sku'";
//I need something here to add the above variables to the same row where PR_SKU = '$sku'
if (!mysql_query($sql))
{
die('Error: '.mysql_error());
}
echo "$row record added";
}
The columns for the missing data on magento_table are called 'image', 'small_image', and 'thumbnail'. This is simple a hack to put data from an old product table into a new product table, export as a CSV, and run a profile in Magento. I don't need to worry about SQL injections. It's something I'm running off of a local machine. I'm trying to avoid as much manual data entry as possible while switching products over to a new ecommerce system. Thanks for any help you can give.
Selecting literal values should do what you intend:
$sql= "INSERT INTO magento_import (sku, description, price, image, small_image, thumbnail)
SELECT PR_SKU, PR_Description, PR_UnitPrice, \"$image\", \"$small_image\", \"$thumbnail\"
FROM products
WHERE PR_SKU = '$sku'";
You can use another update statement to do this. I doubt if you can do this with one single query
foreach($_POST['sku'] as $row=>$sku)
{
//this is the data that needs to be added to the table
$item_sku=$sku;
$image="/$item_sku.jpg";
$small_image="/$item_sku.jpg";
$thumbnail="/$item_sku.jpg";
//currently this is what is working to import data from one table to the other
$sql= "INSERT INTO magento_import (sku, description, price)
SELECT PR_SKU, PR_Description, PR_UnitPrice
FROM products
WHERE PR_SKU = '$sku'";
//I need something here to add the above variables to the same row where PR_SKU = '$sku'
if (!mysql_query($sql))
{
die('Error: '.mysql_error());
}else {
$sql = "UPDATE magento_import SET item='$item',image='$image',small_image='$small_image',thumbnail='$thumbnail' WHERE PR_SKU = '$sku'";
echo "$row record added";
}
}
You can just add the variables directly to the query. As long as they are quoted they will work as simple values.
$sql= "INSERT INTO magento_import (sku, description, price, x, y, z)
SELECT PR_SKU, PR_Description, PR_UnitPrice, '$x' AS x, '$y' AS y, '$z' AS z
FROM products
WHERE PR_SKU = '$sku'";