473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_type,crop_name,cultivation_yrs, local_name)VALUES('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.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  <head><title>Crop form</title>
  3.  </head>
  4.  <body>
  5.  <p>
  6.  <form action="inputcrops.php" method="post">
  7.  <table width="600" cellpadding="10" cellspacing="0" border="2">
  8.  <tr align="center" valign="top">
  9.  <td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
  10.  
  11.   Input crop_id: <input type="text" name="crop_id"><br>
  12.  Input crop_type: <input type="text" name="crop_type"><br>
  13.  Input crop_name: <input type="text" crop_name="crop_name"><br>
  14.  Input cultivation_yrs: <input type="text" cultivation_yrs="cultivation_yrs"><br>
  15.  Input local_name: <input type="text" local_name="local_name"><br>
  16.  <input type="Submit" name="submit" value="Submit">
  17.  
  18. </form></P>
  19.  </body>
  20.  </html>
script two is my php script
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <body>
  3. <?php
  4.  $PGHOST = localhost;
  5.  $PGDATABASE = "SantaRosaDB";
  6.  $PGUSER = "****";
  7.  $PGPASSWORD = "****";
  8.  $PGPORT = 5432;
  9.  $db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER
  10.  password=$PGPASSWORD");
  11.  if ($db_handle) {
  12.  echo 'Connection attempt succeeded.';
  13.  } else {
  14.  echo 'Connection attempt failed.';
  15.  }
  16.  $crop_id = intval( $_POST['crop_id']);  
  17.  $crop_type = pg_escape_string($_POST['crop_type']);
  18.  $crop_name = pg_escape_string($_POST['crop_name']);
  19.  $cultivation_yrs = intval( $_POST['cultivation_yrs']);
  20.  $local_name = pg_escape_string($_POST['local_name']);
  21.  $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)";
  22.  
  23. // INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)VALUES('6','food crop','garri','6','utara'); 
  24.  
  25. $result = pg_query($query);
  26.  if (!$result) {
  27.  $errormessage = pg_last_error();
  28.  echo "Error with query: " . $errormessage;
  29.  exit();
  30.  }
  31.  printf ("These values were inserted into the database - %s %s %s %s %s",$crop_id,$crop_type,$crop_name,$cultivation_yrs,$local_name);
  32.  pg_close();
  33. ?>
  34.   </body>
  35.  </html>
  36.  
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
Nov 27 '09 #1
11 2553
code green
1,726 Expert 1GB
You need to wrap quotes around string values
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO crops (crop_id,crop_type,crop_name,cultivation_yrs, local_name)
  2. 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
Nov 27 '09 #2
I have tried to enclose the string values in quites but it is giving me this error

Expand|Select|Wrap|Line Numbers
  1. $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"')";
  2.  $result = pg_query($query);
Parse error: syntax error, unexpected T_VARIABLE in C:\ms4w\Apache\htdocs\php\inputcrops.php on line 21

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

Expand|Select|Wrap|Line Numbers
  1. $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")";
  2.  $result = pg_query($query);
Please i really need help on this code, its driving me crazy
thanks
Nov 27 '09 #3
Dormilich
8,658 Expert Mod 8TB
Parse error: syntax error, unexpected T_VARIABLE in C:\ms4w\Apache\htdocs\php\inputcrops.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.
Nov 27 '09 #4
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

Expand|Select|Wrap|Line Numbers
  1. $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")";

Expand|Select|Wrap|Line Numbers
  1.  $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")";
Expand|Select|Wrap|Line Numbers
  1. $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\inputcrops.php on line 21
Nov 27 '09 #5
code green
1,726 Expert 1GB
None of the versions you are showing match the example I gave.
Look at my example and you will understand.
Nov 27 '09 #6
Dormilich
8,658 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. $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")";
  2.                =>                                =>                          =>                       ^ end of string
correct usage of quotation marks:
Expand|Select|Wrap|Line Numbers
  1. // quotes inside a string
  2. $str = "bla 'bla' blubb";
  3. $str = 'bla "bla" blubb';
  4. $str = "bla \"bla\" blubb";
  5. $str = 'bla \'bla\' blubb';
  6. // a variable inside a string
  7. $str = "bla bla $blubb";
  8. // concatenate strings (and variables)
  9. $str = "bla bla" . "blubb";
  10. $str = "bla bla" . 'blubb';
  11. $str = "bla bla" . $blubb;
Nov 27 '09 #7
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

Expand|Select|Wrap|Line Numbers
  1. $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"')";
Nov 27 '09 #8
Dormilich
8,658 Expert Mod 8TB
(¬_¬)

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.
Nov 27 '09 #9
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_type,crop_name,cultivation_yrs,local _name)VALUES('crop_id',"$crop_type","$crop_name"," $cultivation_yrs","$local_name")";
Nov 28 '09 #10
Dormilich
8,658 Expert Mod 8TB
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
Nov 28 '09 #11
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
Nov 28 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
2
by: | last post by:
I am hoping a mixed ASP- Javascript programmer could help me with this scenario: I have a list box populated with randomized records (NewID()) from a Broker (Salesperson) sql server table. ...
4
by: Shane | last post by:
Hello, I'm pretty new to setting up databases, but so far I'm getting along swimmingly. But I have one question. I'm setting up a database for a client who wants to sell tickets to their theater...
1
by: Tim Pascoe | last post by:
I've been trying to get the scripts posted earlier to the group by Clay Beatty to work properly. Using the three components he posted, I have managed to get a new SP generated. However, when I...
1
by: cloverme | last post by:
Hi, I need help populating a listbox from a database on a webform. I created a an access database with a table, fields and data. Then I created a WebForm in vb.net and added a DropDownList...
5
by: - | last post by:
Hi all, I want to create an sql script and one of the table that will be created is a 'country' table. The question is how do I populate the country tables? Should there be individual INSERT...
3
by: Himmel | last post by:
I have added a section of code to a function that is designed to copy data from several tables and place them into a single table. The new table already contains a unique ID and name, and I am...
12
by: grace | last post by:
i am wondering why my database retrieval becomes too slow...we set up a new server (ubuntu, breezy badger) machine where we transferred all our files from the old server.. Our new server uses Asus...
4
by: prosad | last post by:
hello, Just solved a problem using Javascript onclick, can click on any cell in a dynamic table and it will pass the innerText object value to my form text field. parts of code given below: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.