Problem Delete Query In 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
zoomm
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2008 4:48 am

Problem Delete Query In Mysql

Post by zoomm »

pickle | Please use [php], [code] and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

In script below the insert query work fine, the delete have problem, thanks for any help...

Code: Select all

<?
$link = mysql_connect($_HOST, $_USER, $_PASSWORD);      
mysql_select_db($_DBNAME);
if(isset($_POST['send']) && $_POST['send']!='' && $_SESSION['usr1name']!='') 
{
    
    $query = "SELECT email FROM  users where id=".$_SESSION['usr1name'];
    $result= mysql_query($query);
    $lins = mysql_fetch_array($result, MYSQL_ASSOC);
    $querys="delete from mails where userid=".$_SESSION['usr1name'];
    $results= mysql_query($querys);
    for($i=0;$i<=(int)$_POST['valuescount'];$i++)
    {
        if($_POST['categ'][$i]!='')
        {
            $query="insert into mails values(".$_POST['categ'][$i].",'".$lins['email']."',0)";
            $result= mysql_query($query);
        }
    }
}
?>

<form name="newsletter" method="post" action="newsletters.php">
                <?
            $getsubcats=array();
            $getsubcats[0]='0';
            if(isset($_POST['send2']) && $_POST['send2']!='') 
            {
                $k=0;
                for($i=0;$i<=(int)$_POST['valuescount'];$i++)
                {
                    if($_POST['categ'][$i]!='')    
                    {
                        $getsubcats[$k]=$_POST['categ'][$i];
                        $k++;
                    }
                }//end for
            }    $j=0;//echo "<br>catslcatslalla===>".$getsubcats[0];
                for($p=0;$getsubcats[$p]!='';$p++)
                {//echo "catslcatslalla";
                    $query = "SELECT * FROM catesendry where pid=".$getsubcats[$p];
                    echo "<tr><td colspan=\"2\"><b>".getCatesendryName($getsubcats[$p])."</b></td></tr>";
                    $result= mysql_query($query);
                    //echo mysql_error();
                    $t=0;             
                    while ($lin = mysql_fetch_array($result, MYSQL_ASSOC)) 
                    {
                        $id=$lin['id'];
                        $cats=hasMail($id);
                        echo"<tr>";
                        echo"    <td width=\"50%\">
                                        <input type=\"checkbox\" name=\"categ[]\" value=\"".$lin['id']."\" ";
                            if($cats!=0)
                                echo "checked";
                        echo"    /> ".$lin['name'];
                            if($cats!=0)
                                echo "($cats)";
                        echo"    </td>";
                        $j++;
                        if($lin = mysql_fetch_array($result, MYSQL_ASSOC))
                        {
                            $id=$lin['id'];
                        $cats=hasMail($id);
                        echo"    <td width=\"50%\">
                                        <input type=\"checkbox\" name=\"categ[]\" value=\"".$lin['id']."\" ";
                            if($cats!=0)
                                echo "checked";
                        echo"             /> ".$lin['name'];
                            if($cats!=0)
                                echo "($cats)";
                        echo"    </td>";
                        $j++;        
                        }
                        else 
                        {
                            echo"    <td width=\"50%\">
                                </td>";
                        }    echo"</tr>";
                        $t++;
                    }//end while
                    if($t==0)
                        echo "<tr><td colspan=\"2\">??? ???????? ????????????? ???? ????????? ????</td></tr>";
                }    
?>

<input type="hidden"  name="valuescount" value="<? echo $j; ?>" />
<input type="submit"  name="send" value="save" />
<input type="submit" name="send2" value="next"/>   

</form>
<?
}
?>

pickle | Please use [php], [code] and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Problem Delete Query In Mysql

Post by superdezign »

zoomm wrote:

Code: Select all

$querys="delete from mails where userid=".$_SESSION['usr1name'];
$results= mysql_query($querys);
Does the table `mails` exist?
Does it have a column `userid`?
Is the `userid` value in $_SESSION['usr1name']?
Do you know that DELETE queries don't return results?
Why are you deleting anything?
Why didn't you use

Code: Select all

tags in order to post your code?
Why haven't you described your problem to us at all?
.. What does this do?
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

using mysql_error() function may help.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

1.

Code: Select all

$querys="delete from mails where userid='{$_SESSION['usr1name']}'";
2. google for SQL Injection
zoomm
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2008 4:48 am

Post by zoomm »

am beginner and I will try as long as better I can to you explain and all English I know. What makes script of is the following: the user selects certain categories and they are registered in the table mails, that is to say when he selects he is stored and when you do not select nothing him he erases. I thank for the comprehension. If you need more information him I will send all.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

What data is in $_SESSION['usr1name']?
zoomm
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2008 4:48 am

Post by zoomm »

superdezign wrote:What data is in $_SESSION['usr1name']?
user email
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Then take Mordred's advice. You need to place quotes around the variable in the query, and you should definitely look into SQL injection and sanitizing the data.
zoomm
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2008 4:48 am

Post by zoomm »

zoomm wrote:
superdezign wrote:What data is in $_SESSION['usr1name']?
user email

CREATE TABLE `mails` (
`categoryid` int(11) NOT NULL default '0',
`userid` varchar(255) NOT NULL default '0',
`html` int(11) NOT NULL default '0',
PRIMARY KEY (`categoryid`,`userid`)
) ENGINE=MyISAM;

--
-- 'Αδειασμα δεδομένων του πίνακα `mails`
--

INSERT INTO `mails` VALUES (1650, 'test@test.gr', 0);
INSERT INTO `mails` VALUES (1691, 'stest@test.gr', 0);
zoomm
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2008 4:48 am

Re: Problem Delete Query In Mysql

Post by zoomm »

do not know that also what it should I make but if somebody wishes you help me it helps, on payment of course and for a future collaboration. Sorry for my English. Thanks
Post Reply