Solved

PHP Postgres DB Save

Posted on 2011-03-03
7
379 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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