Database table not populating properly by php script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sshade25
    New Member
    • Nov 2009
    • 10

    Database table not populating properly by php script

    I am trying to insert some data into my postgresql database table using an html form and a php script. The problem here is that when the script is run, it does not insert data into the last two coulmns and also it would insert a 0 into the third column.

    but when i run the query directly on the database using this sql

    INSERT INTO crops (crop_id,crop_t ype,crop_name,c ultivation_yrs, local_name)VALU ES('6','food crop','garri',' 6','utara');

    it inserts the data very well meaning that something is wrong with my php script

    Please someone should help to tell me what im missing out in my scripts here.

    script 1 is my html form script.

    Code:
    <html>
     <head><title>Crop form</title>
     </head>
     <body>
     <p>
     <form action="inputcrops.php" method="post">
     <table width="600" cellpadding="10" cellspacing="0" border="2">
     <tr align="center" valign="top">
     <td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
     
      Input crop_id: <input type="text" name="crop_id"><br>
     Input crop_type: <input type="text" name="crop_type"><br>
     Input crop_name: <input type="text" crop_name="crop_name"><br>
     Input cultivation_yrs: <input type="text" cultivation_yrs="cultivation_yrs"><br>
     Input local_name: <input type="text" local_name="local_name"><br>
     <input type="Submit" name="submit" value="Submit">
     
    </form></P>
     </body>
     </html>
    script two is my php script
    Code:
    <html>
        <body>
    <?php
     $PGHOST = localhost;
     $PGDATABASE = "SantaRosaDB";
     $PGUSER = "****";
     $PGPASSWORD = "****";
     $PGPORT = 5432;
     $db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER
     password=$PGPASSWORD");
     if ($db_handle) {
     echo 'Connection attempt succeeded.';
     } else {
     echo 'Connection attempt failed.';
     }
     $crop_id = intval( $_POST['crop_id']);  
     $crop_type = pg_escape_string($_POST['crop_type']);
     $crop_name = pg_escape_string($_POST['crop_name']);
     $cultivation_yrs = intval( $_POST['cultivation_yrs']);
     $local_name = pg_escape_string($_POST['local_name']);
     $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)VALUES($crop_id,$crop_type,$crop_name,$cultivation_yrs,$local_name)";
    
    // INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)VALUES('6','food crop','garri','6','utara'); 
     
    $result = pg_query($query);
     if (!$result) {
     $errormessage = pg_last_error();
     echo "Error with query: " . $errormessage;
     exit();
     }
     printf ("These values were inserted into the database - %s %s %s %s %s",$crop_id,$crop_type,$crop_name,$cultivation_yrs,$local_name);
     pg_close();
    ?>
      </body>
     </html>
    Here is the response i get when i run the code

    Connection attempt succeeded.These values were inserted into the database - 1 food crop 0
    Last edited by Dormilich; Nov 27 '09, 11:03 AM. Reason: protecting your password
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You need to wrap quotes around string values
    Code:
    $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)
    VALUES($crop_id,'$crop_type','$crop_name',$cultivation_yrs,'$local_name')";
    It doesn't seen to do any harm wrapping quotes around numerical values also

    Comment

    • sshade25
      New Member
      • Nov 2009
      • 10

      #3
      I have tried to enclose the string values in quites but it is giving me this error

      Code:
      $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)VALUES('$crop_id', '"$crop_type"', '"$crop_name"','$cultivation_yrs','"$local_name"')";
       $result = pg_query($query);
      Parse error: syntax error, unexpected T_VARIABLE in C:\ms4w\Apache\ htdocs\php\inpu tcrops.php on line 21

      when i changed it to this i still get the same error

      Code:
      $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)VALUES('$crop_id', "$crop_type", "$crop_name",'$cultivation_yrs',"$local_name")";
       $result = pg_query($query);
      Please i really need help on this code, its driving me crazy
      thanks

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Parse error: syntax error, unexpected T_VARIABLE in C:\ms4w\Apache\ htdocs\php\inpu tcrops.php on line 21
        the double quotation marks end the string and PHP expects either a semicolon or a dot operator to extend the string.

        Comment

        • sshade25
          New Member
          • Nov 2009
          • 10

          #5
          when you said that php expects a dot or semi-colon to extend the string what do you mean exactly.

          I have these but its still giving me the same error

          Code:
          $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs,local_name)VALUES('crop_id', "$crop_type"; "$crop_name"; "$cultivation_yrs"; "$local_name")";

          Code:
           $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs,local_name)VALUES('crop_id' "$crop_type" "$crop_name" "$cultivation_yrs" "$local_name")";
          Code:
          $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs,local_name)VALUES('crop_id',."$crop_type".,."$crop_name".,."$cultivation_yrs".,."$local_name".)";

          Parse error: syntax error, unexpected T_VARIABLE in C:\ms4w\Apache\ htdocs\php\inpu tcrops.php on line 21

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            None of the versions you are showing match the example I gave.
            Look at my example and you will understand.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              when you said that php expects a dot or semi-colon to extend the string what do you mean exactly.
              I’m talking about basic PHP syntax.

              Code:
              $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs,local_name)VALUES('crop_id', "$crop_type"; "$crop_name"; "$cultivation_yrs"; "$local_name")";
                             =>                                =>                          =>                       ^ end of string
              correct usage of quotation marks:
              Code:
              // quotes inside a string
              $str = "bla 'bla' blubb";
              $str = 'bla "bla" blubb';
              $str = "bla \"bla\" blubb";
              $str = 'bla \'bla\' blubb';
              // a variable inside a string
              $str = "bla bla $blubb";
              // concatenate strings (and variables)
              $str = "bla bla" . "blubb";
              $str = "bla bla" . 'blubb';
              $str = "bla bla" . $blubb;

              Comment

              • sshade25
                New Member
                • Nov 2009
                • 10

                #8
                I have tried what i understand from your example but i am still having the same error on the same line 21 . Please could you check to see if i am getting you right. Here is the code.

                Thanks

                Code:
                $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs,local_name)VALUES('crop_id','"$crop_type"','"$crop_name"','"$cultivation_yrs"','"$local_name"')";

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  (¬_¬)

                  what is so difficult in understanding that the string goes from one " to the next " (no matter what your intention is)?

                  there have been plenty of demos, how to use quotation marks correctly.

                  Comment

                  • sshade25
                    New Member
                    • Nov 2009
                    • 10

                    #10
                    You have not said anything different from what i did and i am saying, i have done that but it is not giving me any different solution.

                    Please all i need you to do is tell me if i did not put my double quotes right (which i am thinking i did).

                    Try and be patient with me, i am not a php expert like you. I am just here to learn from people like you.
                    Below is what i have done, i am still not right, please be patient and correct me

                    $query = "INSERT INTO crops (crop_id,crop_t ype,crop_name,c ultivation_yrs, local_name)VALU ES('crop_id',"$ crop_type","$cr op_name","$cult ivation_yrs","$ local_name")";

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Please all i need you to do is tell me if i did not put my double quotes right (which i am thinking i did).
                      you did not put your double quotes right. your line contains 10 double quotes (i.e. 5 separate strings) without any string operation inbetween. remove (or escape) them but two (first and last) and the error should vanish.

                      strings in PHP

                      PS. that’s not expert knowledge, this is something, you need in every programming and markup language

                      Comment

                      • sshade25
                        New Member
                        • Nov 2009
                        • 10

                        #12
                        Sorry Dormilich, but line 10 has to do only with my database connection. I dont have any error on line 10. Postgresql does not accept a separator comma or semicolon, it only accepts space in between the strings.

                        The database connection portion of the script which is from line 4 to line 14 runs perfectly okay and report success in connection. My problem lies on line 21 which i dont quite get.

                        The script inserts data into only 3 out of the five columns and omits the remaining colums, populating them with just 0 and ".

                        It looks strange to me.

                        if you could just let me know if there is any problem observed on my line 21.
                        I would appreciate it.

                        Thanks

                        Comment

                        Working...