PHP won't post to database

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

    PHP won't post to database

    Hi Guys,

    I really am a novice and found a script on webmonkey. Basically all I want
    to do is to add records to my database. In my database I have a table called
    research_main with the fields already created.

    I have tried to bodge a script to work for me. it doesn't. It looks as
    though it works but I get no data posted. What have I done wrong?

    If you want me to put a statement in anywhere to check something please let
    me know where you want me to put it as I really am useless with this.

    can you tell me what is wrong with this script.

    :-( I am trying honest

    <html>

    <body>

    <?php

    if ($submit) {

    // process form

    $db = mysql_connect(" localhost", "uname", "password") ;

    mysql_select_db ("dbtouse",$db) ;

    $sql = "INSERT INTO research_main
    (date_inv,info, medium_name,pla ce_name,date_de ath,verified_ev ent,closed_even t
    ) VALUES
    ('$date_inv','$ info','$medium_ name','$place_n ame','$date_dea th','$verified_ e
    vent','$closed_ event')";

    $result = mysql_query($sq l);

    echo "Thank you! Information entered.\n";

    } else{



    // display form



    ?>



    <form method="post" action="<?php echo $PHP_SELF?>">

    <p>Date Of Event:
    <input name="date_inv" type="Text" id="date_inv">
    <br>

    Information to be researched:
    <textarea name="info" cols="75" rows="5" id="info"></textarea>
    <br>

    Medium or Researchers name: :
    <input name="medium_na me" type="Text" id="medium_name " maxlength="30">
    <br>

    Place Name: :
    <input name="place_nam e" type="Text" id="place_name " maxlength="50">
    <br>
    Date of Investigation:
    <input name="date_deat h" type="text" id="date_death" >
    <br>
    Verified Event:
    <input name="verified_ event" type="radio" value="radiobut ton">
    Yes
    <input name="verified_ event" type="radio" value="radiobut ton" checked>
    No<br>
    Event Closed:
    <input name="closed_ev ent" type="radio" value="radiobut ton">
    Yes
    <input name="closed_ev ent" type="radio" value="radiobut ton" checked>
    No<br>

    <input type="Submit" name="submit" value="Enter information">
    </p>
    </form>
    <?php



    } // end if



    ?>



    </body>



    </html>



  • Mike Discenza

    #2
    Re: PHP won't post to database

    Dan Weeb wrote:
    [color=blue]
    > Hi Guys,
    >
    > I really am a novice and found a script on webmonkey. Basically all I want
    > to do is to add records to my database. In my database I have a table
    > called research_main with the fields already created.
    >
    > I have tried to bodge a script to work for me. it doesn't. It looks as
    > though it works but I get no data posted. What have I done wrong?
    >
    > If you want me to put a statement in anywhere to check something please
    > let me know where you want me to put it as I really am useless with this.
    >
    > can you tell me what is wrong with this script.
    >
    > :-( I am trying honest
    >
    > <html>
    >
    > <body>
    >
    > <?php
    >
    > if ($submit) {
    >
    > // process form
    >
    > $db = mysql_connect(" localhost", "uname", "password") ;
    >
    > mysql_select_db ("dbtouse",$db) ;
    >
    > $sql = "INSERT INTO research_main
    >[/color]
    (date_inv,info, medium_name,pla ce_name,date_de ath,verified_ev ent,closed_even t[color=blue]
    > ) VALUES
    >[/color]
    ('$date_inv','$ info','$medium_ name','$place_n ame','$date_dea th','$verified_ e[color=blue]
    > vent','$closed_ event')";
    >
    > $result = mysql_query($sq l);
    >
    > echo "Thank you! Information entered.\n";
    >
    > } else{
    >
    >
    >
    > // display form
    >
    >
    >
    > ?>
    >
    >
    >
    > <form method="post" action="<?php echo $PHP_SELF?>">
    >
    > <p>Date Of Event:
    > <input name="date_inv" type="Text" id="date_inv">
    > <br>
    >
    > Information to be researched:
    > <textarea name="info" cols="75" rows="5" id="info"></textarea>
    > <br>
    >
    > Medium or Researchers name: :
    > <input name="medium_na me" type="Text" id="medium_name " maxlength="30">
    > <br>
    >
    > Place Name: :
    > <input name="place_nam e" type="Text" id="place_name " maxlength="50">
    > <br>
    > Date of Investigation:
    > <input name="date_deat h" type="text" id="date_death" >
    > <br>
    > Verified Event:
    > <input name="verified_ event" type="radio" value="radiobut ton">
    > Yes
    > <input name="verified_ event" type="radio" value="radiobut ton" checked>
    > No<br>
    > Event Closed:
    > <input name="closed_ev ent" type="radio" value="radiobut ton">
    > Yes
    > <input name="closed_ev ent" type="radio" value="radiobut ton" checked>
    > No<br>
    >
    > <input type="Submit" name="submit" value="Enter information">
    > </p>
    > </form>
    > <?php
    >
    >
    >
    > } // end if
    >
    >
    >
    > ?>
    >
    >
    >
    > </body>
    >
    >
    >
    > </html>[/color]


    Stick this in after your query is executed:

    if(!$result)
    {
    $errMsg = "<div align=center>\n ".
    "<font size=6 color=red>Error !</font><br>\n".
    "An error was detected in your request<br>\n".
    "SQL used = '<u>$sql</u>'\n".
    "<br>\n".
    mysql_errno($db ) . ": " .
    mysql_error($db ) . "\n";
    exit($errMsg);
    }

    If your insert is blowing up this will show you want MySQL is puking on.

    Comment

    • Pedro Graca

      #3
      Re: PHP won't post to database

      Dan Weeb wrote:[color=blue]
      > If you want me to put a statement in anywhere to check something please let
      > me know where you want me to put it as I really am useless with this.[/color]

      Insert/replace the statements I marked with ###

      [color=blue]
      ><html>
      >
      ><body>
      >
      ><?php[/color]

      ### insert this one
      echo 'register_globa ls is ', (ini_get('regis ter_globals'))? ('On'):('Off');
      ###
      [color=blue]
      > if ($submit) {
      >
      > // process form
      >
      > $db = mysql_connect(" localhost", "uname", "password") ;[/color]

      ### replace the above with
      $db = mysql_connect(" localhost", "uname", "password") or die('No Connection!');
      ###
      [color=blue]
      > mysql_select_db ("dbtouse",$db) ;[/color]

      ### replace the above with
      mysql_select_db ("dbtouse",$ db) or die('No Database!');
      ###
      [color=blue]
      > $sql = "INSERT INTO research_main
      > (date_inv,info, medium_name,pla ce_name,date_de ath,verified_ev ent,closed_even t
      > ) VALUES
      > ('$date_inv','$ info','$medium_ name','$place_n ame','$date_dea th','$verified_ e
      > vent','$closed_ event')";
      >
      > $result = mysql_query($sq l);[/color]

      ### replace the above with
      $result = mysql_query($sq l) or die('No Query!');
      ###
      [color=blue]
      > echo "Thank you! Information entered.\n";[/color]
      (snip)

      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Eric Bohlman

        #4
        Re: PHP won't post to database

        "Dan Weeb" <danielweeb@hot SPAMLESSmail.co m> wrote in
        news:c0r4t5$8cb $1$8302bc10@new s.demon.co.uk:
        [color=blue]
        > I really am a novice and found a script on webmonkey. Basically all I
        > want to do is to add records to my database. In my database I have a
        > table called research_main with the fields already created.
        >
        > I have tried to bodge a script to work for me. it doesn't. It looks as
        > though it works but I get no data posted. What have I done wrong?
        >
        > If you want me to put a statement in anywhere to check something
        > please let me know where you want me to put it as I really am useless
        > with this.
        >
        > can you tell me what is wrong with this script.
        >
        > :-( I am trying honest
        >
        > <html>
        >
        > <body>
        >
        > <?php
        >
        > if ($submit) {[/color]

        There's your problem. Your script assumes that register_global s is set,
        which it isn't by default in recent versions of PHP. Since your form is
        using the POST method, you need to replace each reference to a variable
        that's supposed to come from the form with $_POST['<variable>'], where
        <variable> should be replaced with the name (without the $) of the
        variable. For example, in the above statement you'd replace $submit with
        $_POST['submit'].
        [color=blue]
        >
        > // process form
        >
        > $db = mysql_connect(" localhost", "uname", "password") ;
        >
        > mysql_select_db ("dbtouse",$db) ;
        >
        > $sql = "INSERT INTO research_main
        > (date_inv,info, medium_name,pla ce_name,date_de ath,verified_ev ent,closed_
        > event ) VALUES
        > ('$date_inv','$ info','$medium_ name','$place_n ame','$date_dea th','$verif
        > ied_e vent','$closed_ event')";[/color]

        That's fine for learning, but in real life you'll want to escape the
        various form parameters so the user can't do mischief like sending an info
        value consisting of "');DROP TABLE research_main;" .
        [color=blue]
        >
        > $result = mysql_query($sq l);[/color]

        And in real life you'll want to check $result to make sure the insert
        succeded.

        Comment

        • Joshua Gosse

          #5
          Re: PHP won't post to database

          Did you try changing the username and password parts of the connect to what
          the username and password is?


          Comment

          • Geoff Berrow

            #6
            Re: PHP won't post to database

            I noticed that Message-ID:
            <Xns9491963CB43 E7ebohlmanomsde vcom@130.133.1. 4> from Eric Bohlman
            contained the following:
            [color=blue]
            >That's fine for learning, but in real life you'll want to escape the
            >various form parameters so the user can't do mischief like sending an info
            >value consisting of "');DROP TABLE research_main;" .[/color]

            Always thought MySql would not run multiple queries.

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Dan Weeb

              #7
              Re: PHP won't post to database

              [color=blue]
              > $result = mysql_query($sq l) or die('No Query!');[/color]

              OK Thanks It was a faulty query.

              How do I make sue that my php fields match my database? I want the dates
              and the radio buttons to put correct info into the database fields

              For example in the database

              date_event is type date
              medium_name is type varchar
              info_research is type blob
              place_name is type varchar
              date_invest is type date
              date_death is type date
              verified_event type char(3)
              closed_event type char(3)

              In the php script I have the fields setup as

              date_event is type text <------------ Needs to be a date in format
              dd/mm/yyyy
              medium_name is type text
              info_research is type textarea
              place_name is type text
              date_invest is type text <------------ Needs to be a date in format
              dd/mm/yyyy
              date_death is type text <------------ Needs to be a date in format
              dd/mm/yyyy
              verified_event radiobutton
              closed_event type radiobutton

              Currently if I enter date of 21/12/2004 it displays as 2112-20-04

              Thanks so much

              Paul



              Comment

              • Pedro Graca

                #8
                Re: PHP won't post to database

                Dan Weeb wrote:[color=blue]
                >[color=green]
                >> $result = mysql_query($sq l) or die('No Query!');[/color]
                >
                > OK Thanks It was a faulty query.
                >
                > How do I make sue that my php fields match my database? I want the dates
                > and the radio buttons to put correct info into the database fields[/color]

                You have to reformat them.
                [color=blue]
                > Currently if I enter date of 21/12/2004 it displays as 2112-20-04[/color]

                Read how MySQL deals with DATE columns:



                MySQL does not care about anything other than digits in dates.

                You *have* to insert them in the format YYYY-MM-DD (or similar ... the
                year has to be first, then the month and then the day).


                So you have to transform '21/12/2004' to '2004-12-21'.

                Here's a fast and dirty way (no error-checking, no different format
                allowed, no fancy stuff):
                <?php
                function dmy2ymd($dmy) {
                return substr($dmy, 6, 4) . '-' . substr($dmy, 3, 2) . '-' . substr($dmy, 0, 2);
                }
                ?>


                Or you could do as I do and insert DATEs as if they were numbers (no
                need to mess around with double and single quotes):

                $sql = "insert into table (datecol) values (20041221)";

                .... you still need to reformat though :-)
                --
                --= my mail box only accepts =--
                --= Content-Type: text/plain =--
                --= Size below 10001 bytes =--

                Comment

                • Phil Roberts

                  #9
                  Re: PHP won't post to database

                  With total disregard for any kind of safety measures Geoff Berrow
                  <blthecat@ckdog .co.uk> leapt forth and uttered:
                  [color=blue]
                  > Always thought MySql would not run multiple queries.
                  >[/color]

                  MySQL will, the mysql_query() API function will not.

                  --
                  Phil Roberts | Nobody In Particular | http://www.flatnet.net/

                  Comment

                  • Carlos Marangon

                    #10
                    Re: PHP wont post to database

                    Hello!

                    If you are trying to run the program in LOCALHOST
                    you must left password blank.

                    Try this one for $db variable:

                    $db = mysql_connect(" localhost", "root", "");

                    I dont know if is this your problem,
                    I hope this helps.

                    []

                    CM

                    Mike Discenza <discenza@sbcgl obal.net> wrote in message news:<j79Yb.114 5$gL4.649410725 @newssvr11.news .prodigy.com>.. .[color=blue]
                    > Dan Weeb wrote:
                    >[color=green]
                    > > Hi Guys,
                    > >
                    > > I really am a novice and found a script on webmonkey. Basically all I want
                    > > to do is to add records to my database. In my database I have a table
                    > > called research_main with the fields already created.
                    > >
                    > > I have tried to bodge a script to work for me. it doesn't. It looks as
                    > > though it works but I get no data posted. What have I done wrong?
                    > >
                    > > If you want me to put a statement in anywhere to check something please
                    > > let me know where you want me to put it as I really am useless with this.
                    > >
                    > > can you tell me what is wrong with this script.
                    > >
                    > > :-( I am trying honest
                    > >
                    > > <html>
                    > >
                    > > <body>
                    > >
                    > > <?php
                    > >
                    > > if ($submit) {
                    > >
                    > > // process form
                    > >
                    > > $db = mysql_connect(" localhost", "uname", "password") ;
                    > >
                    > > mysql_select_db ("dbtouse",$db) ;
                    > >
                    > > $sql = "INSERT INTO research_main
                    > >[/color]
                    > (date_inv,info, medium_name,pla ce_name,date_de ath,verified_ev ent,closed_even t[color=green]
                    > > ) VALUES
                    > >[/color]
                    > ('$date_inv','$ info','$medium_ name','$place_n ame','$date_dea th','$verified_ e[color=green]
                    > > vent','$closed_ event')";
                    > >
                    > > $result = mysql_query($sq l);
                    > >
                    > > echo "Thank you! Information entered.\n";
                    > >
                    > > } else{
                    > >
                    > >
                    > >
                    > > // display form
                    > >
                    > >
                    > >
                    > > ?>
                    > >
                    > >
                    > >
                    > > <form method="post" action="<?php echo $PHP_SELF?>">
                    > >
                    > > <p>Date Of Event:
                    > > <input name="date_inv" type="Text" id="date_inv">
                    > > <br>
                    > >
                    > > Information to be researched:
                    > > <textarea name="info" cols="75" rows="5" id="info"></textarea>
                    > > <br>
                    > >
                    > > Medium or Researchers name: :
                    > > <input name="medium_na me" type="Text" id="medium_name " maxlength="30">
                    > > <br>
                    > >
                    > > Place Name: :
                    > > <input name="place_nam e" type="Text" id="place_name " maxlength="50">
                    > > <br>
                    > > Date of Investigation:
                    > > <input name="date_deat h" type="text" id="date_death" >
                    > > <br>
                    > > Verified Event:
                    > > <input name="verified_ event" type="radio" value="radiobut ton">
                    > > Yes
                    > > <input name="verified_ event" type="radio" value="radiobut ton" checked>
                    > > No<br>
                    > > Event Closed:
                    > > <input name="closed_ev ent" type="radio" value="radiobut ton">
                    > > Yes
                    > > <input name="closed_ev ent" type="radio" value="radiobut ton" checked>
                    > > No<br>
                    > >
                    > > <input type="Submit" name="submit" value="Enter information">
                    > > </p>
                    > > </form>
                    > > <?php
                    > >
                    > >
                    > >
                    > > } // end if
                    > >
                    > >
                    > >
                    > > ?>
                    > >
                    > >
                    > >
                    > > </body>
                    > >
                    > >
                    > >
                    > > </html>[/color]
                    >
                    >
                    > Stick this in after your query is executed:
                    >
                    > if(!$result)
                    > {
                    > $errMsg = "<div align=center>\n ".
                    > "<font size=6 color=red>Error !</font><br>\n".
                    > "An error was detected in your request<br>\n".
                    > "SQL used = '<u>$sql</u>'\n".
                    > "<br>\n".
                    > mysql_errno($db ) . ": " .
                    > mysql_error($db ) . "\n";
                    > exit($errMsg);
                    > }
                    >
                    > If your insert is blowing up this will show you want MySQL is puking on.[/color]

                    Comment

                    • Tony Marston

                      #11
                      Re: PHP won't post to database


                      "Dan Weeb" <danielweeb@hot SPAMLESSmail.co m> wrote in message
                      news:c0rm0v$813 $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                      >[color=green]
                      > > $result = mysql_query($sq l) or die('No Query!');[/color]
                      >
                      > OK Thanks It was a faulty query.
                      >
                      > How do I make sue that my php fields match my database? I want the dates
                      > and the radio buttons to put correct info into the database fields[/color]

                      Take a look at http://www.tonymarston.co.uk/php-mysql/dateclass.html for a
                      class which both validates and formats dates.

                      --
                      Tony Marston
                      This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL

                      [color=blue]
                      > For example in the database
                      >
                      > date_event is type date
                      > medium_name is type varchar
                      > info_research is type blob
                      > place_name is type varchar
                      > date_invest is type date
                      > date_death is type date
                      > verified_event type char(3)
                      > closed_event type char(3)
                      >
                      > In the php script I have the fields setup as
                      >
                      > date_event is type text <------------ Needs to be a date in format
                      > dd/mm/yyyy
                      > medium_name is type text
                      > info_research is type textarea
                      > place_name is type text
                      > date_invest is type text <------------ Needs to be a date in format
                      > dd/mm/yyyy
                      > date_death is type text <------------ Needs to be a date in format
                      > dd/mm/yyyy
                      > verified_event radiobutton
                      > closed_event type radiobutton
                      >
                      > Currently if I enter date of 21/12/2004 it displays as 2112-20-04
                      >
                      > Thanks so much
                      >
                      > Paul
                      >
                      >
                      >[/color]


                      Comment

                      Working...