maat Posted December 27, 2009 Share Posted December 27, 2009 Hello, I'm new to php, and I'm trying to write an application for a blog. I'm keeping it very simple--just trying to get a basic framework that works, which I will go back and tweak later. I've read several books, web tutorials, and have researched the posts on this forum for solutions, which have helped me to some extent. However, right now I am stuck. I have already successfully written a login.php file for this blog, which, after logging in, directs to the file I am now working on: blog_entry.php. Here's the code: <?php ini_set("display_errors", "1"); error_reporting(E_ALL); require_once('db_login.php'); include('blog_entry.inc'); $conn = mysqli_connect($db_host, $db_username, $db_password, $db_database) or die ('Error connecting to MySQL'); if(isset($_POST['btnSign'])) { $title = trim($_POST['txtTitle']); $body = trim($_POST['txtBody']); $sql = "INSERT INTO posts (post_id, title, body) VALUES ('', '$title', '$body')"; $result = mysql_query($sql) or die('Error, query failed'); } ?> Like I said, very simple, and I will build on it once I get it to work. The error message I am getting is "Error, query failed." I suspect this might have something to do with mysql and mysqli. Here are the db_login and blog_entry.inc files: db_login: <?php $db_host='localhost'; $db_database='xxxx'; $db_username='root'; $db_password='xxxxxxx'; ?> and blog_entry.inc: <html> <head> <title>Login</title> </head> <body> <form method="post" name="blogform"> <table width="550" border="0" cellpadding="2" cellspacing="1"> <tr> <td width="100">Title *</td> <td> <input name="txtTitle" type="text" size="30" maxlength="30"></td> </tr> <tr> <td width="100">Body *</td> <td> <textarea name="txtBody" cols="80" rows="5"></textarea></td> </tr> <tr> <td width="100"> </td> <td> <input name="btnSign" type="submit" value="Add Post"></td> </tr> </table> </form> </body> </html> I'd really appreciate any help you can offer me on this! Thanks, Maat Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/ Share on other sites More sharing options...
premiso Posted December 27, 2009 Share Posted December 27, 2009 Add some better error reporting. Also try not to use or die, instead use or trigger_error as errors can be turned off for production: <?php ini_set("display_errors", "1"); error_reporting(E_ALL); require_once('db_login.php'); include('blog_entry.inc'); $conn = mysqli_connect($db_host, $db_username, $db_password, $db_database) or trigger_error('Error connecting to MySQL: ' . mysql_error()); if(isset($_POST['btnSign'])) { $title = trim($_POST['txtTitle']); $body = trim($_POST['txtBody']); $sql = "INSERT INTO posts (post_id, title, body) VALUES ('', '$title', '$body')"; $result = mysql_query($sql) or trigger_error('Error, query failed: ' . mysql_error()); } ?> Give that a shot and see what the error is, note the use of mysql_error which will give you a more indepth error message. Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/#findComment-984365 Share on other sites More sharing options...
laffin Posted December 27, 2009 Share Posted December 27, 2009 udually, id's are primary key with autoincrement on looking at the query I see $sql = "INSERT INTO posts (post_id, title, body) VALUES ('', '$title', '$body')"; if your not setting the id, than take it out $sql = "INSERT INTO posts (title, body) VALUES ('$title', '$body')"; Another problem I see, is that yer not escapting title or body $title = trim($_POST['txtTitle']); $body = trim($_POST['txtBody']); try something like $title = mysql_real_escape_string(trim($_POST['txtTitle'])); $body = mysql_real_escape_string(trim($_POST['txtBody'])); I would throw in some validation to see if title & body do have some form of content as well. Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/#findComment-984379 Share on other sites More sharing options...
maat Posted December 27, 2009 Author Share Posted December 27, 2009 Thank you, thank you, thank you! I am off to try your reccomendations, and I will definitely let you know how it works out, Maat Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/#findComment-984604 Share on other sites More sharing options...
maat Posted December 28, 2009 Author Share Posted December 28, 2009 Yes, I got it to work! I had to convert some things from mysql to mysqli, and that was it. Does anyone know if there is some resource that basically "translates" between mysql and mysqli? Like a side-by-side display that shows what a function is in mysql and its equivalent in mysqli. You see, I have another script attached to this one that is in mysqli, and I know there was a line in my php.ini file that I switched to reflect the mysqli approach. So while this blog_entry script now works, it could cause some future problems. Thanks again, Maat Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/#findComment-984948 Share on other sites More sharing options...
ignace Posted December 28, 2009 Share Posted December 28, 2009 You could aswell get rid of MySQL and MySQLi by using something like PDO or ODBC which allows you to use any kind of database through a common API. Meaning your code never changes while your database engine used can be switched from MySQL to PostGreSQL to SQLite to ... Quote Link to comment https://forums.phpfreaks.com/topic/186405-please-help-with-a-roadblock-on-a-blog-script/#findComment-984985 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.