Freid001 Posted December 19, 2012 Share Posted December 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/ Share on other sites More sharing options...
jazzman1 Posted December 19, 2012 Share Posted December 19, 2012 I think that now() is not a php function is a sql one. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400385 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 I think that now() is not a php function is a sql one. You are correct OP: try this $sql = "INSERT INTO `posts` (`title`,`image`,`text`,`author`,`date`) VALUES('$title ', '$image', '$text', '$user', NOW())"; $k = $db->prepare($sql); $k->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400393 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 I have tried to use that but it still does not work. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400416 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 I have tried to use that but it still does not work. What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400417 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 Its really wired. When I use the code it works fine if I am inserting just string values. But then when I use variables it just doesn't work for some reason. I not understand why? Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400430 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 But what does 'doesn't work' mean? You get a white page? No results in your db? What? Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400433 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) I am just getting no result/new entry in the data base if I am inserting variable values. But if I insert just string values it works. Edited December 19, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400435 Share on other sites More sharing options...
Jessica Posted December 19, 2012 Share Posted December 19, 2012 When you echo the SQL what does it say? Check out the link in my signature on debugging SQL Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400440 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 When you echo the SQL what does it say? Check out the link in my signature on debugging SQL I have have used that but it returns nothing just a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400446 Share on other sites More sharing options...
PFMaBiSmAd Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400449 Share on other sites More sharing options...
Jessica Posted December 19, 2012 Share Posted December 19, 2012 I have have used that but it returns nothing just a blank page. Then you need to turn on error reporting to E_ALL. And PFMaBiSmAd is 100% right. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400450 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 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)); Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400453 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) 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)); Edited December 19, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400454 Share on other sites More sharing options...
Jessica Posted December 19, 2012 Share Posted December 19, 2012 In your SQL you are doing ':$post', when it should be :post You NEED to turn on error reporting. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400457 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) 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)); Edited December 19, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400458 Share on other sites More sharing options...
Jessica Posted December 19, 2012 Share Posted December 19, 2012 In your SQL you are doing ':$post', when it should be :post You NEED to turn on error reporting. So now you have ':post'. Still not right. Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400483 Share on other sites More sharing options...
Freid001 Posted December 19, 2012 Author Share Posted December 19, 2012 Excellent! It is working now thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/272190-insert-query-help/#findComment-1400484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.