Jump to content

PHP and Postgresql help....


jralexander137

Recommended Posts

So my boss has pretty much thrown me into fixing a problem with the company website and I have little to no php/sql knowledge. I'm trying to take the information from the html form and determine if the customer is already in the database and if so update their info otherwise add a new record.

 

I'm not sure what I'm doing wrong but again I'm in unfamiliar territory here. The form works fine but when trying to grab the data and query the database I get a 'network timed out' error.

 

Any thoughts or suggestions would be greatly appreciated.

 

That being said here is what I have so far:

The html page/form:

<LINK REL=StyleSheet HREF="interior.css" TYPE="text/css" MEDIA=screen>

</head>


<body bgcolor = "white" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">

<script language="JavaScript" src="javascript/menu.js"></script>
<script language="JavaScript" src="javascript/menucontextbottom.js"></script>

<script language="JavaScript">
    showToolbar();
</script>

<table BORDER="0" CELLPADDING="40" CELLSPACING="0" WIDTH="100%" >
  <tr valign = "top">
  <td bgcolor = "white">

  <table BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" >

    <tr valign = "top">

    <td bgcolor = "white">


    <body BGCOLOR="#FFFFFF">
  <table border="0" width="60%" align="center">
      <P><br>
      <FONT FACE="Arial, Helvetica, sans-serif">
      Thank you for your interest in the Auric code.&#160;&#160;Please fill
      out the following form to let us know who you are, and then you will
      be given download instructions.
      </FONT>
      </table>
      <P>
      <form method="post" action="/products/auric/registration.php">

  <table cellpadding="5" cellspacing="0" border="0" width="425" align="center">
            <TR BGCOLOR="#CC9999"> 
            <TR><TD> </TD><TR>
              <TD COLSPAN="2" ALIGN="CENTER" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="100%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
            <B><CENTER>
              <font color=#ff0000 FACE="Arial,Helvetica" SIZE=2>*</FONT> indicates a
        	required field.
            </CENTER></B>
	  </TD>

              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" FONT SIZE="2" COLOR="#FFFFFF">
        	  <font color=#ff0000>*</font><B>FIRST NAME</B></FONT></FONT> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="FirstName" SIZE="30" MAXLENGTH="40">
        	  </TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
        	  <font color=#ff0000>*</font><B>LAST NAME</FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%"> 
        	  <INPUT TYPE="Text" NAME="LastName" SIZE="30" MAXLENGTH="40">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
        	  <font color=#ff0000>*</font><B>COMPANY </FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%"> 
        	  <INPUT TYPE="Text" NAME="Company" SIZE="30" MAXLENGTH="100">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
        	  <font color=#ff0000>*</font><B>E-MAIL </FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%"> 
        	  <INPUT TYPE="Text" NAME="Email" SIZE="30" MAXLENGTH="80">
        	   </TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%">
                <FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
	 <B>ADDRESS 1</FONT></B>
               </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="Address1" SIZE="30" MAXLENGTH="100">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">
	<B>ADDRESS 2</FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%"> 
        	  <INPUT TYPE="Text" NAME="Address2" SIZE="30" MAXLENGTH="100">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"><B>CITY 
        	  </FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="City" SIZE="30" MAXLENGTH="40">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"><B>STATE 
        	  </FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="State" SIZE="30" MAXLENGTH="40">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"><B>ZIP CODE 
        	  </FONT></B> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="Zip" SIZE="30" MAXLENGTH="40">
        	</TD>
              </TR>
              <TR BGCOLOR="#CC9999"> 
        	<TD ALIGN="RIGHT" VALIGN="MIDDLE" BGCOLOR="#3F9AFE" WIDTH="25%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"><B>PHONE
        	  </B></FONT> </TD>
        	<TD ALIGN="left" VALIGN="TOP" BGCOLOR="#3F9AFE" WIDTH=" 62%">
        	  <INPUT TYPE="Text" NAME="Phone" SIZE="30" MAXLENGTH="40">
        	</TD>
              </TR>
              <TR BGCOLOR="#3F9AFE"> 
        	<TD COLSPAN="2" ALIGN="CENTER" VALIGN="TOP" WIDTH="100%">
        	  <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Submit">
        	  <INPUT TYPE="RESET" NAME="clear" VALUE="Clear">
        	</TD>
              </TR>
  </table>
      </form>


    <pre>

    </pre>

    <br>
    <br>

    </td></tr>

  </table>

 

And here is my php code:

<?php
phpinfo();
/**
 * This functionality of this code is to query the customer database and determine
 * if that customer already exists and if so update that record. Otherwise the
 * new customer information will be added to the database.
 */
global $connection, $id, $feedback;
//Establishing connection to database.
$connection = pg_connect("dbname=custDB" "user=postgres");

//If the connection is unsuccessful, will try to catch the error and print to screen.
if(!$connection){
	echo pg_errormessage($connection)
    	die("Error in connection: " . pg_last_error());
}

$myresult = pg_exec($connection, "SELECT * FROM FirstName, LastName,
     Company, Email, Address1, Address2, City, State, Zip, Phone");

//Simple wrapper function to reduce the code needed for each postgres query.
function query($sql){
	global $connection;
	return pg_exec($connection,$sql);
}

//Querying postgres for the next unique value in our sequence.
$res = query("SELECT nextval('seq_customer_id");

//Checking for errors.
if (!$res || pg_numrows($res)<1){
	$feedback .= pg_errormessage($connection);
	$feedback .= ' Error - Database didn\'t return next value. ';
	query("ROLLBACK");
	return false;	
}else{
	//Set the current customer's id number to the one retrieve from postgres.
	$id = pg_result($res,0,0);
}

//Register the id with PHP4.
$_SESSION['id'] = $id;

    //Checking to see if the required fields(firstname, lastname, company, email) are
    //filled out.
    if(isset($_POST('submit')){
    	$FirstName = $_POST['FirstName'];
    	$LastName = $_POST['LastName'];
    	$Company = $_POST['Company'];
    	$Email = $_POST['Email'];
    
    
    //If the information is accepted the 'id' is then ran against the customer
    //database.
    if($FirstName && $LastName && $Company && $Email){
    	//Collecting information for the rest of the customer record.
    	$Address1 = $_POST['Address1'];
        $Address2 = $_POST['Address2'];
        $City = $_POST['City'];
        $State = $_POST['State'];
        $Zip = $_POST['Zip'];
        $Phone = $_POST['Phone'];
    }
    //If the record exists then update the record.	
    if ($id)
       {

         $sql = "UPDATE custinfo SET FirstName='$FirstName',LastName='$LastName',Company='$Company'
         ,Email='$Email',Address1='$Address1',Address2='$Address2',City='$City',State='$State',
         Zip='$Zip',Phone='$Phone' WHERE id=$id";

       }
       //Otherwise inserting new record.
       else
       {
         $regdate = date(jmy);
         $sql = "INSERT INTO custinfo (FirstName, LastName, Company, Email,
          Address1, Address2, City, State, Zip, Phone, RegDate) VALUES ('$FirstName','$LastName','$Company',
         '$Email', '$Address1', '$Address2', '$City', '$State', '$Zip', '$Phone', $regdate)";
       }
       
       //Running SQL against the database(CustomerDatabase).
       $result = query($sql);

       session_write_close();
       header( "location: http://www.cpi.com/products/auric/license.html\n\n" );
       exit();
    }
    {
       session_write_close();
       header( "location: http://www.cpi.com/products/auric/fillOutReq.html\n\n" );
       exit();

     }
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/207507-php-and-postgresql-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.