Jump to content

Why is my PHP not updating my database?


soc86

Recommended Posts

I have a PHP that when i enter data and click post, I want it to add the info to my data base, but for some reason this is not happening?

 

My PHP code is:

 

 

<?php
mysql_connect ("localhost", "root", "");
mysql_select_db("blog");
?>
<html>
<head>
<title>Admin - Post new entry</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$category = $_POST['category'];
$content = $_POST['content'];

$sql = mysql_query("INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')");
echo "Data has been posted, click <a href='index.php'?here</a> to see it";
}else{
?>
<form action='admin.php' method='post'>
Title: <input type='text' name='title' /><br />
Category: <input type='text' name='category' /><br />
Content: <textarea name='content'></textarea><br />
<input type='submit' name='submit' value='post!' />
<?php
}
?>
</form>
</body>
</html>

 

And my database:

          id title                                           content                  category

1 Check 1                                Welcome to my site           Post 1

2 Check 2                            Is it working                      Post 2

 

 

 

Hope this helps, please can someone tell me why its not updating?

Link to comment
Share on other sites

Sorry, i don't think I get where this should be added? I have done the following, but still not working:

 

 

<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$category = $_POST['category'];
$content = $_POST['content'];

$sql = mysql_query("INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')");
$result = mysql_query($sql);
echo "Data has been posted, click <a href='index.php'?here</a> to see it";
}else{
?>

Link to comment
Share on other sites

No, the query is already being executed so adding that will just introduce another problem. One thing you need to do is get out of the habit of nesting the query string in the call to mysql_query(). Assign the query string to a variable, then pass the variable to mysql_query() so when something goes wrong you can echo the query string along with any errors.

 

<?php
if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
$title = $_POST['title'];
$category  = $_POST['category'];
$content = $_POST['content'];

$query = "INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')";
if( !$result = mysql_query($query) ) {
	echo "<br>Query: $query<br>Produced error: " . mysql_error();
}
} else {
?>

Link to comment
Share on other sites

Still having problems??

 

 

<?php
if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
$title = $_POST['title'];
$category  = $_POST['category'];
$content = $_POST['content'];

$query = "INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')";
if( !$result = mysql_query($query) ) {
	echo "<br>Query: $query<br>Produced error: " . mysql_error();
}
} else {
?>

Link to comment
Share on other sites

When my code is has it was originally, i.e.

 

 

<?php
mysql_connect ("localhost", "root", "");
mysql_select_db("blog");
?>
<html>
<head>
<title>Admin - Post new entry</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$category = $_POST['category'];
$content = $_POST['content'];

$sql = mysql_query("INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')");
echo "Data has been posted, click <a href='index.php'?here</a> to see it";
}else{
?>
<form action='admin.php' method='post'>
Title: <input type='text' name='title' /><br />
Category: <input type='text' name='category' /><br />
Content: <textarea name='content'></textarea><br />
<input type='submit' name='submit' value='post!' />
<?php
}
?>
</form>
</body>
</html>

 

 

The post is accepted, i get a message displaying:

 

Data has been posted, click <="" a=""> to see it

 

When i click to view my blog there is no update?

Link to comment
Share on other sites

The error message is:

 

Query: INSERT INTO blogdata (title, category, content,) VAULE('rewerw ', 'erwrr', 'rewrwer')

Produced error: 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 ') VAULE('rewerw ', 'erwrr', 'rewrwer')' at line 1

 

I have entered the code as:

 

 

<?php
mysql_connect ("localhost", "root", "");
mysql_select_db("blog");
?>
<html>
<head>
<title>Admin - Post new entry</title>
</head>
<body>
<?php

if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
$title = $_POST['title'];
$category  = $_POST['category'];
$content = $_POST['content'];

$query = "INSERT INTO blogdata (title, category, content,) VAULE('$title ', '$category', '$content')";
if( !$result = mysql_query($query) ) {
	echo "<br>Query: $query<br>Produced error: " . mysql_error();
}
} else {
?>
<form action='admin.php' method='post'>
Title: <input type='text' name='title' /><br />
Category: <input type='text' name='category' /><br />
Content: <textarea name='content'></textarea><br />
<input type='submit' name='submit' value='post!' />
<?php
}
?>
</form>
</body>
</html>

Link to comment
Share on other sites

Still a problem after removing the comma, help?

 

Error message:

 

Query: INSERT INTO blogdata (title, category, content) VAULE('rewerw ', 'erwrr', 'rewrwer')

Produced error: 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 'VAULE('rewerw ', 'erwrr', 'rewrwer')' at line 1

Link to comment
Share on other sites

What do you see wrong with VAULE? Maybe it should be VALUES instead?

Methinks he copied and pasted your code which you copied and pasted from his original code.  :P

 

Yup, and I did that intentionally. I'd rather show people how to find and fix the errors themselves than fix the same thing over and over and over and over . . .

Link to comment
Share on other sites

Yup, and I did that intentionally. I'd rather show people how to find and fix the errors themselves than fix the same thing over and over and over and over . . .

 

I figured that much. Those sort of errors are so obvious to experienced coders.

 

 

Give a man a fish vs. teaching him how to fish.

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.