> On Sat, Jul 5, 2008 at 9:28 PM, John Black <[EMAIL PROTECTED]>
> wrote:
> 
> >   Vijayaraghavan R wrote:
> > > When I try to enter the form I never get any errors and it gets saved
> > > properly.
> >
> > Could be a database error, maybe too many connections.
> >
> > Change the line
> > echo '<p><font color="BLUE">ERROR !!! D W R NOT Saved !!! </font></p>';
> >
> > to
> > echo '<p><font color="BLUE">ERROR !!! D W R NOT Saved !!! <br
> > />'.mysql_error().'</font></p>';
> >
> > This will also output any errors from the database, if there are any.
> >
> > I would also suggest you check the Webserver and Database server's logs
> > around the time of the reported errors to see if they contain a clue.
> >
> > --
> > John


--- Vijayaraghavan R <[EMAIL PROTECTED]> wrote:

> Hi John,
> 
> As you mentioned in your mail, I had placed the "mysql_error()" function in
> the code and the
> error got trapped.
> 
> The entry is not saved when single quotes ( ' ) was used while entering any
> text.
> 
> How to avoid this?  Can it be filtered while saving it to mysql database
> (or) is there anyway to validate
> the text field while the form is entered.
> 
> Regds
> R.Vijay


When in development and testing modes I will usually have a line like this to
send a query:

$res = mysql_query($q, $db) or die("<pre>$q</pre><hr>" . mysql_error());

Before this line, $q contains the query and $db the connection to the DB.

If the query is successful, it will return TRUE and the second half of the OR
statement is not evaluated.  However, if it fails, then PHP looks to the second
half.  The die() function prints the message and stops the program.  In this
case, the message contains the SQL of the query and the error message returned
by the MySQL server.

I used PRE tags around the SQL because a long query often benefits from some
newline "\n" characters to break it up (be sure to have at least one space as
well between lines).  The mysql_error() function will tell you which line of
the query contains the first part it could not process (the error could be
lines earlier).


As to your question about single quotes, these need to be escaped.  The old way
was to use the addslashes() function to put backslashes around the single
quotes in the value.  You would run this function on each string which is to be
put in the database query.  The alternative now is mysql_real_escape_string().

You should process each user input to be sure it falls in the range of values
you expect.  Unprocessed strings can lead to SQL Injection which can hand over
your data and program to a mildly clever attacker who types odd values in your
form or URL.

This cartoon illustrates one example of SQL injection:  

  http://xkcd.com/327/

More examples and an explanation may be seen on Wikipedia:

  http://en.wikipedia.org/wiki/SQL_Injection

James
ps  Put your replies at the bottom (top-down posting) or we may not reply to
your threads. :)

Reply via email to