PHP not deleting data from database

General discussions related to php

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Mar 21, 2013 4:32 am

PHP not deleting data from database

Postby bullbreed » Thu Mar 21, 2013 4:38 am

Hello you clever people.

I have a bit if an issue. I'm new to PHP and have been building a CMS.

The following code is working fine to delete data from the database

Code: Select all

if (isset($_GET['p'])){
           $namecheck = mysql_query("SELECT `name` FROM `store` WHERE `name` = '".$_GET["p"]." '");
          $count = mysql_num_rows($namecheck);
           if ($count !== 0){
                if ($_GET['sure'] === '1'){
                        mysql_query("DELETE FROM `store` WHERE `name` = '".$_GET["p"]."' LIMIT 1");
                   echo '<font size="2" color="#ff0000">Are you sure you want to delete this retailer? <a href="deleteretailer.php?p='.$_GET['p'].'&sure=1">Yes</a> or <a href="deleteretailer.php">No</a></font>';

However it seems if there is a & character in the data such as in a company name of A & B Engineering the data will not delete and I don't get the warning message as displayed in the PHP above "are you sure you want to ..."

Does the & sign cause issues?

php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: PHP not deleting data from database

Postby seandisanti » Thu Mar 21, 2013 11:38 am

in mysql, the ampersand (&) is the operator for a bitwise 'and' comparison. 'A & B' translates to 0. instead of using mysql functions, you should use PDO. the syntax is similar enough that you'll catch on quickly and pdo functions are much faster and more secure. a good example of use can be seen at ... o-examples . it's a 12 minute video that goes over some basics and advantages, including using prepared statements. Whether you switch to pdo or not, you should also not delete based on a field that is not a primary key, as it creates the possibility for error. You should have a findByName() if you like that returns a record or id, and then delete based only on the id or a unique field. then you won't have to limit results, or risk your data if someone re-uses a name. quoting your fields (using PDO::quote() or mysqli::real_escape_string) should avoid the current issue you're having though.

php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP not deleting data from database

Postby johnj » Thu Mar 21, 2013 9:35 pm

Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest