As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

writing to a postgreSQL database

BendystrawsBendystraws Registered User regular
edited May 2007 in Help / Advice Forum
hey all -

I'm trying to use php to write to a postGreSqL server. The way i understand it is that you have to do it through 2 different .php files, one calling the other "action" file. The problem is, I can read properly... just cant write to the database. I'm only looking to add 2 fields so its pretty simple. Anyone have experiance with this? Here are my current .php files.


this is my form .php

<html>
<body>
<form action="add.php" method="post">
name : <input type="text" name="name" size="40" length="40" value="Name" id="name"><BR>
text : <input type="text" name="text" size="40" length="40" value="Text" id ="text"><BR>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Clear It">
</form>



</body>
</html>


)

and here is my action file (add.php)

<html>
<body>
<?php
$dbh=pg_connect("host=thisisthehost dbname=dbname user=username password=password");

$name = pg_escape_string($_POST);
$text = pg_escape_string($_POST);

$query = "INSERT INTO story(name, text) VALUES('" . $name . "', '" . $text . "')";
$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", $name, $text,);
pg_close();
?>

</body>
</html>




whats wrong? thanks for all the help.

Bendystraws on

Posts

  • Options
    FyreWulffFyreWulff YouRegistered User, ClubPA regular
    edited May 2007
    I can't help you at the moment but you might want to hide that password

    FyreWulff on
  • Options
    rock217rock217 Registered User regular
    edited May 2007
    What is the error? What is the line number? What are your column data types? What are your key constraints? How about a link to your phpinfo(); ? FFS spell check your posts!

    rock217 on
    don't draw an ascii penis...don't draw an ascii penis...don't draw an ascii penis...
  • Options
    GeodGeod swim, swim, hungryRegistered User regular
    edited May 2007
    Yeah, an error or line number would be helpful. Anyways, lets see what I can do. I'm more used to mySQL so I might be mistaken on a few things and I'm still only a month old when it comes to PHP.

    You have:

    [PHP]$query = "INSERT INTO story(name, text) VALUES('" . $name . "', '" . $text . "')";[/PHP]

    That might work, but you can also still do:

    [PHP]$query = "INSERT INTO story(name, text) VALUES('$name', '$text')";[/PHP]

    Try it out. You also have

    [PHP]printf ("These values were inserted into the database - %s %s %s", $name, $text,);[/PHP]

    What's with the extra comma on the end?

    You also don't have to have two php pages if you don't want to, there's an easy way to do it on one.

    I can't really help you beyond that, as it looks okay but an error message/line would be necessary for any further help. Make sure your database is set up correctly too.

    Geod on
  • Options
    krlkrl Registered User regular
    edited May 2007
    Are you sure you have permission to insert stuff into the db?

    krl on
Sign In or Register to comment.