Solved

PHP Postgres DB Save

Posted on 2011-03-03
7
367 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
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 count occurrences of each item in an array.

758 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

23 Experts available now in Live!

Get 1:1 Help Now