Login | Register | FAQ
Anonymous

cant write to the DB

+ Post a reply

4 posts Page 1 of 1


cant write to the DB

by jotapay » Thu Jun 07, 2012 7:35 pm

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>
User avatar

jotapay

  • Posts: 1
  • Joined: Thu Jun 07, 2012 7:31 pm

Re: cant write to the DB

by soccerwebsitemaker » Thu Jun 07, 2012 8:40 pm

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
User avatar

soccerwebsitemaker

  • Posts: 139
  • Joined: Sat Dec 31, 2011 6:25 pm
--
Coins. Profile. Games. Chat. Fun.

CoinAwards.

Check it out at http://www.coinawards.net63.net/

Re: cant write to the DB

by soccerwebsitemaker » Thu Jun 07, 2012 8:47 pm

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.
User avatar

soccerwebsitemaker

  • Posts: 139
  • Joined: Sat Dec 31, 2011 6:25 pm
--
Coins. Profile. Games. Chat. Fun.

CoinAwards.

Check it out at http://www.coinawards.net63.net/

Re: cant write to the DB

by XainPro » Fri Jun 08, 2012 10:52 am

agreed with soccerwebsitemaker
you 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
User avatar

XainPro

  • Posts: 3706
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1