Page 1 of 1

Problem Delete Query In Mysql

Posted: Wed Jan 09, 2008 5:01 am
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.

Re: Problem Delete Query In Mysql

Posted: Wed Jan 09, 2008 6:45 am
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?

Posted: Wed Jan 09, 2008 6:53 am
by webspider
using mysql_error() function may help.

Posted: Wed Jan 09, 2008 6:55 am
by Mordred
1.

Code: Select all

$querys="delete from mails where userid='{$_SESSION['usr1name']}'";
2. google for SQL Injection

Posted: Wed Jan 09, 2008 7:31 am
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.

Posted: Wed Jan 09, 2008 7:36 am
by superdezign
What data is in $_SESSION['usr1name']?

Posted: Wed Jan 09, 2008 7:43 am
by zoomm
superdezign wrote:What data is in $_SESSION['usr1name']?
user email

Posted: Wed Jan 09, 2008 7:48 am
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.

Posted: Wed Jan 09, 2008 7:48 am
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);

Re: Problem Delete Query In Mysql

Posted: Fri Jan 11, 2008 2:25 am
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