cant write to the DB
Page 1 of 1
cant write to the DB
so i have a very simple form, which should update a table i have created on phpmyadmin. the table has 3 columns, ID (set to auto-increment), title, and content. However, when I enter text into the form and hit submit, it appears to work, but the table isnt updated! please help:<head>
<title>
Add a Post
</title>
<?php
if (isset($_POST['title']) && isset($_POST['post'])) {
mysql_connect('localhost', 'root', '');
mysql_select_db('phpacademy');
$title = $_POST['title'];
$content = $_POST['post'];
"INSERT INTO `posts` (`title`,`content`) VALUES ('$title','$content')";
echo "entry added!";
}
?>
</head>
<body>
<div id="content">
<form method="post" action="">
Title:<br />
<input type="text": name="title" id="title"> <br />
Post:<br />
<textarea name="post" id="post" cols="20"rows="10"></textarea ><br />
<input type="submit" value="post" >
</form>
Re: cant write to the DB
You may be right about this, but I think you have to put action="destinationpage" (or whatever page you want) and then put the top paragraph of code in that page.But maybe that's not the error, I'm not sure
Re: cant write to the DB
No, wait I figured out the problem.You have to put mysql_query for the INSERT INTO string.
My advice would be to put
"INSERT INTO `posts` (`title`,`content`) VALUES ('$title','$content')";
into a string-variable. Right now, that text is just a random string floating around, it's not getting executed!
Do this:
- Code: Select all
$SQLinsert = "INSERT INTO `posts` (`title`,`content`) VALUES ('$title','$content')";
mysql_query($SQLinsert);
This will execute your function and update the database.
Re: cant write to the DB
agreed with soccerwebsitemakeryou missed one important step which is query refer to the following steps
1.Connect to server
2.Select the Database
3.Make Query
4.Execute Query
5.Verify Query
use mysql_query("insert into .........");
for proper learning you can read following tutorial
http://html.net/tutorials/php/lesson19.php
Page 1 of 1