Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey,

Im stuck for a while with trying to get data using bindvalue in a table and using the same query to get ID from a other table into this table...

PHP
$query = $conection->prepare("INSERT INTO `posts`(`Titel`, `Prioriteit`, `Image`,`Samenvatting`, `Post`, `Poster`, `Datum`) VALUES (:titel, :prioriteit, :image, :samenvatting, :post, :poster, :datum)");
                                   // met bindvalue worden alle values gebonden aan de parameters van de class
                                   $query->bindValue(":titel", trim($this->titel, " "), PDO::PARAM_STR);
                                   $query->bindValue(":prioriteit", $max+ 1, PDO::PARAM_STR);
                                   $query->bindValue(":image", trim($this->image, " "), PDO::PARAM_STR);
                                   $query->bindValue(":samenvatting", trim($this->samenvatting, " "), PDO::PARAM_STR);
                                   $query->bindValue(":post", trim($this->post, " "), PDO::PARAM_STR);
                                   $query->bindValue(":poster", $_SESSION["userId"], PDO::PARAM_STR);
                                   // huidige datum word opgeslagen
                                   $date = date('Y-m-d H:i:s');
                                   $query->bindValue(":datum",($date) , PDO::PARAM_STR);
                                   $query->execute();

                                   $query3 = $conection->prepare("INSERT INTO `tags`( `Content`, `Blog_id`) SELECT ':content', posts.id AS `Blog_id` FROM `tags` INNER JOIN `posts` ON tags.Blog_id=posts.id");

                                   $query3->bindValue(":content", trim($this->tags, " "), PDO::PARAM_STR);
                                   $query3->execute();



im keep getting :content in the column instead of the data i gave the bindvalue, if i try Value it does work but i think the innerjoin wont work that way.. the innerjoin doesnt work anyway tho.


What im trying to get is if i use this query it should fill the content collumn and at the same time get the ID from the posts table stored in this row.

Sorry for bad english,,
Posted
Updated 2-Apr-15 4:05am
v2

1 solution

That is because you are passing `:content` NOT as a PARAMETER but as a LITERAL VALUE to the Content column.
insert into `tags` (`Content`, `blog_id`) select ':content', posts.id ....

Refer: http://www.w3schools.com/sql/sql_insert_into_select.asp[^]
Remove the select from the insert. After the insertion operation, use a separate select to get the content.
 
Share this answer
 
v3
Comments
Member 11576150 2-Apr-15 10:42am    
can i do that in the same query?
Member 11576150 2-Apr-15 11:04am    
INSERT INTO `tags`(`Content`, `Blog_id`) VALUES ('test', (Blog_id) SELECT MAX(id) FROM posts);

i tried this but doesnt work.. is there something like this that does work?
Peter Leow 2-Apr-15 11:39am    
No. Two separate sql operations.
Member 11576150 2-Apr-15 11:46am    
Tried that aswell but that makes 2 differen row .. one with the right content value i typed in and the other with the right Blog_id from the other table.. but i need them in 1 row
Peter Leow 2-Apr-15 11:54am    
I am afraid you do not understand what I said. Suggest you read up on sql tutorial like the link that I have provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900