QUERY AND UPDATE PROBLEM

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

    #1

    QUERY AND UPDATE PROBLEM

    Having a problem updating my database from a web page, through a
    submission form. Can anyone help?

    ----THIS IS MY CODE IN update.php----(user, pass, and database are
    typed in directly, I changed them here for securiy reasons :) )

    <?php

    /*---CONNECT TO DATABASE---*/

    $conn = mysql_connect(" localhost", "user", "pass") or
    die(mysql_error ());
    mysql_select_db ("database",$co nn) or die(mysql_error ());

    $event = $_GET["id"];
    $title=$_POST['title'];
    $date=$_POST['datetime'];
    $desc=$_POST['desc'];

    //---UPDATE ENTRY IN DATABASE---
    $query = "UPDATE 'calendar_event s' SET 'event_title' = '$title',
    'event_start' = '$date', 'event_shortdes c' = '$desc' WHERE 'id' =
    $event";

    //LIST ITEMS
    echo "$event $title $date $desc";

    mysql_query($qu ery);
    echo "Record Updated";
    mysql_close();

    ?>

    -----------------------THIS IS THE
    OUTPUT-------------------------------------
    TEST changed 2006-02-22 06:00:00 TESTING EDITINGRecord Updated
    --------------------------------------------------------------------------------------------
    The original information was "TEST 2006-02-22 06:00:00 TESTING EDITING"
    I changed "TEST" to "TEST changed" in the submit form on a previous
    page.

    I included the echo to see if my information was truly getting passed,
    it is. I passed the id, title, event_start, and event_shortdesc from
    the previous page where the submission form is located. Why isn't my
    database entry (row) updating? Please help.

  • Kimmo Laine

    #2
    Re: QUERY AND UPDATE PROBLEM

    "Lag" <lagoron@gmail. com> wrote in message
    news:1140671438 .716831.205800@ u72g2000cwu.goo glegroups.com.. .[color=blue]
    > Having a problem updating my database from a web page, through a
    > submission form. Can anyone help?
    >
    > ----THIS IS MY CODE IN update.php----(user, pass, and database are
    > typed in directly, I changed them here for securiy reasons :) )
    >
    > <?php
    >
    > /*---CONNECT TO DATABASE---*/
    >
    > $conn = mysql_connect(" localhost", "user", "pass") or
    > die(mysql_error ());
    > mysql_select_db ("database",$co nn) or die(mysql_error ());
    >
    > $event = $_GET["id"];
    > $title=$_POST['title'];
    > $date=$_POST['datetime'];
    > $desc=$_POST['desc'];
    >
    > //---UPDATE ENTRY IN DATABASE---
    > $query = "UPDATE 'calendar_event s' SET 'event_title' = '$title',
    > 'event_start' = '$date', 'event_shortdes c' = '$desc' WHERE 'id' =
    > $event";
    >
    > //LIST ITEMS
    > echo "$event $title $date $desc";
    >
    > mysql_query($qu ery)[/color]

    put:
    or die(mysql_error ());
    here and see what the problem is.

    Debugging is a difficult skill to learn.

    --
    "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • samudasu

      #3
      Re: QUERY AND UPDATE PROBLEM

      Don't quote the table name or field names. Only quote the variables for
      example '$title'.

      Comment

      • Marcin Dobrucki

        #4
        Re: QUERY AND UPDATE PROBLEM

        Lag wrote:[color=blue]
        > Having a problem updating my database from a web page, through a
        > submission form. Can anyone help?
        >
        > ----THIS IS MY CODE IN update.php----(user, pass, and database are
        > typed in directly, I changed them here for securiy reasons :) )
        >
        > <?php
        >
        > /*---CONNECT TO DATABASE---*/
        >
        > $conn = mysql_connect(" localhost", "user", "pass") or
        > die(mysql_error ());
        > mysql_select_db ("database",$co nn) or die(mysql_error ());
        >
        > $event = $_GET["id"];
        > $title=$_POST['title'];
        > $date=$_POST['datetime'];
        > $desc=$_POST['desc'];
        >
        > //---UPDATE ENTRY IN DATABASE---
        > $query = "UPDATE 'calendar_event s' SET 'event_title' = '$title',
        > 'event_start' = '$date', 'event_shortdes c' = '$desc' WHERE 'id' =
        > $event";
        >
        > //LIST ITEMS
        > echo "$event $title $date $desc";
        >
        > mysql_query($qu ery);
        > echo "Record Updated";
        > mysql_close();
        >
        > ?>[/color]

        require_once ("DB.php"); // PEAR::DB
        $db =& DB::connect(/*db params*/);

        $fields = array ("event_titl e" => $title,
        "event_star t" => $date,
        "event_shortdes c" => $desc);
        $res = $db->autoExecute("c alendar_events" ,$fields,
        DB_AUTOQUERY_UP DATE,
        "id = " . $event);
        if (DB::isError($r es)) die ($res->getMessage() );
        echo "Record updated";

        With a small change you can also use the autoExecute for inserts with
        just about identical code without repeating yourself.

        Comment

        • noone

          #5
          Re: QUERY AND UPDATE PROBLEM

          Lag wrote:[color=blue]
          > Having a problem updating my database from a web page, through a
          > submission form. Can anyone help?
          >
          > ----THIS IS MY CODE IN update.php----(user, pass, and database are
          > typed in directly, I changed them here for securiy reasons :) )
          >
          > <?php
          >
          > /*---CONNECT TO DATABASE---*/
          >
          > $conn = mysql_connect(" localhost", "user", "pass") or
          > die(mysql_error ());
          > mysql_select_db ("database",$co nn) or die(mysql_error ());
          >
          > $event = $_GET["id"];
          > $title=$_POST['title'];
          > $date=$_POST['datetime'];
          > $desc=$_POST['desc'];
          >
          > //---UPDATE ENTRY IN DATABASE---
          > $query = "UPDATE 'calendar_event s' SET 'event_title' = '$title',
          > 'event_start' = '$date', 'event_shortdes c' = '$desc' WHERE 'id' =
          > $event";
          >
          > //LIST ITEMS
          > echo "$event $title $date $desc";
          >
          > mysql_query($qu ery);
          > echo "Record Updated";
          > mysql_close();
          >
          > ?>
          >
          > -----------------------THIS IS THE
          > OUTPUT-------------------------------------
          > TEST changed 2006-02-22 06:00:00 TESTING EDITINGRecord Updated
          > --------------------------------------------------------------------------------------------
          > The original information was "TEST 2006-02-22 06:00:00 TESTING EDITING"
          > I changed "TEST" to "TEST changed" in the submit form on a previous
          > page.
          >
          > I included the echo to see if my information was truly getting passed,
          > it is. I passed the id, title, event_start, and event_shortdesc from
          > the previous page where the submission form is located. Why isn't my
          > database entry (row) updating? Please help.
          >[/color]

          Why are you quoting the table names. This is VERY POOR programming
          style and the database name (depending on database engine) Will
          interpret tablea and 'tablea' as two different tables. Database entity
          names are case insensitive.

          As someone that has been around databases for a very long time, do not
          quote anything but text columns in the database - and then you must in
          order for it to work :).

          Comment

          • Lag

            #6
            Re: QUERY AND UPDATE PROBLEM

            Thanks for the help......will try these things to get it to work.
            Haven't been doing this long, php mysql programming, so yeah my code is
            going to be bad and messy.......LOL .

            Comment

            • Lag

              #7
              Re: QUERY AND UPDATE PROBLEM

              I removed the quotes and the same thing still happens......th e
              variables are passed but the database doesn't update.
              Any other help you can give would be appreciated.

              CODE: $query = "UPDATE calendar_events SET event_title = $title,
              event_start = $date, event_shortdesc = $desc WHERE id = $event" or
              die(mysql_error ());

              OUTPUT: test 2006-02-23 01:00:00 testingRecord Updated.
              (original information: test 2006-02-23 01:00:00 testRecord Updated.)

              Comment

              • noone

                #8
                Re: QUERY AND UPDATE PROBLEM

                Lag wrote:[color=blue]
                > I removed the quotes and the same thing still happens......th e
                > variables are passed but the database doesn't update.
                > Any other help you can give would be appreciated.
                >
                > CODE:[/color]

                (modified for readability)

                $query = "UPDATE calendar_events SET event_title = '" . $title . "',";
                $query .= "event_star t = " . $date . ", event_shortdesc = '".$desc."'" ;
                $query .= " WHERE id = ". $event;

                text should be enclosed in single quotes.
                ($title and $desc are text fields)

                Can you give me the output of "show fields from calendar_events ;"

                You may also need to enclose the $date field....

                or
                die(mysql_error ());[color=blue]
                >
                > OUTPUT: test 2006-02-23 01:00:00 testingRecord Updated.
                > (original information: test 2006-02-23 01:00:00 testRecord Updated.)
                >[/color]

                Comment

                • samudasu

                  #9
                  Re: QUERY AND UPDATE PROBLEM

                  Like i said in my earlier post, quote the variables in the query.

                  $query = "UPDATE calendar_events SET event_title = '$title',
                  event_start = '$date', event_shortdesc = '$desc' WHERE id = '$event'";

                  This part, "or die(mysql_error ())" is NOT part of the query. It should
                  be used like this:
                  mysql_query($qu ery) or die(mysql_error ());

                  Comment

                  • Lag

                    #10
                    Re: QUERY AND UPDATE PROBLEM

                    I tried and it still passes the variables but does not update.....I
                    even tried using the root user and password, thinking I didn't update
                    the user privileges in MySQL server. But that didn't work either.
                    Thanks for the help, any other ideas?

                    $event = $_GET["id"];
                    $title=$_POST['title'];
                    $date=$_POST['datetime'];
                    $desc=$_POST['desc'];

                    //---DELETE ENTRY FROM DATABASE---
                    $query = "UPDATE calendar_events SET event_title = '$title',
                    event_start = '$date', event_shortdesc = '$desc' WHERE id = '$event'";

                    //LIST ITEMS
                    echo "$event $title $date $desc";

                    @mysql_query($q uery) or die(mysql_error ());
                    echo "Record Updated";
                    mysql_close();

                    Comment

                    • Lag

                      #11
                      Re: QUERY AND UPDATE PROBLEM

                      //---DELETE ENTRY FROM DATABASE---

                      should be

                      //---UPDATE ENTRY IN DATABASE---

                      Comment

                      • Lag

                        #12
                        Re: QUERY AND UPDATE PROBLEM

                        This is the ouput for......

                        $query = "UPDATE calendar_events SET event_title = '" . $title . "',";

                        $query .= "event_star t = " . $date . ", event_shortdesc = '".$desc."'" ;

                        $query .= " WHERE id = ". $event;


                        test2 2006-02-23 01:00:00 testYou have an error in your SQL syntax.
                        Check the manual that corresponds to your MySQL server version for the
                        right syntax to use near '01:00:00, event_shortdesc = 'test' WHERE id
                        =' at line 1

                        Comment

                        • Lag

                          #13
                          Re: QUERY AND UPDATE PROBLEM

                          Versions of software being used:

                          MySQL v3.23.49
                          PHP v4.4.1

                          Comment

                          • Lag

                            #14
                            Re: QUERY AND UPDATE PROBLEM

                            I added a count ($count) to see if or how many rows were being
                            affected....... ......even though I am tagging them by id, which auto
                            increments for each new event. I am not selecting any row, even though
                            I know the id is correct. I went into the database to make sure.

                            This is the code and the result of running the code.

                            //---UPDATE ENTRY IN DATABASE---
                            $result = mysql_query("SE LECT * FROM calendar_events WHERE
                            id='.$event'",$ conn);
                            $count = mysql_num_rows( $result);
                            $query = "UPDATE calendar_events SET event_title = '$title',
                            event_start = '$date', event_shortdesc = '$desc' WHERE id = '$result'";
                            mysql_query($qu ery) or die(mysql_error ());

                            //LIST ITEMS
                            echo "$event $title $date $desc";

                            echo "<br>Record Updated! Rows Affected: $count";
                            mysql_close();

                            ?>

                            This is the result....

                            test2 2006-02-23 01:00:00 test
                            Record Updated! Rows Affected: 0

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: QUERY AND UPDATE PROBLEM

                              Lag wrote:[color=blue]
                              > This is the ouput for......
                              >
                              > $query = "UPDATE calendar_events SET event_title = '" . $title . "',";
                              >
                              > $query .= "event_star t = " . $date . ", event_shortdesc = '".$desc."'" ;
                              >
                              > $query .= " WHERE id = ". $event;
                              >
                              >
                              > test2 2006-02-23 01:00:00 testYou have an error in your SQL syntax.
                              > Check the manual that corresponds to your MySQL server version for the
                              > right syntax to use near '01:00:00, event_shortdesc = 'test' WHERE id
                              > =' at line 1
                              >[/color]

                              It's telling you the same thing everyone else has been saying - although
                              not so clearly.

                              ALL non-numeric data MUST be within single quotes. And 01:00:00 is not
                              numeric data.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...