Help with php to MySql...

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

    Help with php to MySql...

    Hello --

    I am trying to write to a a MySql database using php, and I seem to be
    hitting a wall.

    I am attaching my php file in case someone can look at it. When I run
    it, I don't get any error messages and all my echoes come through but
    nothing is written to my database. I think I am missing something
    stupid. Any help would be appreciated.

    Thanks,
    Peter

  • Marcin Dobrucki

    #2
    Re: Help with php to MySql...


    1) check that error reporting is enabled
    2) check you don't have transactions on (you will need to commit by hand)
    3) mysql_query can return a result. Capure it and analyse it
    4) enable logging on the DB, and see what it gets. This often gives
    a clue where the query breaks

    And as much as PEAR::DB can kill performance, a query like that is
    just begging to be automated via some kind of statement preparation.
    You should also pay attention to escaping quotes and such, in your fields.

    /marcin

    Peschtra wrote:[color=blue]
    > Hello --
    >
    > I am trying to write to a a MySql database using php, and I seem to be
    > hitting a wall.
    >
    > I am attaching my php file in case someone can look at it. When I run
    > it, I don't get any error messages and all my echoes come through but
    > nothing is written to my database. I think I am missing something
    > stupid. Any help would be appreciated.
    >
    > Thanks,
    > Peter
    >
    > ------------------------------------------------------------------------
    >
    > Processed
    > "; $link = mysql_connect(" db377.perfora.n et", "dbo1650864 04",
    > "jxtJdnCJ") or die ('I cannot connect to the database because: ' .
    > mysql_error()); echo "Connected
    > "; mysql_select_db (db165086404) or die('Could not select database');
    > echo "db Selected
    > "; echo "Hi
    > "; mysql_query("IN SERT INTO 2006_eval (overall,
    > overall_comment s,difficult, difficulty_comm ents, homework,
    > homework_commen ts, pace, pace_comments, grading_structu re,
    > grading_structu re_comments, grading_fairnes s, grading_fairnes s_comments,
    > tests, tests_comments, labs, labs_comments, questions,
    > questions_comme nts, approach, approach_commen ts, fav_topic, fav_act,
    > least_fav_topic , least_fav_act, extra_topic, tests_prepared, didnt_get,
    > change, again, again_comments, general_comment s, name, ip, year, math,
    > science, grade, number) VALUES ('$overall', '$overall_comme nts',
    > '$difficulty', '$difficulty_co mments', '$homework',
    > '$homework_comm ents', '$pace', '$pace_comments ', '$grading_struc ture',
    > '$grading_struc ture_comments', '$grading_fairn ess',
    > $grading_fairne ss_comments', '$tests', '$tests_comment s', '$labs',
    > '$labs_comments ', '$questions', '$questions_com ments', '$approach',
    > '$approach_comm ents', '$fav_topic', '$fav_act', '$least_fav_top ic',
    > '$least_fav_act ', '$extra_topic', '$tests_prepare d', '$didnt_get',
    > '$change', '$again', '$again_comment s', '$general_comme nts', '$name',
    > '$ip', '$year', '$math', '$science', '$grade')"); ?> Inserted[/color]

    Comment

    • strawberry

      #3
      Re: Help with php to MySql...

      take a close look at $grading_fairne ss_comments
      Peschtra wrote:[color=blue]
      > Hello --
      >
      > I am trying to write to a a MySql database using php, and I seem to be
      > hitting a wall.
      >
      > I am attaching my php file in case someone can look at it. When I run
      > it, I don't get any error messages and all my echoes come through but
      > nothing is written to my database. I think I am missing something
      > stupid. Any help would be appreciated.
      >
      > Thanks,
      > Peter
      >
      > --------------030200090501090 308020103
      > Content-Type: text/html
      > Content-Transfer-Encoding: quoted-printable
      > Content-Disposition: inline;
      > filename="eval. php"
      > X-Google-AttachSize: 3113
      >
      > <html>
      > <body>
      >
      > <?php
      >
      > $overall =3D $_POST["overall"];
      > echo $overall;
      > $overall_commen ts =3D $_POST["overall_commen ts"];
      > echo $overall_commen ts;
      > $difficulty =3D $_POST["difficulty "];
      > $difficulty_com ments =3D $_POST["difficulty_com ments"];
      > $homework =3D $_POST["homework"];
      > $homework_comme nts =3D $_POST["homework_comme nts"];
      > $pace =3D $_POST["pace"];
      > $pace_comments =3D $_POST["pace_comme nts"];
      > $grading_struct ure =3D $_POST["grading_struct ure"];
      > $grading_struct ure_comments =3D $_POST["grading_struct ure_comments"];
      > $grading_fairne ss =3D $_POST["grading_fairne ss"];
      > $grading_fairne ss_comments =3D $_POST["grading_fairne ss_comments"];
      > $tests =3D $_POSTS["tests"];
      > $tests_comments =3D $_POST["tests_comments "];
      > $labs =3D $_POST["labs"];
      > $labs_comments =3D $_POST["labs_comme nts"];
      > $questions =3D $_POST["questions"];
      > $questions_comm ents =3D $_POST["questions_comm ents"];
      > $approach =3D $_POST["approach"];
      > $approach_comme nts =3D $_POST["approach_comme nts"];
      > $fav_topic =3D $_POST["fav_topic"];
      > $fav_act =3D $_POST["fav_act"];
      > $least_fav_topi c =3D $_POST["least_fav_topi c"];
      > $least_fav_act =3D $_POST["least_fav_ act"];
      > $extra_topic =3D $_POST["extra_topi c"];
      > $tests_prepared =3D $_POST["tests_prepared "];
      > $didnt_get =3D $_POST["didnt_get"];
      > $change =3D $_POST["change"];
      > $again =3D $_POST["again"];
      > $again_comments =3D $_POST["again_comments "];
      > $general_commen ts =3D $_POST["general_commen ts"];
      > $name =3D $_POST["name"];
      > $ip =3D GetHostByName($ REMOTE_ADDR);
      > echo $ip;
      > $year =3D $_POST["year"];
      > echo $year;
      > $math =3D $_POST["math"];
      > $science =3D $_POST["science"];
      > $grade =3D $_POST["grade"];
      > echo $grade;
      >
      > echo "<br />Processed <br />";
      >
      > $link =3D mysql_connect(" db377.perfora.n et", "dbo1650864 04", "jxtJdnCJ") =
      > or die ('I cannot connect to the database because: ' . mysql_error());
      >
      > echo "Connected <br />";
      >
      > mysql_select_db (db165086404) or die('Could not select database');
      >
      > echo "db Selected <br />";
      >
      > echo "Hi <br />";
      >
      > mysql_query("IN SERT INTO 2006_eval (overall, overall_comment s,difficult, =
      > difficulty_comm ents, homework, homework_commen ts, pace, pace_comments, gr=
      > ading_structure , grading_structu re_comments, grading_fairnes s, grading_fa=
      > irness_comments , tests, tests_comments, labs, labs_comments, questions, q=
      > uestions_commen ts, approach, approach_commen ts, fav_topic, fav_act, least=
      > _fav_topic, least_fav_act, extra_topic, tests_prepared, didnt_get, change=
      > , again, again_comments, general_comment s, name, ip, year, math, science,=
      > grade, number) VALUES ('$overall', '$overall_comme nts', '$difficulty', '=
      > $difficulty_com ments', '$homework', '$homework_comm ents', '$pace', '$pace=
      > _comments', '$grading_struc ture', '$grading_struc ture_comments', '$gradin=
      > g_fairness', $grading_fairne ss_comments', '$tests', '$tests_comment s', '$=
      > labs', '$labs_comments ', '$questions', '$questions_com ments', '$approach'=
      > , '$approach_comm ents', '$fav_topic', '$fav_act', '$least_fav_top ic', '$l=
      > east_fav_act', '$extra_topic', '$tests_prepare d', '$didnt_get', '$change'=
      > , '$again', '$again_comment s', '$general_comme nts', '$name', '$ip', '$yea=
      > r', '$math', '$science', '$grade')");
      > ?>
      >
      > Inserted
      > </body>
      > </html>
      >
      > --------------030200090501090 308020103--[/color]

      Comment

      • strawberry

        #4
        Re: Help with php to MySql...

        Marcin's right, 'a query like that is just begging to be automated'
        but, in any event, writing the query something like this would make
        those kind of mistakes much easier to spot;

        $query = "INSERT INTO table
        (
        value1, //you can even add comments
        value2,
        value3
        )
        VALUES
        (
        'value1',
        'value2',
        'value3'
        );";

        mysql_query($qu ery) etc

        Comment

        • Paul Lautman

          #5
          Re: Help with php to MySql...

          Peschtra wrote:[color=blue]
          > Hello --
          >
          > I am trying to write to a a MySql database using php, and I seem to be
          > hitting a wall.
          >
          > I am attaching my php file in case someone can look at it. When I run
          > it, I don't get any error messages and all my echoes come through but
          > nothing is written to my database. I think I am missing something
          > stupid. Any help would be appreciated.
          >
          > Thanks,
          > Peter
          >
          >
          >
          >
          > Inserted[/color]

          Instead of all those assignments fron $_POST, take a look at the PHP
          extract() function.


          Comment

          • Peschtra

            #6
            Re: Help with php to MySql...

            Marcin --

            Thanks for the reply. However, I don't know how to do most of what you
            suggest. After I send this I am going to library to hopefully find a
            book as I find most of the website confusing.

            How does one enable error reporting?

            I don't know understand what your statement about transactions or
            committing by hand means. :(

            I think I capture the mysql query with $res = mysql_query (INSERT, but I
            don't know how to analyze it.

            If I don't have the DB on my computer (it is on my hosts computer) can I
            still enable logging?

            Sorry for the questions but greatful for any help from anyone.

            Peter Kupfer

            Marcin Dobrucki wrote:[color=blue]
            >
            > 1) check that error reporting is enabled
            > 2) check you don't have transactions on (you will need to commit by hand)
            > 3) mysql_query can return a result. Capure it and analyse it
            > 4) enable logging on the DB, and see what it gets. This often gives a
            > clue where the query breaks
            >
            > And as much as PEAR::DB can kill performance, a query like that is
            > just begging to be automated via some kind of statement preparation. You
            > should also pay attention to escaping quotes and such, in your fields.
            >
            > /marcin[/color]

            Comment

            • Peschtra

              #7
              Re: Help with php to MySql...

              Strawberry -

              Thanks for your help.

              I don't know what it means for something to be automated. :(

              I changed my script as you suggested and found one missing ' (where you
              suggested in your previous post), but still no luck.

              Thanks again,

              Peter

              I don't know what

              strawberry wrote:[color=blue]
              > Marcin's right, 'a query like that is just begging to be automated'
              > but, in any event, writing the query something like this would make
              > those kind of mistakes much easier to spot;
              >
              > $query = "INSERT INTO table
              > (
              > value1, //you can even add comments
              > value2,
              > value3
              > )
              > VALUES
              > (
              > 'value1',
              > 'value2',
              > 'value3'
              > );";
              >
              > mysql_query($qu ery) etc
              >[/color]

              Comment

              • strawberry

                #8
                Re: Help with php to MySql...

                OK, well try to narrow the possibilities down a bit. Add something like
                the following to the top of your page:

                <pre>
                <?php
                // Let's see what data is successfully passed to this page:

                echo "GET";
                print_r ($_GET);
                echo "POST";
                print_r ($_POST);
                ?>
                </pre>

                Then add an "or die" statement to the end of your query - something
                like this:

                $result = mysql_query($qu ery) or die ("Couldn't INSERT");

                and echo the query.

                echo $query;

                Peschtra wrote:[color=blue]
                > Strawberry -
                >
                > Thanks for your help.
                >
                > I don't know what it means for something to be automated. :(
                >
                > I changed my script as you suggested and found one missing ' (where you
                > suggested in your previous post), but still no luck.
                >
                > Thanks again,
                >
                > Peter
                >
                > I don't know what
                >
                > strawberry wrote:[color=green]
                > > Marcin's right, 'a query like that is just begging to be automated'
                > > but, in any event, writing the query something like this would make
                > > those kind of mistakes much easier to spot;
                > >
                > > $query = "INSERT INTO table
                > > (
                > > value1, //you can even add comments
                > > value2,
                > > value3
                > > )
                > > VALUES
                > > (
                > > 'value1',
                > > 'value2',
                > > 'value3'
                > > );";
                > >
                > > mysql_query($qu ery) etc
                > >[/color][/color]

                Comment

                • william.clarke@gmail.com

                  #9
                  Re: Help with php to MySql...

                  Did you have a look at the quoting strawberry suggesting in the
                  previous post?

                  '$grading_fairn ess', $grading_fairne ss_comments'

                  Notice the missing quotation mark...if that isn't all try doing a dummy
                  transaction using your MySQL front-end, by grabbing this code and
                  substituting values in or even better get php to echo the whole
                  statement on the page and cut and paste that directly into your MySQL
                  front-end, this should uncover any syntax errors etc. very quickly.


                  Peschtra wrote:[color=blue]
                  > Strawberry -
                  >
                  > Thanks for your help.
                  >
                  > I don't know what it means for something to be automated. :(
                  >
                  > I changed my script as you suggested and found one missing ' (where you
                  > suggested in your previous post), but still no luck.
                  >
                  > Thanks again,
                  >
                  > Peter
                  >
                  > I don't know what
                  >
                  > strawberry wrote:[color=green]
                  > > Marcin's right, 'a query like that is just begging to be automated'
                  > > but, in any event, writing the query something like this would make
                  > > those kind of mistakes much easier to spot;
                  > >
                  > > $query = "INSERT INTO table
                  > > (
                  > > value1, //you can even add comments
                  > > value2,
                  > > value3
                  > > )
                  > > VALUES
                  > > (
                  > > 'value1',
                  > > 'value2',
                  > > 'value3'
                  > > );";
                  > >
                  > > mysql_query($qu ery) etc
                  > >[/color][/color]

                  Comment

                  • strawberry

                    #10
                    Re: Help with php to MySql...

                    Also, "number" appears to be missing from the values. Maybe 'number' is
                    autoincremented - in which case the end of your statement should like
                    this:

                    '$science',
                    '$grade','')

                    And (less importantly) there is a name mismatch between "difficult" and
                    "difficulty ". This doesn't matter at the moment but it could cause
                    problems when you get round to learning about generating this stuff
                    automatically!

                    Comment

                    • Marcin Dobrucki

                      #11
                      Re: Help with php to MySql...



                      Peschtra wrote:
                      [color=blue]
                      > Thanks for the reply. However, I don't know how to do most of what you
                      > suggest. After I send this I am going to library to hopefully find a
                      > book as I find most of the website confusing.
                      >
                      > How does one enable error reporting?[/color]

                      You know, you could make a bit of an effort:

                      [color=blue]
                      > I don't know understand what your statement about transactions or
                      > committing by hand means. :([/color]


                      [color=blue]
                      > I think I capture the mysql query with $res = mysql_query (INSERT, but I
                      > don't know how to analyze it.[/color]



                      All you need to do is copy&paste the example to get the mysql error
                      out of your query (if such error occured)
                      [color=blue]
                      > If I don't have the DB on my computer (it is on my hosts computer) can I
                      > still enable logging?[/color]

                      No, then probably you can't unless you can arrange it with the
                      privider. You can however print the queries onto the screen, then copy
                      them onto a locally installed db, and see what it complains about.

                      /m

                      Comment

                      Working...