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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    46
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Delete multiple tables not working

    Hi,

    I'm trying to delete a record from 2 tables off an ID. I have tried all the JOIN functions but when I use those they end up deleteing everything from both tables. Below is the code. Can anyone help please as I can't get my head round this at all!

    Code:
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    if ((isset($_GET['id'])) && ($_GET['id'] != "")) {
      $deleteSQL = sprintf("DELETE menu_item, dow FROM menu_item, dow WHERE id=%s",
                           GetSQLValueString($_GET['id'], "int"));
    
      mysql_select_db($database_design_online_connection, $design_online_connection);
      $Result1 = mysql_query($deleteSQL, $design_online_connection) or die(mysql_error());
    
      $deleteGoTo = "index.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
        $deleteGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $deleteGoTo));
    }
    ?>
    If anyone could help that would be awesome

    Thanks alot
    Joe

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,208
    Thanks
    23
    Thanked 605 Times in 604 Posts
    I don't think you can delete rows from two tables at the same time (one query).
    You need to use a separate query for each table.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post
    There is no other way rather then using joins.
    Well...,below is the query which i have used before in one of my application and has worked fine for me. you can try out with the below example query:

    DELETE FROM boards, topics, messages
    USING boards INNER JOIN topics INNER JOIN messages
    WHERE boards.boardid = $boardid
    AND topics.boardid = boards.boardid
    AND messages.boardid = boards.boardid;

    Hope this helps.

  • Users who have thanked priyankagound for this post:

    sunfighter (10-23-2013)

  • #4
    New Coder
    Join Date
    Mar 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot for all your replies

    The solution priyankagound gave me worked excellent

    Thanks alot


  •  

    Posting Permissions

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