?
Solved

PHP Postgres DB Save

Posted on 2011-03-03
7
Medium Priority
?
380 Views
Last Modified: 2012-05-11
Hi all,

I need to save some variables into a postgres table using php.

I have the variables and I just need to click on a link and have them saved.

Can anyone provide a simple straight foward sample code please?

0
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 35026358
ref. these links,

Basic
Sample 1
Sample 2

0
 

Author Comment

by:error77
ID: 35026395
For 500 points I'd like something specific to what I need please.
0
 
LVL 6

Expert Comment

by:dr_linux
ID: 35026657
Did you look at the links that logudotcom posted?  They are worth 1000 points.  Very simple and straight forward.  You can copy and paste the code in the links and be up and running.  You posted a very simple and basic question without much detail and were provided with VERY helpful links to accomplish what you need.  If your problem is more specific then you need to provide more details.
0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 3

Accepted Solution

by:
pius_babbun earned 2000 total points
ID: 35026748
I hope this code would help you !
<?php 
  // CONNECT TO DATABASE
   
  $conn_string = "host=localhost port=5432 dbname=database_name user=database_username password=database_password";
  $connection = pg_connect($conn_string);
 //connect to a database named "test" on the host "sheep" with a username and password

  //CHECK CONNECTION
  if (!$connection) {
    print("Connection Failed.");
    exit;
  }
  

?>
<!-- FORM TO GET THE VALUES-->
  <form method="post" action="process.php" name="insertform">
    User Id: <input type="text" name="id"> <br />
    User Name <input type=text name="username"> <br />
    First Name <input type=text name="fname"> <br />
    Last Name <input type=text name="lname"> <br />
    <hr />
    <input type="submit" name="submit" value="Insert">
     <a href="#" onclick="document.insertform.submit()">Click Here to Save</a>
  </form>

<?php
  //get values from form
  $submit 	= $_REQUEST['submit'];
  $id		= $_REQUEST['id'];
  $username = $_REQUEST['username'];
  $lname 	= $_REQUEST['lname'];
  
  // save this file as process.php
   if($submit != "Insert") 
   {
   
    // Insert the values from the form
    pg_query("INSERT INTO users VALUES ('$id','$username','$fname','$lname')");
    
	
	
    $myresult = pg_query($connection, "SELECT * FROM users WHERE id='$id'");
     // process results
    for ($lt = 0; $lt < pg_num_rows($myresult); $lt++) {
      $id = pg_result($myresult, $lt, 0);
      $username = pg_result($myresult, $lt, 1);
      $fname = pg_result($myresult, $lt, 2);
      $lname = pg_result($myresult, $lt, 3);
      
      // print results
      print("You inserted: <br />");
      print("<hr /><br />");
      print("User Id: $id<br />\n");
      print("Username: $username<br />\n");
      print("First Name: $fname<br />\n");
      print("Last Name: $lname<br />\n");
    }
?>

Open in new window

0
 

Author Closing Comment

by:error77
ID: 35026908
Thanks
0
 

Author Comment

by:error77
ID: 35026946
Ref: dr_linux...

I understand those links might contain great info ... but the beauty of Experts Exchange is that you get specific help for a problem and I use it when I want something specific and don't want to google around.

In this case I was requesting sample code to my specific question as didn't have time to look around.

I appreciate all the help but in this case what I wanted was some code, that's all.

Thanks all anyway.

0
 
LVL 6

Expert Comment

by:dr_linux
ID: 35026971
Shoot, I'm dumb, I could have received the points by copying and pasting the code in those links.  I'll do better next time, sorry.  That is the beauty of Experts Exchange.  I'm new to this and have a lot to learn.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question