DELETE problem with MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gustaf Liljegren

    DELETE problem with MySQL

    I've been learning using PHP and MySQL from the tutorial at
    freewebmasterhe lp.com. Everything works fine, except deleting things.
    The script is invoked with a URL like this:



    And here's the script:

    <?
    include("dbinfo .inc.php");

    $id = $_POST['id'];
    mysql_connect($ server, $user, $password);
    $query = "DELETE FROM test1 WHERE id = '$id'";
    @mysql_select_d b($database) or die("Unable to select database.");
    mysql_query($qu ery);
    mysql_close();

    header("Locatio n: entries.php");
    ?>

    The dbinfo.inc.php file defines the variables $server, $database, $user
    and $password. The login part works.

    Gustaf
  • Noel Wood

    #2
    Re: DELETE problem with MySQL

    I'm no expert but if the code is invoked via the url then you will need to
    use the $_GET global variable to access it not the $_POST one you presently
    are using Hope this helps
    Noel.

    "Gustaf Liljegren" <gustafl@algone t.se> wrote in message
    news:cfk825$mfs $1@green.tninet .se...[color=blue]
    > I've been learning using PHP and MySQL from the tutorial at
    > freewebmasterhe lp.com. Everything works fine, except deleting things.
    > The script is invoked with a URL like this:
    >
    > http://www.example.org/test/delete_entry.php?id=35
    >
    > And here's the script:
    >
    > <?
    > include("dbinfo .inc.php");
    >
    > $id = $_POST['id'];
    > mysql_connect($ server, $user, $password);
    > $query = "DELETE FROM test1 WHERE id = '$id'";
    > @mysql_select_d b($database) or die("Unable to select database.");
    > mysql_query($qu ery);
    > mysql_close();
    >
    > header("Locatio n: entries.php");
    > ?>
    >
    > The dbinfo.inc.php file defines the variables $server, $database, $user
    > and $password. The login part works.
    >
    > Gustaf[/color]


    Comment

    • Marian Heddesheimer

      #3
      Re: DELETE problem with MySQL

      On Sat, 14 Aug 2004 07:29:13 +0200, Gustaf Liljegren wrote:
      [color=blue]
      >freewebmasterh elp.com. Everything works fine, except deleting things.[/color]

      often it helps to print out the error message from the database
      server. In your case put the line:
      echo mysql_error()

      after the line:
      mysql_query($qu ery)

      so you will get a hint what causes the problem. If that don't help,
      just print out the query line with "echo $query" to check if you pass
      the correct SQL syntax and if you put in all the information that you
      wanted to pass to mySQL.

      These two things helped me most of the time to find any error related
      to mySQL problems.

      Regards

      Marian

      --
      Tipps und Tricks zu PHP, Coaching und Projektbetreuun g
      Da ich mich seit einiger Zeit aus der Programmierung von Websites zurückgezogen habe, sind meine Tutorials und Schulungsinhalte nicht mehr länger verfügbar, da sie inzwischen veraltet sind.

      Comment

      • CJ Llewellyn

        #4
        Re: DELETE problem with MySQL

        "Gustaf Liljegren" <gustafl@algone t.se> wrote in message
        news:cfk825$mfs $1@green.tninet .se...[color=blue]
        > I've been learning using PHP and MySQL from the tutorial at
        > freewebmasterhe lp.com. Everything works fine, except deleting things.
        > The script is invoked with a URL like this:
        >
        > http://www.example.org/test/delete_entry.php?id=35
        >
        > And here's the script:
        >
        > <?
        > include("dbinfo .inc.php");
        >
        > $id = $_POST['id'];
        > mysql_connect($ server, $user, $password);
        > $query = "DELETE FROM test1 WHERE id = '$id'";
        > @mysql_select_d b($database) or die("Unable to select database.");
        > mysql_query($qu ery);
        > mysql_close();
        >
        > header("Locatio n: entries.php");
        > ?>[/color]

        If the examples are anything like this then I suggest that you look
        elsewhere for free tutorials. Only one statement in the above code attempts
        to detect and handle errors (and not very nicely at that), no wonder you are
        having trouble.

        <?php

        include("dbinfo .inc.php");

        $id = $_POST['id'];
        if($conn = mysql_connect($ server, $user, $password))
        {
        if(! mysql_select_db ($database))
        {
        echo "Unable to select the database";
        exit;
        }
        $query = "DELETE FROM test1 WHERE id = '$id'";
        $result = mysql_query($qu ery , $conn);
        if(! $result || mysql_error($co nn))
        {
        printf("Unable to delete record: %s [%s]" , mysql_error($co nn) ,
        $query);
        exit;
        }
        mysql_close();
        header("Locatio n: entries.php");
        exit;
        }
        echo "Unable to open database connection";
        ?>

        This should point out what the error is.


        Comment

        • Chris Hope

          #5
          Re: DELETE problem with MySQL

          CJ Llewellyn wrote:
          [color=blue]
          > "Gustaf Liljegren" <gustafl@algone t.se> wrote in message
          > news:cfk825$mfs $1@green.tninet .se...[color=green]
          >> I've been learning using PHP and MySQL from the tutorial at
          >> freewebmasterhe lp.com. Everything works fine, except deleting things.
          >> The script is invoked with a URL like this:
          >>
          >> http://www.example.org/test/delete_entry.php?id=35
          >>
          >> And here's the script:
          >>
          >> <?
          >> include("dbinfo .inc.php");
          >>
          >> $id = $_POST['id'];
          >> mysql_connect($ server, $user, $password);
          >> $query = "DELETE FROM test1 WHERE id = '$id'";
          >> @mysql_select_d b($database) or die("Unable to select database.");
          >> mysql_query($qu ery);
          >> mysql_close();
          >>
          >> header("Locatio n: entries.php");
          >> ?>[/color]
          >
          > If the examples are anything like this then I suggest that you look
          > elsewhere for free tutorials. Only one statement in the above code
          > attempts to detect and handle errors (and not very nicely at that), no
          > wonder you are having trouble.
          >
          > <?php
          >
          > include("dbinfo .inc.php");
          >
          > $id = $_POST['id'];
          > if($conn = mysql_connect($ server, $user, $password))
          > {
          > if(! mysql_select_db ($database))
          > {
          > echo "Unable to select the database";
          > exit;
          > }
          > $query = "DELETE FROM test1 WHERE id = '$id'";
          > $result = mysql_query($qu ery , $conn);
          > if(! $result || mysql_error($co nn))
          > {
          > printf("Unable to delete record: %s [%s]" , mysql_error($co nn)
          > ,
          > $query);
          > exit;
          > }
          > mysql_close();
          > header("Locatio n: entries.php");
          > exit;
          > }
          > echo "Unable to open database connection";
          > ?>
          >
          > This should point out what the error is.[/color]

          Also your code is subject to SQL injection. (Do a Google search if you don't
          know what that is http://www.google.com/search?q=sql+injection)

          You should cast the $id as an integer like in the following examples:
          $id = (int)$_POST['id'];
          $id = (int)$_GET['id'];
          $id = (int)$_COOKIE['id'];

          If it's a string then you need to escape quotes. This can be done by using
          the addslashes() function eg:
          $string = addslashes($_PO ST['string']);

          --
          Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

          Comment

          • Gustaf Liljegren

            #6
            Re: DELETE problem with MySQL

            Noel Wood wrote:
            [color=blue]
            > I'm no expert but if the code is invoked via the url then you will need to
            > use the $_GET global variable to access it not the $_POST one you presently
            > are using Hope this helps[/color]

            This makes sense, and it solves my problem. But I'm still a bit
            confused, because in the script for updating entries (included below),
            which is also invoked vith a URL, I'm also using POST to fetch the ID,
            and it works.

            When I use POST in the delete script, I get no error at all, not even
            with the line

            echo mysql_error();

            or all the nice error code CJ Llewellyn provided. I take it as if GET is
            right here, but can anyone explain why POST works in the update script?

            <?
            include("dbinfo .inc.php");

            $id = $_POST['id'];
            $dt_created = $_POST['dt_created'];
            $dt_updated = strftime('%Y-%m-%d %H:%M:%S', time());
            $title = $_POST['title'];
            $content = $_POST['content'];
            $status = $_POST['status'];

            mysql_connect($ server, $user, $password);
            $query = "UPDATE test1 SET dt_created = '$dt_created', dt_updated =
            '$dt_updated', title = '$title', content = '$content', status =
            '$status' WHERE id = '$id'";
            @mysql_select_d b($database) or die("Unable to select database.");
            mysql_query($qu ery);
            mysql_close();

            // Back to the entries page
            header("Locatio n: entries.php");
            ?>

            I'll make it proof to SQL injection too. Thanks.

            Gustaf

            Comment

            • Westcoast Sheri

              #7
              Re: DELETE problem with MySQL

              CJ Llewellyn wrote:
              [color=blue]
              > "Gustaf Liljegren" <gustafl@algone t.se> wrote in message
              > news:cfk825$mfs $1@green.tninet .se...[color=green]
              > > I've been learning using PHP and MySQL from the tutorial at
              > > freewebmasterhe lp.com. Everything works fine, except deleting things.
              > > The script is invoked with a URL like this:
              > >
              > > http://www.example.org/test/delete_entry.php?id=35
              > >
              > > And here's the script:
              > >
              > > <?
              > > include("dbinfo .inc.php");
              > >
              > > $id = $_POST['id'];
              > > mysql_connect($ server, $user, $password);
              > > $query = "DELETE FROM test1 WHERE id = '$id'";
              > > @mysql_select_d b($database) or die("Unable to select database.");
              > > mysql_query($qu ery);
              > > mysql_close();
              > >
              > > header("Locatio n: entries.php");
              > > ?>[/color]
              >
              > If the examples are anything like this then I suggest that you look
              > elsewhere for free tutorials. Only one statement in the above code attempts
              > to detect and handle errors (and not very nicely at that), no wonder you are
              > having trouble.
              >
              > <?php
              >
              > include("dbinfo .inc.php");
              >
              > $id = $_POST['id'];
              > if($conn = mysql_connect($ server, $user, $password))
              >[/color]

              .....um....actu ally, shouldn't you have "==" instead of "=" ???
              ;-)

              Comment

              • CJ Llewellyn

                #8
                Re: DELETE problem with MySQL

                "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                news:4134D24F.E 0674DA@nospamun 8nospam.com...[color=blue]
                > CJ Llewellyn wrote:[/color]
                -snip-[color=blue][color=green]
                > > <?php
                > >
                > > include("dbinfo .inc.php");
                > >
                > > $id = $_POST['id'];
                > > if($conn = mysql_connect($ server, $user, $password))
                > >[/color]
                >
                > ....um....actua lly, shouldn't you have "==" instead of "=" ???
                > ;-)[/color]

                if mysql_connect assigns a value to conn then the condition is true.

                ;-)

                Since $conn doesn't exist before the connect statement, it is unlikely to
                ever equal the result of mysql_connect unless the connection fails.


                Comment

                • Westcoast Sheri

                  #9
                  Re: DELETE problem with MySQL

                  CJ Llewellyn wrote:
                  [color=blue]
                  > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                  > news:4134D24F.E 0674DA@nospamun 8nospam.com...[color=green]
                  > > CJ Llewellyn wrote:[/color]
                  > -snip-[color=green][color=darkred]
                  > > > <?php
                  > > >
                  > > > include("dbinfo .inc.php");
                  > > >
                  > > > $id = $_POST['id'];
                  > > > if($conn = mysql_connect($ server, $user, $password))
                  > > >[/color]
                  > >
                  > > ....um....actua lly, shouldn't you have "==" instead of "=" ???
                  > > ;-)[/color]
                  >
                  > if mysql_connect assigns a value to conn then the condition is true.
                  >
                  > ;-)
                  >
                  > Since $conn doesn't exist before the connect statement, it is unlikely to
                  > ever equal the result of mysql_connect unless the connection fails.[/color]

                  Well...just so I wouldn't "speak to soon" I did a little test. I did this
                  with a "correct" user, server, and password, and then I did it by using an
                  incorrect password, etc.:

                  if($conn = mysql_connect($ server, $user, $password)) {
                  echo "yes";
                  }

                  It echoed "yes" both times.

                  ;-)


                  Comment

                  • CJ Llewellyn

                    #10
                    Re: DELETE problem with MySQL

                    "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                    news:4134E24F.3 D8C2546@nospamu n8nospam.com...[color=blue]
                    > CJ Llewellyn wrote:
                    >[color=green]
                    > > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                    > > news:4134D24F.E 0674DA@nospamun 8nospam.com...[color=darkred]
                    > > > CJ Llewellyn wrote:[/color]
                    > > -snip-[color=darkred]
                    > > > > <?php
                    > > > >
                    > > > > include("dbinfo .inc.php");
                    > > > >
                    > > > > $id = $_POST['id'];
                    > > > > if($conn = mysql_connect($ server, $user, $password))
                    > > > >
                    > > >
                    > > > ....um....actua lly, shouldn't you have "==" instead of "=" ???
                    > > > ;-)[/color]
                    > >
                    > > if mysql_connect assigns a value to conn then the condition is true.
                    > >
                    > > ;-)
                    > >
                    > > Since $conn doesn't exist before the connect statement, it is unlikely[/color][/color]
                    to[color=blue][color=green]
                    > > ever equal the result of mysql_connect unless the connection fails.[/color]
                    >
                    > Well...just so I wouldn't "speak to soon" I did a little test. I did this
                    > with a "correct" user, server, and password, and then I did it by using an
                    > incorrect password, etc.:
                    >
                    > if($conn = mysql_connect($ server, $user, $password)) {
                    > echo "yes";
                    > }
                    >
                    > It echoed "yes" both times.[/color]

                    DOH! Just goes to show you should always test before posting :-)



                    Comment

                    • Westcoast Sheri

                      #11
                      Re: DELETE problem with MySQL

                      CJ Llewellyn wrote:
                      [color=blue]
                      > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                      > news:4134E24F.3 D8C2546@nospamu n8nospam.com...[color=green]
                      > > CJ Llewellyn wrote:
                      > >[color=darkred]
                      > > > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                      > > > news:4134D24F.E 0674DA@nospamun 8nospam.com...
                      > > > > CJ Llewellyn wrote:
                      > > > -snip-
                      > > > > > <?php
                      > > > > >
                      > > > > > include("dbinfo .inc.php");
                      > > > > >
                      > > > > > $id = $_POST['id'];
                      > > > > > if($conn = mysql_connect($ server, $user, $password))
                      > > > > >
                      > > > >
                      > > > > ....um....actua lly, shouldn't you have "==" instead of "=" ???
                      > > > > ;-)
                      > > >
                      > > > if mysql_connect assigns a value to conn then the condition is true.
                      > > >
                      > > > ;-)
                      > > >
                      > > > Since $conn doesn't exist before the connect statement, it is unlikely[/color][/color]
                      > to[color=green][color=darkred]
                      > > > ever equal the result of mysql_connect unless the connection fails.[/color]
                      > >
                      > > Well...just so I wouldn't "speak to soon" I did a little test. I did this
                      > > with a "correct" user, server, and password, and then I did it by using an
                      > > incorrect password, etc.:
                      > >
                      > > if($conn = mysql_connect($ server, $user, $password)) {
                      > > echo "yes";
                      > > }
                      > >
                      > > It echoed "yes" both times.[/color]
                      >
                      > DOH! Just goes to show you should always test before posting :-)[/color]

                      Okay....now if you could help on one tiny little problem: I *don't* want php to
                      display error messages to my browser. I don't have access to server compile
                      files, so in the past simply placing "error_reportin g(E_NONE);" at the top of
                      my script worked. Now it doesn't work. What am I doing wrong? Or does that
                      directive work on some servers but not others???


                      Comment

                      • Jasper Bryant-Greene

                        #12
                        Re: DELETE problem with MySQL

                        Westcoast Sheri wrote:
                        [color=blue]
                        > Okay....now if you could help on one tiny little problem: I *don't* want php to
                        > display error messages to my browser. I don't have access to server compile
                        > files, so in the past simply placing "error_reportin g(E_NONE);" at the top of
                        > my script worked. Now it doesn't work. What am I doing wrong? Or does that
                        > directive work on some servers but not others???[/color]

                        Try:

                        ini_set('displa y_errors','0');

                        at the start of your php script.

                        --
                        Jasper Bryant-Greene
                        Cabbage Promotions

                        Comment

                        • CJ Llewellyn

                          #13
                          Re: DELETE problem with MySQL

                          "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                          news:41355414.D 272B7CB@nospamu n8nospam.com...[color=blue]
                          > CJ Llewellyn wrote:
                          >[color=green]
                          > > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                          > > news:4134E24F.3 D8C2546@nospamu n8nospam.com...[color=darkred]
                          > > > CJ Llewellyn wrote:
                          > > >
                          > > > > "Westcoast Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
                          > > > > news:4134D24F.E 0674DA@nospamun 8nospam.com...
                          > > > > > CJ Llewellyn wrote:
                          > > > > -snip-
                          > > > > > > <?php
                          > > > > > >
                          > > > > > > include("dbinfo .inc.php");
                          > > > > > >
                          > > > > > > $id = $_POST['id'];
                          > > > > > > if($conn = mysql_connect($ server, $user, $password))
                          > > > > > >
                          > > > > >
                          > > > > > ....um....actua lly, shouldn't you have "==" instead of "=" ???
                          > > > > > ;-)
                          > > > >
                          > > > > if mysql_connect assigns a value to conn then the condition is true.
                          > > > >
                          > > > > ;-)
                          > > > >
                          > > > > Since $conn doesn't exist before the connect statement, it is[/color][/color][/color]
                          unlikely[color=blue][color=green]
                          > > to[color=darkred]
                          > > > > ever equal the result of mysql_connect unless the connection fails.
                          > > >
                          > > > Well...just so I wouldn't "speak to soon" I did a little test. I did[/color][/color][/color]
                          this[color=blue][color=green][color=darkred]
                          > > > with a "correct" user, server, and password, and then I did it by[/color][/color][/color]
                          using an[color=blue][color=green][color=darkred]
                          > > > incorrect password, etc.:
                          > > >
                          > > > if($conn = mysql_connect($ server, $user, $password)) {
                          > > > echo "yes";
                          > > > }
                          > > >
                          > > > It echoed "yes" both times.[/color]
                          > >
                          > > DOH! Just goes to show you should always test before posting :-)[/color]
                          >
                          > Okay....now if you could help on one tiny little problem: I *don't* want[/color]
                          php to[color=blue]
                          > display error messages to my browser. I don't have access to server[/color]
                          compile[color=blue]
                          > files, so in the past simply placing "error_reportin g(E_NONE);" at the top[/color]
                          of[color=blue]
                          > my script worked. Now it doesn't work. What am I doing wrong? Or does that
                          > directive work on some servers but not others???
                          >
                          >[/color]


                          Comment

                          Working...