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 »

MySQL DISTINCT to Remove Duplicate Listing

How to select and display records from MySQL table using the DISTINCT keyword for unique data.
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 »

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 »

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 »

PHPRunner

PHPRunner builds visually appealing web interface for any local or remote MySQL database. Your web site visitors will be able to easily search, add, edit, delete and export data in MySQL database. Advanced security options allow to build password-protecte
Continue Reading »

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

How to search multiple fields in a database
Continue Reading »

PHP Search Between Values in MySQL Database

Need to know how to filter a database search result between two values. Use the WHERE clause in SQL to provides conditions in your search.
Continue Reading »

Page 1 of 212