Results 1 to 5 of 5

Thread: [RESOLVED] deleting rows - MySQL-PHP

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    8

    Resolved [RESOLVED] deleting rows - MySQL-PHP

    Hi guys.

    Having a mysterious problem here...

    Code:
    $con = mysql_connect("localhost","username","password"); //connect to the mysql service
    if (!$con) //if there is no connection;
    {
    	die('Could not connect: ' . mysql_error()); //provide error
    }
    
    mysql_select_db("radio", $con); //select which database we want to use
    
    $rem = $_POST['remuser'];
    
    if ($rem=="")
    {
    	echo "Please select a user to remove. <a href='admin.php?sel=destroy'>Try again</a>";
    }
    else
    {
    	mysql_query("DELETE FROM 'accounts' WHERE username='$rem'"); //delete row from database
    	echo "Removed " . $rem;
    }
    See that MySQL query? Apparantly something is wrong with that. I have google'd for the past week or so looking at how everybody else on the internet removes entries from databases, and its the same as how i'm doing it (with variations, of course - most of which i've tried).

    Echoing the contents of $rem after the mysql query returns a user i have selected on a previous form to remove (drop down form, code below), which means, AFAIK, the form is working as it should.

    Code:
    $blank = "";
    $con = mysql_connect("localhost","username","password"); //connect to the mysql service
    if (!$con) //if there is no connection;
    	{
    	die('Could not connect: ' . mysql_error()); //provide error
    	}
    
    mysql_select_db("radio", $con); //select which database we want to use
    
    $query="SELECT username FROM accounts";
    
    /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
    
    $result = mysql_query ($query);
    echo "Select a user to remove:<br> <form  name='remuser' action='admin-destroy.php' method='post'> <select name='remuser'>Remove User</option>";
    // printing the list box select command
    
    while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
    if ($nt[username]=="radmin") {
    echo "<option value=$blank></option>";
    }
    else {
    echo "<option value=$nt[username]>$nt[username]</option>";
    }
    /* Option values are added by looping through the array */
    }
    echo "</select> <input type='submit' value='Remove'/>";// Closing of list box
    
    }
    Does anybody have any ideas as to why rows wont be deleted?

    php 5.2.5
    mysql 5.0.51

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: deleting rows - MySQL-PHP

    It might be the quotes around the table name accounts. Calling mysql_error(); if the query returns 0 will show you if there are any error messages.

    Also, you need to escape your variables before inserting them into your query. As it stands at the moment someone could wipe your entire table of users with an SQL string in 'remuser'. You should really be using PDO or mysqli as these support parametrized queries that escape the variables on your behalf.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    8

    Re: deleting rows - MySQL-PHP

    What the...

    I am sure that i have tried that multiple times :P
    Obviously not :P

    It worked. Thanks a bunch!

    Security Vuln: Yeah, i'm just getting the thing to work first - my policy when coding is 1) work 2) secure 3) optimise :P

    Thanks again

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] deleting rows - MySQL-PHP

    Your policy is flawed. Security should never be bolted on - it should be integrated into your code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] deleting rows - MySQL-PHP

    If you need to quote identifiers in MySQL, use backticks (`). That said, you shouldn't give your tables names that need quoting anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width