Jump to content

Insert Query Help!


Freid001

Recommended Posts

I'm not sure what I am doing wrong here but when I run the code it just does nothing. It is supposed to insert new data into the data base but it just does nothing. Not sure why. But if I insert text values not variables it works fine.

 


$time = now();
$sql = "INSERT INTO `posts` (`title`,`image`,`text`,`author`,`date`) VALUES('$title ', '$image', '$text', '$user', '$time')";
$k = $db->prepare($sql);
$k->execute();

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/272190-insert-query-help/
Share on other sites

Yes I know I did also try using this which is a proper prepared statement I think. But this doesn't work either and I have the same problem. With that code too.

$query = "INSERT INTO posts(`post_title`,`post_image`,`post`,`author`,`time`) VALUES(':post_title', ':post_image', ':$post', ':$user' ,':$time')";

$a=k = $db->prepare($query);
$k->execute(array(':post_title'=>$post_title,':post_image'=>$post_image,':post'=>$post,':user'=>$user,':time'=>$time));

Link to comment
https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400453
Share on other sites

This is slightly off topic, but why are you using a prepared query statement, but putting php variables directly into the query statement. That defeats the purpose of using a prepared query statement.

 

Yes I know I did also try using this which is a proper prepared statement I think. But this doesn't work either and I have the same problem. With that code too.

$query = "INSERT INTO posts(`post_title`,`post_image`,`post`,`author`,`time`) VALUES(':post_title', ':post_image', ':$post', ':$user' ,':$time')";

$a=k = $db->prepare($query);
$k->execute(array(':post_title'=>$post_title,':post_image'=>$post_image,':post'=>$post,':user'=>$user,':time'=>$time));

Link to comment
https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400454
Share on other sites

Ok using this code I can now insert values in to the data base but the value inserted in is :title etc. not the variable value.

 


//Add data to database
$query = "INSERT INTO posts(`title`,`image`,`post`,`author`,`time`) VALUES(':title', ':image', ':post', ':user' ,':time')";
$a = $db->prepare($query);
$a->execute(array(':title'=>$post_title,':image'=>$post_image,':post'=>$post_text,':user'=>$user,':time'=>$time));

Link to comment
https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400458
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.