Jump to content

inserting Data into MySQL help


seephp

Recommended Posts

I am writing A simple variatino of a blogging engine and it I keep getting the error:

 

Could not select the database because: Column count doesn't match value count at row 1The query was INSERT INTO blog_entries (blogid, title, entry, date_entered) VALUES (0, 'I\'m trying this again!' 'This time I fixed a simple HTML problem!', NOW()).

 

The code is:

 

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Creat A sticky note</title>
</head>
<body>
<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) {
if ($dbc = mysql_connect
('localhost', 'root', 'vertrigo'))
{		

		if (!@mysql_select_db ('mysticky'))
		{	
			die ('<p>Could not select the database because: <b>' .
			mysql_error() . '</b></p>');
		}

} else {
	die ('<p>Could not select the database because: <b>' .
			mysql_error() . '</b></p>');

}

$query = "INSERT INTO blog_entries
(blog_id, title, entry, date_entered)
VALUES (0, '{$_POST['title']}'
'{$_POST['entry']}', NOW())";

		if (mysql_query ($query)) {
			print '<p>Your StickyWeb Sticky has been stuck.</p>';

		} else {
			print "<p>Could not select the database because: <b>" .
			mysql_error() . "</b>The query was $query.</p>";

		}

		mysql_close();
}
?>
<form action="addNote.php" method="post">
<p>Create your Websticky Sticky note!</p>
<p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p>
<p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

Link to comment
Share on other sites

Now I am getting this error:

 

Could not select the database because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'Trying this AGAIN!' ' at line 2The query was INSERT INTO blog_entries ('blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'Trying this AGAIN!' 'I hope this works!', NOW()).

 

I had changed the code to:

 

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Creat A sticky note</title>
</head>
<body>
<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) {
if ($dbc = mysql_connect
('localhost', 'root', 'vertrigo'))
{		

		if (!@mysql_select_db ('mysticky'))
		{	
			die ('<p>Could not select the database because: <b>' .
			mysql_error() . '</b></p>');
		}

} else {
	die ('<p>Could not select the database because: <b>' .
			mysql_error() . '</b></p>');

}

$query = "INSERT INTO blog_entries
('blog_id', 'title', 'entry', 'date_entered')
VALUES (0, '{$_POST['title']}'
'{$_POST['entry']}', NOW())";

		if (mysql_query ($query)) {
			print '<p>Your StickyWeb Sticky has been stuck.</p>';

		} else {
			print "<p>Could not select the database because: <b>" .
			mysql_error() . "</b>The query was $query.</p>";

		}

		mysql_close();
}
?>
<form action="addNote.php" method="post">
<p>Create your Websticky Sticky note!</p>
<p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p>
<p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

 

 

Link to comment
Share on other sites

As said previously, you forgot a comma. You have:

"INSERT INTO blog_entries

(blog_id, title, entry, date_entered)

VALUES (0, '{$_POST['title']}'

'{$_POST['entry']}', NOW())";

Change to :

"INSERT INTO blog_entries

(blog_id, title, entry, date_entered)

VALUES (0, '{$_POST['title']}',<--------note the added comma!

'{$_POST['entry']}', NOW())";

Link to comment
Share on other sites

I added the comma but I'm still getting:

 

Could not select the database because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'I reall hope this wor' at line 2The query was INSERT INTO blog_entries ('blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'I reall hope this works this time!', 'If this works I can begin working on my project for Master Alley!', NOW()).

 

 

Link to comment
Share on other sites

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Creat A sticky note</title>
</head>
<body>
<?php

//php.ini settings.

ini_set ('display_errors', 1);

//php.ini error settings.

error_reporting (E_ALL & ~E_NOTICE);

// if the form of title and entry are blank

if(($title=="none")||($entry=="none")){

// tell user to fill in all the form

echo "please fill in all the form cheers";

}
//database

$db=mysql_connecy("localhost","username","password");
mysql_select_db("databse",$db) or die (mysql_error());


//protect database

$blog_id=addslases($_POST['blog_id']);
$title=addslashes($_POST['title']);
$entry=addslashes($_POST['entry']);
$date_entered=addslashes($_POST['date_entered']);

//date for date entred.

$date_entered=date("d-m-y");


//if the user post an entry.

if (isset($_POST['submit'])) {

$query = "INSERT INTO blog_entries(blog_id,title,entry,date_entered)
VALUES('$blog_id', '$title', '$entry', '$date_entered')";

$result=mysql_query($query) or die(mysql_error());

// 1st message if correct.

echo '<p>Your StickyWeb Sticky has been stuck.</p>';

}else{

// 2nd message not correct.

echo "<p>Could not select the database because:";


}

?>
<form action="addNote.php" method="post">
<p>Create your Websticky Sticky note!</p>
<p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p>
<p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

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.