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.
Posts
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.