soc86 Posted September 12, 2011 Share Posted September 12, 2011 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? Quote Link to comment Share on other sites More sharing options...
dougjohnson Posted September 12, 2011 Share Posted September 12, 2011 I think you need to add: $result = mysql_query($sql); after the INSERT. Quote Link to comment Share on other sites More sharing options...
soc86 Posted September 12, 2011 Author Share Posted September 12, 2011 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{ ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 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 { ?> Quote Link to comment Share on other sites More sharing options...
soc86 Posted September 12, 2011 Author Share Posted September 12, 2011 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 { ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 What kind of problems? Did your computer explode? Quote Link to comment Share on other sites More sharing options...
soc86 Posted September 12, 2011 Author Share Posted September 12, 2011 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? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 Well if you're going to use that code, obviously if it didn't work before it isn't going to work now. Did you try replacing it with the code I posted above to see what errors are returned? Quote Link to comment Share on other sites More sharing options...
soc86 Posted September 12, 2011 Author Share Posted September 12, 2011 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> Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 Remove the comma after content in: INSERT INTO blogdata (title, category, content,) Remove the space after title in: VAULE('$title ', '$category', '$content') while you're at it too. If you really want a space added after the title you should do so when assigning the $title. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 What do you see wrong with VAULE? Maybe it should be VALUES instead? Quote Link to comment Share on other sites More sharing options...
soc86 Posted September 12, 2011 Author Share Posted September 12, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 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. EDIT: I'm also pretty sure you'll need to add a space after VALUES. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 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. 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 . . . Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 12, 2011 Share Posted September 12, 2011 Yeah, I was thinking in swapping mysql keywords for the word ERROR just for fun. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.