Fix SQL INSERT problems with PHP addslashes function

If you’ve created you own custom content management system using PHP, you may have noticed problems inserting data. One of the problems could be that your data may have characters that prevent it from being inserted. Some of the character that would cause this type of problem are single or double quotes, backslash, and NUL characters.

You can escape these problematic characters using the PHP function addslashes(). As explained by the PHP manual: It returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (), double quote (), backslash (\) and NUL (the NULL byte).

As you can see from my example below, I first run the content through the addshashes function and than return it to the same named string “$Page_Content” before passing it to my SQL UPDATE or INSERT statement.

$Page_Content = addslashes($Page_Content);
$query = "UPDATE site_content SET Page_Title='$Page_Title', Page_Content='$Page_Content' WHERE id_f='$id_f'";
query_db($query);

How to backup MySQL Database via SSH

First you log in using your terminal application by typing the flowing SSH command:

ssh yourusername@hostname-or-ipaddress

After that, you will be prompted to enter your password. As you enter your password, the text will be hidden. Next we should switch to the “root” user. You can do so by typing the following in your terminal window:

su – root

You will have to enter a password once more. Now navagate to where you would like to store your MySQL database file. For an example of changing the directory to the “public_html” direcotry, type this:

cd /var/www/html_public

Now you can back up your db here using the following command. This command will backup all of your databases. I also chose to compress them after the pipe “|” as a gzip file. Type the following but be sure to change the user name and password to your info:

mysqldump -u yourusername -p yourpassword –all-databases | gzip >databasebackup.sql.gz

You can also target single database using the following:

mysqldump -u yourUserName -p yourDBName | gzip >databasebackup.sql.gz

Select UNIQUE or DISTINCT MySQL/PHP Queries

How to filter / remove duplicate items in your database query result using DISTINCT

Continue Reading »

PHP MySQL Order by Two Columns

Here’s how to order your data by two or more columns using PHP and MySQL Continue Reading »

MySQL Table Fields Data Types

Learn MySQL data types like Numeric Types, Date and Time Types and String Types for building dynamic web sites.
Continue Reading »

Create MySQL Database With PHP

Create a dynamic web site by using MySQL Database and PHP. It is easy and I provided the code to cut and paste. Continue Reading »

PHP Order By Multiple Fields in MySQL

Want to be able to order by two or more database fields / columns using the ASC and DESC order clause
Continue Reading »

PHP Search using WHERE, AND, LIKE, OR Query Functions

How to search multiple fields in a database
Continue Reading »

Polls and Comments with PHP and MySQL

Create a poll voting and comments system with the help of PHP and MySQL. Explains in details the script and database design.
Continue Reading »

Installing PHP MySQL Apache on windows 98 in 5 min

This article on “Installing Apache-MySQL-PHP on Windows 98″ in just five min is a complete step by step tutorial to install this trio package. The article contains full graphics snapshots to help the new developers to install the trio package without any Continue Reading »