0

This is possibly a broad question:

I've been teaching myself how to program for the last year. I've learned how to create simple websites using HTML, CSS, and JavaScript. I've also learned how to create simple databases using PostgreSQL. I need help figuring out how to connect (API) the front end and the back end.

A friend suggested I learn how to use AWS RDS so I've already created a PostgreSQL DB instance there (tutorial). I've also connected to my DB using pgAdmin 4 using this tutorial.

What I would like to do is have my front end take in a number as the user's input and "submit" (POST) that value in the DB. Every time the user submits a new value, the new value is saved in the DB and the cumulative values of everything POSTed is returned to the front end for the user to see.

I know this project is a bit asinine but its a start. I can't find any more links on the internet to teach myself how to do this. If you have tips or resources (web links) to help me accomplish this, I would be really grateful.

Side note - I've also been studying python for the last year if that helps.

Fissure
  • 229
  • 4
  • 9

1 Answers1

0

First, connect to the database like so:

$c1 = new PDO('pgsql:host=THERDSURL;user=THEDBUSERNAME;dbname=THEDATABASENAME;password=THEPASSWORD;connect_timeout=500',null,null);

Then start a new query like so:

$statement=$c1->prepare("select * from mytable");

Then execute it:

$statement->execute();

then loop through the results like so:

while($row=$statement->fetch());
{
   var_dump($row)
}

it will vary a little depending on whether you're getting data or inserting data, but begin by learning how to get (select) data. Create a table using a GUI of some kind and learn how to query it programatically.

NOTE: Most people use a library of some kind to make the querying both more powerful and less complicated.. This code is in PHP, but a very similar process is done in many languages.

Note2: do not try to do any of this in javascript that's embedded in html or that's running on a client machine. Connecting to postgres using embeded javascript is not the normal way to go about doing this. Usually people use a language like PHP, Python, Java, etc.. and that's usually running on the server-- and although you can use javascript that runs on the server, I wanted to warn you about this because newer developers might not realize that javascript running on the server vs the browser has completely different capabilities where this is concerned.

Joe Love
  • 5,594
  • 2
  • 20
  • 32