Solved

PHP Postgres DB Save

Posted on 2011-03-03
7
373 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
pius_babbun earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

939 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now