How to show error code from mySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mayajewe
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 12:56 pm

How to show error code from mySQL

Post by mayajewe »

Hi there,

Background:
I have never used php before (I am learning Java at uni) I am trying to creating a wcms system by working through a great tutorial I found.

I have a php page which collects data from a form and INSERT INTO my mySQL table. It works when I enter numeric data into the sectionid field, but fails if I add text to it. The field is set to type="text" in the html form and the mySQL database is set to VARCHAR. I don't know why it won't save text. The code from the tutorial is only has basic error handling so the only error I get is "Sorry, there was an error saving to the database".

I would be grateful if anyone could let me know how to get the actual error code so I can understand why is isn't writing to my database. Also if anyone has any ideas what I am doing wrong...?

Thanks,

Here is the code:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');
 
// Check whether a form has been submitted. If so, carry on
if ($HTTP_POST_VARS){
 
// Create an instance of DbConnector
$connector = new DbConnector();
 
    // **  Start of add content code:  **
 
    // Create an SQL query (MySQL version)
    $insertQuery = "INSERT INTO pagecontent(pagename,sectiontitle,sectionid,thecontent) VALUES (".
    "'".$HTTP_POST_VARS['pagename']."', ".
    "'".$HTTP_POST_VARS['sectiontitle']."', ".
    $HTTP_POST_VARS['sectionid'].", ".
    "'".$HTTP_POST_VARS['thecontent']."')";
 
    // Save the form data into the database 
    if ($result = $connector->query($insertQuery)){
 
    // It worked, give confirmation
    echo '<center><b>Page content added to the database</b></center><br>';
 
    }else{
 
    // It hasn't worked so stop. Better error handling code would be good here!
    exit('<center>Sorry, there was an error saving to the database</center>');
 
    }
    // **  End of add content code.  ** 
}
?>
<body>
<form name="form1" method="post" action="newArticle.php">
        <p>&nbsp;Page Name:
          <input name="pagename" type="text" id="pagename">
        </p>
        <p>&nbsp;Section Title:
          <input name="sectiontitle" type="text" id="sectiontitle">
        </p>
        <p>&nbsp;Section ID:
          <input name="sectionid" type="text" id="sectionid">
        </p>
        <p>&nbsp;Page Content:
          <textarea name="thecontent" cols="50" rows="6" id="thecontent"></textarea>
        </p>
        <p align="center">
          <input type="submit" name="Submit" value="Submit">
        </p>
</form>
</body>
</html>
 



Thanks
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: How to show error code from mySQL

Post by Randwulf »

To find the error code, do an:

or die ("Error: " . mysql_error());

I'll take a look at your code, but I'm notoriously bad at spotting SQL errors.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to show error code from mySQL

Post by John Cartwright »

Look carefully at the single quotes around the other columns. Numeric data does not need quotes, but strings do.

Also, you shouldn't be using $HTTP_POST_VARS (unless your using a really old version of PHP) because it is deprecated. Instead, replace it with $_POST.
mayajewe
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 12:56 pm

Re: How to show error code from mySQL

Post by mayajewe »

Hi again,

It is now working thank you both. :D

Randwulf - The error code worked great I am sure I will be using that one a lot so thank you.

Jcart - I have changed the $HTTP_POST_VARS to $_POST, I guess the article I am reading must be old so I will keep a look out. Thank you for pointing out the quotes, the original code had this field set to a number and I just changed the field name and the database type. I didn't realise that the line was different.

Just in case anyone else like me wants to know what finally worked, here is the corrected bit of code:

Code: Select all

 
    // Create an SQL query (MySQL version)
    $insertQuery = "INSERT INTO pagecontent(pagename,sectiontitle,sectionid,thecontent) VALUES (".
    "'".$_POST['pagename']."', ".
    "'".$_POST['sectiontitle']."', ".
    "'".$_POST['sectionid']."', ".
    "'".$_POST['thecontent']."')";
 
Post Reply