Forum Moderators: coopster
INSERT into db (title, author, year) Values (?,?,?),
array($_POST['title'],($_POST['author'],($_POST['year']);
It works if I just have one place holder but as soon as I add more it doesn't. Also is there another way of correcting the apostrophe problem?
Any help appreciated.
$title = addslashes($_POST['title']);
$author = addslashes($_POST['author']);
$year = addslashes($_POST['year']);
INSERT into db (title, author, year) Values ($title,$author,$year)
You can also use the function mysql_escape_string() to filter your data before inserting it.
(Note: beware magic quotes, if they are turned on then your ' chars are already escaped and addslashes will just add extra slashes where you won't want them).
if (!get_magic_quotes_gpc()) {
$title = addslashes($_POST['title']);
$author = addslashes($_POST['author']);
$year = addslashes($_POST['year']);
}
INSERT into db (title, author, year) Values ($title,$author,$year)
Adding this to my INSERT query solved the problem
if (!get_magic_quotes_gpc()) {
$title = addslashes($_POST['title']);
$author = addslashes($_POST['author']);
$year = addslashes($_POST['year']);
}
I didn't need the stripslashes when retrieving. I don't know why.
I continue to be in awe of all the help I get in this forum. It's such a relief to know you can get help when you are stuck. I hope there will come a day when I can be of help to someone.
$Results = $db->autoExecute("db",$_POST,DB_AUTOQUERY_INSERT);
This function takes a name => value array (like in $_POST) and makes your SQL statement for you, then does an insert. It also takes care of slashes and all that stuff that irritates a lot of developers.
Another way to do it:
$SqlStmt = $db->prepare("INSERT into db (title, author, year) Values (?,?,?)");
$Values = array($_POST['title'],$_POST['author'],$_POST['year']);
$Results = $db->execute($SqlStmt,$Values);
[pear.php.net...]
PEAR rocks!