Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Postgres DB Save

Posted on 2011-03-03
7
Medium Priority
?
381 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
Technology Partners: 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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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