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.
script two is my php script
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
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>
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>
Connection attempt succeeded.These values were inserted into the database - 1 food crop 0
Comment