Hello!
I need help.. please..

i'm new at php... but i need to make this:

i have a database (postgreSQL) on a server...

now on my webpage, i want to insert (manually-in my webpage) like with a text box, i insert there the client name (and other box's for the rest of the parameters of that table) and want to save that parameters, on my database, when i click to proceed..


can someone help me with the php code to make this..

if you don't understand what i want just tell me...

i really need help because it's for a school work :S

Thanks a lot!

if its a school work, think about some google search first.Trust me it will be in your favour in the long run.Start coding on your, if you get stucked up somewhere post your code here.Me and my friends here will try to help you.

I will post the algorithm here -
-connect to the database through the php
(think about getting php manual for you to help you)
-on your form submit, insert all the html element values to the db
-close connection

I searched a lot before i post here.. if i found i wouldnt bother no one...
i really though someone could me tell some code example for what i need.. i think its a easy thing to someone who knows very well php/sql...

this isn't all my work.. it's just a small thing that i'm stuck.. and if you could help me with some code example, it would help me a lot...

thank you.. :<

This is the way to connect to the db -

$dbconn = pg_connect("dbname=test");
//connect to a database named "test"

$dbconn2 = pg_connect("host=localhost port=5432 dbname=test");
// connect to a database named "test" on "localhost" at port "5432"

$dbconn3 = pg_connect("host=test port=5432 dbname=test user=test password=passwd");
//connect to a database named "test" on the host "test" with a username and password

$conn_string = "host=sheep port=5432 dbname=test user=test password=passwd";
$dbconn4 = pg_connect($conn_string);
//connect to a database named "test" on the host "test" with a username and password

Something like this will be your html -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>For you</title>
<style type="text/css">
.table{
font:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color:#3300FF;
background-color:#6699FF;
border:1px solid #FFFFFF;
}
.td{
color:#3300FF;
padding:5px 5px 5px 5px;
margin:5px 5px 5px 5px;
width:100px;

}
</style>
</head>

<body>
<table width="200" border="1">
  <tr>
    <td>&nbsp;User First Name:</td>
    <td>&nbsp;<input type="text" name="txtFName" id="txtFName"  /></td>
  </tr>
  <tr>
    <td>&nbsp;User Last Name</td>
    <td>&nbsp;<input type="text" name="txtFName" id="txtFName"  /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;<input type="submit" name="btnSubmit" id="btnSubmit"  /></td>
  </tr>
</table>

</body>
</html>

Now we need to embed your php into this html .
It may look like this -

<?
$dbconn = pg_connect("host=localhost port=5432 dbname=test") ;
if($dbconn)
{
echo "Connected to DB";
}else
{ echo "Connection Failed";exit();}
// connect to a database named "test" on "localhost" at port "5432"


/*
$dbconn = pg_connect("dbname=test");
//connect to a database named "test"

$dbconn3 = pg_connect("host=test port=5432 dbname=test user=test password=passwd");
//connect to a database named "test" on the host "test" with a username and password

$conn_string = "host=sheep port=5432 dbname=test user=test password=passwd";
$dbconn4 = pg_connect($conn_string);
//connect to a database named "test" on the host "test" with a username and password
*/
if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'])
{
$fName = $_POST['txtFName'];
$lName = $_POST['txtLName'];
$query= "insert into TABLENAME(FirstName,LastName) values ('".$fName."','".$lName."' )";
$sql_ex = pg_query($query);
	if($sql_ex)
	{
		echo "Inserted Successfully";
	}else{ echo "query Failed";}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>For you</title>
<style type="text/css">
.table{
font:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color:#3300FF;
background-color:#6699FF;
border:1px solid #FFFFFF;
}
.td{
color:#3300FF;
padding:5px 5px 5px 5px;
margin:5px 5px 5px 5px;
width:100px;

}
</style>
</head>

<body>
<table width="200" border="1">
  <tr>
    <td>&nbsp;User First Name:</td>
    <td>&nbsp;<input type="text" name="txtFName" id="txtFName"  /></td>
  </tr>
  <tr>
    <td>&nbsp;User Last Name</td>
    <td>&nbsp;<input type="text" name="txtFName" id="txtFName"  /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;<input type="submit" name="btnSubmit" id="btnSubmit"  /></td>
  </tr>
</table>

</body>
</html>

thanks a lot network18 for your help!

i will try it.. and tell later the results :)

fine:) best luck !!
mark the thread as solved if you think you are satisfied.

Ok i tried the code you gave, it creates the text box's but, when i click on submit, it does nothing... nothing happen when i click submit..

what maybe the problem?

my database it's from postgreSQL


edited: it says that it's connected to DB.

what error you get now,
did you set the variables to proper host name,database name etc and the table name also in the query.
which echo you getting amongst -
"Inserted Successfully" or echo "query Failed" ?

<?
$dbconn = pg_connect("host=x port=5432 user=x password=x dbname=x") ;
if($dbconn)
{
	echo "Connected to DB";
}
else
{ 
	echo "Connection Failed";
	exit();
}


if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'])
{
$nome = $_POST['nome'];
$idade = $_POST['idade'];
$morada = $_POST['morada'];
$clien_disp_id = $_POST['clien_disp_id'];
$query= "insert into cliente(nome,idade,morada,clien_disp_id) values ('".$nome."','".$idade."''".$morada."','".$clien_disp_id."')";
$sql_ex = pg_query($query);
	if($sql_ex)
	{
		echo "Inserted Successfully";
	}else{ echo "query Failed";}
}
?>

i have a table cliente, that has that the collumns: clien_id (witch it's a nextval), nome, idade, morada, and clien_disp_id (fk key).

it show up the text box's and it says it's connect to DB, i fill the text box's.. and when i click submit button, it doesnt make nothing...

thanks for help

Did you change your this line with proper host name etc ?
$dbconn = pg_connect("host=x port=5432 user=x password=x dbname=x") ;
echo your query and post here.
Also you did not change this with your submit button name

yes i changed the host, user, password and dbname..

i think it connects to database because it echo the message: "Connected to DB".

http://gpssafety.heliohost.org/_classes/textbox.php

thats the page.. does the button submit works with you? here it doesn't :S

tell me, why you make 2 times: $_POST ? it was because you insert the FName and the LName?? because now i'm inserting more parameters... don't know if it's because of that..

<?
$dbconn = pg_connect("host=x port=5432 user=x password=x dbname=x") ;
if($dbconn)
{
	echo "Connected to DB";
}
else
{ 
	echo "Connection Failed";
	exit();
}


if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'])
{
$nome = $_POST['nome'];
$idade = $_POST['idade'];
$morada = $_POST['morada'];
$clien_disp_id = $_POST['clien_disp_id'];
$query= "insert into cliente(nome,idade,morada,clien_disp_id) values ('".$nome."','".$idade."''".$morada."','".$clien_disp_id."')";
$sql_ex = pg_query($query);
	if($sql_ex)
	{
		echo "Inserted Successfully";
	}else{ echo "query Failed";}
}
?>

tell me, why you make 2 times: $_POST ? it was because you insert the FName and the LName?? because now i'm inserting more parameters... don't know if it's because of that..

No that's not the case.
Echo your query and post it here.

sorry i dind't understand what you mean? :s

anyone?!

i need to show some text box's and when i fill them and click on submit i want to that parameters get INSERTed in my database...

:S

ok i made it .. i putted the input code in a form with method post..

and now it's inserting in my database...

now could you tell me how when i click on my button i go to a page that i want?

when i click submit , it enters on here:

if($sql_ex)
	{
		echo "Inserted Successfully";
	}
	else
	{ 
		echo "query Failed";
	}

and it sends that message and stays on that page.. but i wanted to go to other page after click on submit...

the code of submit button:

<input type="submit" name = "btnSubmit" value="Submeter" />

what should i do , what code?

ok thread closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.