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
Share on other sites

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();

Link to comment
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
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));

Edited by Freid001
Link to comment
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));

Edited by Freid001
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.