I am trying to insert data into a mysql database by a html textarea. However, when testing the textarea and if I use single quotes within the textarea the data doesn’t get put into the database, something to do with the variables? I have heard that you should use JavaScript to validate the form or something. I would rather just use PHP if that is possible. I have googled it, scanned through some books and searched these forums to no success. Any help would be greatly appreciated. Thanks
if(isset($_POST['submit']))
{
// Putting data from form into variables to be manipulated
$title = $_POST['title'];
$article = $_POST['article'];
// Getting the form variables and then placing their values into the MySQL table
mysql_query("INSERT INTO blog (title, article) VALUE('$title', '$article')");
}
if(isset($_POST['submit']))
{
// Putting data from form into variables to be manipulated
$title = $_POST['title'];
$article = $_POST['article'];
// Getting the form variables and then placing their values into the MySQL table
mysql_query("INSERT INTO blog (title, article) VALUE('$title', '$article')");
}
Try again:
if(isset($_POST['submit']))
{
// Putting data from form into variables to be manipulated
$title = $_POST['title'];
$article = $_POST['article'];
$conn = mysql_connect("localhost","root","root") or die ("Can't connect");
mysql_select_db("your_db",$conn);
// Getting the form variables and then placing their values into the MySQL table
mysql_query("INSERT INTO blog (title, article) VALUES ('".$title."', '".$article."')");
}
You fix “VALUE” -> “VALUES”
And check connect to database.
Host: localhost
Username: root
Pass: root
Database: your_db
if(isset($_POST['submit']))
{
// Putting data from form into variables to be manipulated
$title = $_POST['title'];
$article = $_POST['article'];
$conn = mysql_connect("localhost","root","root") or die ("Can't connect");
mysql_select_db("your_db",$conn);
// Getting the form variables and then placing their values into the MySQL table
mysql_query("INSERT INTO blog (title, article) VALUES ('".mysql_real_escape_string($title)."', '".mysql_real_escape_string($article)."')");
}
Also, you should check to see if any errors have occurred during the execution of the query. Look at mysql_error.
Last tip: investigate PDO. It’s the defacto way of accessing databases from PHP, and offers a lot more security and features than the old mysql_* functions you’re currently using. It’ll take a while to learn it though.
Cheers, problem sorted! Now just need to work on editing posts.
I have come across PDO before from the book I was reading before called PHP solutions. The author wrote code for PDO and MySQLi, but i only worked from the MySQLi part. So i take it you would recommend PDO over MySQLi then? I would use one of them for this project i have been given but the university require us to use MySQL only.