www.webdeveloper.com
Results 1 to 8 of 8

Thread: deleting row from server

  1. #1
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104

    deleting row from server

    The error's in the query, but the $primary is correct I just don't get wats wrong

    Code:
    $primary = $_POST['primary'];
    
    $dbc = mysqli_connect('localhost','Marius','password','test')
    	or die('Error connecting to server');
    	
    $query = 'DELETE FROM contact_messages WHERE PRIMARY KEY = "' . $primary . '"';
    	
    $result = mysqli_query($dbc,$query)
    	or die('Error querying server');
    	
    echo $result;
    	
    mysqli_close($dbc);

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Why not have PHP/MySQL tell you what the problem is:
    PHP Code:
    $result mysqli_query($dbc,$query)
        or die(
    "Error querying server:<br />\n" mysqli_error() . "<br />$query"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    So I've interchanged PRIMARY KEY with the columns name 'index' but it just returns

    Error querying server:<br />
    <br />DELETE FROM contact_messages WHERE PRIMARY KEY = "2"

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Oops, I forgot the mysqli_error() needs the link as the argument:
    PHP Code:
    mysqli_error($dbc
    However, I'm pretty sure the problem is that you need a column name in your WHERE clause, not the keywords PRIMARY KEY.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104

    resolved

    so i'm guessing index must be some sort of keyword in SQL cuz i changed it to 'id' and it works fine now

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Probably it is a reserved word. But you could still use it if you wanted to by "quoting" it with back-ticks (a.k.a. left quotes):
    Code:
    . . . WHERE `index` = . . .
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    I have had problems in the past when deleting a row where the index was being auto incremented. It seemed the numbers got all bollixed up. I don't recall the exact problem now, but I ended up adding a column that indicated whether the row was active or not; ActiveID = 0 (inactive), 1 (active). Then added to queries ... WHERE ActiveID = 1 AND ...

  8. #8
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    Well for that I'm using AJAX, so the $primary variable is sent directly from the object.index from the database so it always matches up correctly

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles