Jump to content

Recommended Posts

Hello,  :D

 

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  :shy:

 

 

 

 

 

 

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.

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.

 

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

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

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.