Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 49
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Delete rows from multiple tables

    i dont know why nothing i write works first time. I am trying to delete from 4 tables based on a name, for some reason it's not deleting anything. my code is below. Please someone help me.

    <?php

    // connect to the database
    include("config.php");

    // connect to the mysql server
    $link = mysql_connect($server, $db_user, $db_pass)
    or die ("Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die ("Could not select database because ".mysql_error());

    // delete the entry
    $delete = mysql_query("FROM users, properties,
    userProperties, userStats WHERE name = '".$_POST['name']."'");
    $res= mysql_query($delete);


    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,314
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    You need to use 4 seperate delete queries. AFAIK you can't name 4 seperate tables in one delete query.

    Also.. things often don't work first time in programming. Thats why its called learning. When things start to work first time then learning changes its name to debugging. In other words very few programmers write code that works perfectly first time so you're not to start be too hard on yourself ok?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    thanks, its reassuring to know its not just me.

    is there anychance you can give an idea as to how it should look?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,314
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    As said, 4 different delete queries.

    PHP Code:
    //Sanitize the $_POST name so its safe
    $Name mysql_real_escape_string($_POST['name']);

    // delete the entries
    mysql_query("FROM users WHERE name = '$Name'");
    mysql_query("FROM properties WHERE name = '$Name'");
    mysql_query("FROM userProperties WHERE name = '$Name'");
    mysql_query("FROM userStats WHERE name = '$Name'"); 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    hi, thanks for your help. unfortunately it's still not working , this is how my code looks now.

    <?php

    // connect to the database
    include("config.php");

    // connect to the mysql server
    $link = mysql_connect($server, $db_user, $db_pass)
    or die ("Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die ("Could not select database because ".mysql_error());

    //Sanitize the $_POST name so its safe
    $Name = mysql_real_escape_string($_POST['name']);

    // delete the entries
    mysql_query("FROM users WHERE name = '$name'");
    mysql_query("FROM properties WHERE name = '$name'");
    mysql_query("FROM userProperties WHERE name = '$name'");
    mysql_query("FROM userStats WHERE name = '$name'");
    $res= mysql_query($delete);


    ?>

  • #6
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by tangoforce View Post
    things often don't work first time in programming. Thats why its called learning.
    i thought it was called debugging lol

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Foster View Post
    hi, thanks for your help. unfortunately it's still not working , this is how my code looks now.
    1. Variables are case-sensitive
    2. It's `DELETE FROM ...`
    3. Nowhere is $delete defined, not sure why you're including that last line.

  • Users who have thanked kbluhm for this post:

    tangoforce (11-20-2011)

  • #8
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    ok code now looks like this and is still not working.

    <?php

    // connect to the database
    include("config.php");

    // connect to the mysql server
    $link = mysql_connect($server, $db_user, $db_pass)
    or die ("Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die ("Could not select database because ".mysql_error());

    //Sanitize the $_POST name so its safe
    $name = mysql_real_escape_string($_POST['name']);

    // delete the entries
    mysql_query("DELETE FROM users WHERE name = '$name'");
    mysql_query("DELETE FROM properties WHERE name = '$name'");
    mysql_query("DELETE FROM userProperties WHERE name = '$name'");
    mysql_query("DELETE FROM userStats WHERE name = '$name'");



    ?>
    the variables are the same case on here as in the database

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    For future reference, the case between PHP and the database makes no difference.

    I was referring to $Name and $name, which you have fixed.

    Are you seeing any errors? Are there rows in the database that should be deleted but are not? A little direction would help as opposed to just telling us it's not working.

  • #10
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    i'm not gettting any errors and it's simply not deleting anything from the tables. sorry i should have given more info

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,314
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Ok, so print out the value of mysql_error() then.

    If that reveals nothing then try selecting a row by the value of $name and see what is returned.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    not getting any error's from it, think i might have to give up. thanks for all your help though

  • #13
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    For future reference it would be a lot easier if each user had a unique ID so there wouldn't be any confusion between tables..

    Try going into your database and manually executing all of those queries within the same SQL statement delimited by ';' and see if you get any errors.

  • #14
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    ok ran it in phpmyadmin and get the following error

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql_query("DELETE FROM users WHERE name = '$name'")' at line 1

  • #15
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    ??? You cannot interpolate PHP within pure SQL queries.

    In your latest code, change the first mysql_query to exit.

    So instead of:
    PHP Code:
    mysql_query("DELETE FROM users WHERE name = '$name'"); 
    ...you will have:
    PHP Code:
    exit("DELETE FROM users WHERE name = '$name'"); 
    Then copy/paste the output here.

    I am thinking $name does not contain the value you're expecting. That has to be it, everything else looks correct.


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

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