• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

php/postgres problems

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

rebelwarlock

Member
Joined
Dec 13, 2004
since our database teacher decided it was important for us to use a db that most webhosts don't support, i'm stuck with trying to make my forms play nice with postgres. i can't figure out why, but when i try to insert data, nothing happens. i can select and display existing data, but i can't enter anything from my forms. here's the code i used:

Code:
$query = "INSERT INTO store (store_name, address, city, state) VALUES ('$store_name', '$address', '$city', '$state')";
pg_query($query);

i've even tried adding this:

Code:
$store_name = pg_escape_string($_POST['store_name']);
$address = pg_escape_string($_POST['address']);
$city = pg_escape_string($_POST['city']);
$state = pg_escape_string($_POST['state']);

but still nothing happens. i can't even get an error to display. i can't even get a variable to display! when i include "echo $store_name;" after the insert, it doesn't display anything. what's going on here?
 
I think with postgres variables inside the query have to be in {}

$query = "INSERT INTO store (store_name, address, city, state) VALUES ('{$store_name}', '{$address}', '{$city}', '{$state}')";

that's 1 part, I have barely touched postgres and try to stay clear of it as much as possible.
 
Code:
<?php

if (isset($_POST['Submit_store'])) {

  // Get posted values
  $store_name = pg_escape_string($_POST['store_name']);
  $address = pg_escape_string($_POST['address']);
  $city = pg_escape_string($_POST['city']);
  $state = pg_escape_string($_POST['state']);

  pg_connect("dbname=your_db user=your_username password=your_pass") or die("Couldn't Connect ".pg_last_error());

  $query = "INSERT INTO store (store_name, address, city, state) VALUES ('$store_name', '$address', '$city', '$state')";
  pg_query($query) or die("Query Failed ".pg_last_error());
  
} // END if POST Submit_store

?>

<form name="form_store" method="POST" action="<?=$PHP_SELF?>">
  <input type="text" name="store_name">
  <br>
  <input type="text" name="address">
  <br>
  <input type="text" name="city">
  <br>
  <input type="text" name="state">
  <br>
  <input type="submit" name="Submit_store" value="Submit">
</form>
 
thanks, got that part done now. here's the code that worked for me:

Code:
$store_name = $_POST["store_name"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];

the problem i'm having now is that one of my forms does not increase the $_POST count. here's what i'm using:

Code:
if(count($_POST == 0))
{
$query = "SELECT * FROM store WHERE store_id = '$id'";
$result = pg_query($query);
while($line = pg_fetch_row($result))
{
$store_name = $line[1];
$address = $line[2];
$city = $line[3];
$state = $line[4];
}
}
if(count($_POST)!=0)
{
echo "Successfully Deleted\n";
$store_name = $_POST["store_name"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$query = "DELETE FROM store WHERE store_id = '$id'";
pg_query($query);
}

i assign the values if the post count is 0 because those are the values i'm using to populate the form. the form displays the data so that the user can confirm that they're deleting the right record. when they press delete (a submit button), it should increase the post count and thus delete the record. however, when i noticed it wasn't working i put in an "echo count($_POST);" and saw that it stayed as 0. this is really confusing to me because i did my update form nearly identical to this one and it works perfectly.
 
rebelwarlock said:
Code:
$store_name = $_POST["store_name"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
You'll want to keep the pg_escape_string() functions in there or at least add_slashes() so that people will not be able to do SQL injections (very bad).
rebelwarlock said:
Code:
if(count($_POST == 0))
Wrap the count() around the $_POST then compare. Like this:
Code:
if (count($_POST) == 0))
Still though, I wouldn't suggest using the POST count, but like my last code, check to see if the submit_button has been set/posted.
 
Back