Customized User Registration Form - php

i have made user-register.tpl.php file. And i have set many text field in that.
But now i need that....
i want to store the users information to the database. bcz i have created the customized registration page, so i need that my text field values should be store in the database.
like this.......
Username: <input type="text" name="myuser" id="myuser" />
Now i want to store the username, which will entered in this myuser text filed.
NitishPanchjanya Corporation

<form action="index.php" method="post" name="acc_info_form">
Username: <input type="text" name="myuser" id="myuser" /><br/>
First Name: <input type="text" name="name" id="name" /><br/>
<input type="submit" value="Save"/>
</form>
index.php
//Here you have to write database connection and then
<?PHP if ( $_POST){ ?>
mysql_query("INSERT INTO users (myuser, name) VALUES('".$_POST['myuser']."',".$_POST['name']."' )") or die(mysql_error());
<?PHP } ?>

Related

how to insert in a table like shown in this image where tusername is come from previous page

i have page where i send tusername and get on 2nd page where a form is created.
i want to add multiple data in a table using that form. where id is requested and sub_code is entered by us table will save data like this.
http://i.stack.imgur.com/Zf8wg.jpg
<?php
include("db.php");
$tusername =$_REQUEST['tusername'];
#mysql_query("update INTO `techsub`(sub_code, tusername)
VALUES ('$sub_code','$tusername') where tusername='$tusername'");
?>
<html>
<body>
<form method="post">
Subject One: <input type="text" name="sub_code"><br>
Subject Two: <input type="text" name="sub_code"><br>
Subject Three: <input type="text" name="sub_code"><br>
Subject Four: <input type="text" name="sub_code"><br>
<input type="submit" value="Assign">
</form>
</body>
</html>
I think this can help you out
<?php
include("db.php");
// Lets test if the form has been submitted
if(isset($_POST['SubmitCheck'])) {
// The form has been submited
$tusername =$_REQUEST['tusername'];
foreach($_POST['sub_code'] as $sub_code) {
//insert your data
#mysql_query("update INTO `techsub`(sub_code, tusername)
VALUES ('$sub_code','$tusername') where tusername='$tusername'");
}
}
else {
// The form has not been posted
}
?>
<form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Subject One: <input type="text" name="sub_code[1]"><br>
Subject Two: <input type="text" name="sub_code[2]"><br>
Subject Three:<input type="text" name="sub_code[3]"><br>
Subject Four: <input type="text" name="sub_code[4]"><br>
<input type="hidden" name="SubmitCheck" value="Assign">
<input type="Submit" name="Form1_Submit" >
</form>

MYSQL/PHP: Inserting data via HTML form, same "input name"

I'm new to MySQL and as a learning project I'd like to make a recipe database. I'd like to the user to be able to enter ingredients through a simple HTML form but I'm stuck in how to label the form so that I can enter several ingredients into the database at once.
I'd like to do something like this:
<form method="post" action="insert.php">
Ingredient 1: <input type="text" name="ingredient"><br />
Ingredient 2: <input type="text" name="ingredient"><br />
Ingredient 3: <input type="text" name="ingredient"><br />
<input type="submit" value="Submit">
</form>
When I do this, I add rows to the table but they're all empty. I know it's got something to do with me using "ingredient" (the table value where I want to add the ingredient name) several times in the form, but I just don't know how to solve it.
I would absolutely love some input on how to make it work.
write it like
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
and when you will get the REQUEST array in php, you will actually
get an array of names
like
$ing = $_POST['ingredient']; // $ing will be indexed array
You can't use same name for multiple textboxes like you have done. The last input box's value will overwrite all of the other ones' since they have the same name. Either you have to use different names for each input texts or define name as array like :
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
Ingredient 3: <input type="text" name="ingredient[]"><br />
So $_REQUEST['ingredient'] or $_POST['ingredient'] will be a normal PHP array from which you can get the value of each textbox like $_REQUEST['ingredient'][x] where x is some integer index, which is valid as long as count($_REQUEST['addCart']) > x.
Working Code :
index.html
<!DOCTYPE html>
<html>
<head>
<title> Recipes </title>
</head>
<body>
<form method="post" action="register.php">
Ingredient 1 : <input type="text" name="ing1"/><br/>
Ingredient 2 : <input type="text" name="ing2"/><br/>
Ingredient 3 : <input type="text" name="ing3"/><br/><br/>
<input type="submit" value="Enter" name="submit"/>
</form>
</body>
</html>
register.php
<?php
// coding to check database connection
$connection = mysqli_connect('localhost','root','adm','recipe');
/*
root - username
adm - passowrd
recipe - database name
*/
//checking whether submit button is clicked or not
if(isset($_POST['submit']))
{
// Escaping special char & getting the values we entered in form through POST method
$ing1 = mysqli_real_escape_string($connection,$_POST['ing1']);
$ing2 = mysqli_real_escape_string($connection,$_POST['ing2']);
$ing3 = mysqli_real_escape_string($connection,$_POST['ing3']);
//data is table name
$query = "INSERT into data VALUES('','$ing1','$ing2','$ing3')";
$result = mysqli_query($connection,$query);
echo "<p>Successfully Entered</p>";
?>
Click here to go to home
<?
}
?>
Create a database and name it as recipe and a table as data. Table data structure:
Output :

What should form action="???" on a signup page?

Okay, so I programmed this code, referencing various websites. I'm trying to program a signup page for a website. How does the html form connects to the PHP/how do I connect it?
I know that one place I have messed up is the action="" in the form. Different websites have told me to put different things in, from the server name ("localhost"), to "" to the name of the file that the php is in (I want to do it in the same file as the form if that is possible, I tried both that and a separate file). What do I put in there so when submit is clicked, it gives the error messages on the same screen as the form, and when submit is clicked and there are no error messages, it continues? Where do I link the page it continues on to?
Also, tell me if any of my code is deprecated. I've been trying to check everything, but I could of missed something.
<?php
include 'connect.php';
//if submit is clicked
if (isset($_POST['submit'])) {
//then check if all fields are filled
if (!$_POST['username'] | !$_POST['password'] | !$_POST['firstname'] | !$_POST['MI'] | !$_POST['lastname'] | !$_POST['email'] | !$_POST['phonenumber'] | !$_POST['country'] ) {
die('You did not complete all of the required fields'); }
$usernamesquery = mysql_query("SELECT * FROM logins WHERE username='$usernametest'");
if(mysqli_stmt_num_rows($usernamesquery) > 0) {
die('This username is already taken.');
}
}
?>
<form action="????????" method="post">
Username: <input type="text" name="username" maxlength="30"><br>
Password: <input type="password" name="password" maxlength="30"><br>
First Name: <input type="text" name="firstname" maxlength="30"><br>
Middle Initial: <input type="password" name="MI" maxlength="30"><br>
Last Name: <input type="text" name="lastname" maxlength="30"><br>
Email: <input type="password" name="email" maxlength="50"><br>
Phone Number: <input type="text" name="phonenumber" maxlength="11"><br>
Country: <input type="password" name="country" maxlength="40"><br>
<input type="submit">
</form>
If you want it to direct to the same file, then just use:
<form action="" method="POST">
Also there is no $_POST["submit"] since you haven't given your submit button a name.
<input type="submit" name="submit">
Also, does $usernametest actually contain anything? Since you haven't given it a value in your code above.
The "action" is page with some script, that parse the form data and do the login.
For example: the form has action "login.php", that means after you submit the form, the data are sent to "login.php" where you can acces it via $_POST variable. If you have login logic and form in the same file, you don't have to set any action, it's ok if you do it like this
<form action="" method="POST">
More info here
If you want to submit the form to the same page (the URL you are allready on), you can just leave out the action from the <from> tag. Otherwise, you specify the URL (relative or absolute) the form needs to be submitted to.

Updating a Row in PostgreSQL with PHP

I was wondering what the syntax was in PHP to update a row in a PostgreSQL database. I have made a login page that checks a UserName and Password from a database, then it goes to a page where it displays all the user info from the database for that user name. I am trying to allow the user to change some of the columns, like password, name, etc. So I added another page that has fields for each of the columns I want to change.
This is the code I have for the query:
if(array_key_exists('save',$_POST))
{
$firstname=$_POST['ifirstname'];
$lastname=$_POST['ilastname'];
$email=$_POST['iemail'];
$password=$_POST['ipassword'];
$conn_string='host=#### port=#### dbname=###### user=####### password=######';
$dbconn=pg_connect($conn_string) or die('Connection failed');
$query="UPDATE project.customer SET FirstName='$firstname',
LastName='$lastname',Email='$email',Password='$password')
WHERE UserName=$1";
$result=pg_query($dbconn,$query);
$row_count= pg_num_rows($result);
pg_free_result($result);
pg_close($dbconn);
}
This is for the fields:
<div id="header">UPDATE USER INFO</div>
<form id="testform" name="testform" method="post" action="" >
<p> <label for="ifirstname">First Name:</label>
<input name="ifirstname" type="text" id="ifirstname"/>
</p>
<p> <label for="ilastname">Last Name:</label>
<input name="ilastname" type="text" id="ilastname"/>
</p>
<p> <label for="iemail">E-Mail:</label>
<input name="iemail" type="text" id="iemail"/>
</p>
<p>
<label for="ipassword">Password:</label>
<input name="ipassword" type="password" id="ipassword"/>
</p>
<p>
<label for="iconfpass">Confirm Password:</label>
<input name="iconfpass" type="password" id="iconfpass"/>
</p>
<p>
<input type="submit" name="save" value="Register"/>
</p>
</form>
I think it must be like this. Also make user to write old password when changing data for security reason. Also dont forget to filter your data before using in query to avoid sql injection attacks
$query="UPDATE project.customer
SET (FirstName,LastName,Email,Password) =
('$firstname','$lastname','$email','$password')
WHERE UserName= '$1' and Password = '$oldpassword'";
Why not just use standard SQL syntax?
Update project.customer Set
"FirstName" = '$firstname',
...
Where ...
The main difference in Postgres is that you usually quote the column names.

Inserting data from html to php to mysql database

I have done my research but have found nothing specific enough to my problem
I have an HTML form, asking for data, then a php script that is suppose to put the data in a mysql database
When i try it on my localhost, i dont get any errors
but when i check on phpmyadmin, there is no new data
the html:
<html>
<head>
<form action="insert.php" method="post">
ID: <input type="text" name="ID"><br>
Family ID: <input type="text" name="Family_ID"><br>
First Name: <input type="text" name="First_Name"><br>
Last Name: <input type="text" name="Last_Name"><br>
Gender: <input type="text" name="Gender"><br>
Birthday: <input type="text" name="Birthday"><br>
Birthplace: <input type="text" name="Birthplace"><br>
Father ID: <input type="text" name="Father_ID"><br>
Mother ID: <input type="text" name="Mother_ID"><br>
Maiden Name: <input type="text" name="Maiden_Name"><br>
Mariage ID: <input type="text" name="Mariage_ID"><br>
Deathdate: <input type="text" name="Deathdate"><br>
Deathplace: <input type="text" name="Deathplace"><br>
Grave Location: <input type="text" name="Grave_Location"><br>
Email: <input type="text" name="Email"><br>
Phone: <input type="text" name="Phone"><br>
Address: <input type="text" name="Adress"><br>
Bio: <input type="text" name="Bio"><br>
Studies: <input type="text" name="Travail"><br>
Travail: <input type="text" name="Travail"><br>
Photo: <input type="text" name="Photo"><br>
Fete: <input type="text" name="Fete"><br>
<input type="Submit">
</form>
</head>
<body>
</body>
</html>
the php:
$username='root';
$password='121395';
$database='genealogy';
mysql_connect("localhost",$username,$password);
#mysql_select_db($database) or die( 'Unable to select database');
echo "Connected to MySQL";
$ID=mysql_real_escape_string($_POST['ID']);
$Family_ID=mysql_real_escape_string($_POST['Family_ID']);
$First_Name=mysql_real_escape_string($_POST['First_Name']);
$Last_Name=mysql_real_escape_string($_POST['Last_Name']);
$Gender=mysql_real_escape_string($_POST['Gender']);
$Birthday=mysql_real_escape_string($_POST['Birthday']);
$Birthplace=mysql_real_escape_string($_POST['Birthplace']);
$Father_ID=mysql_real_escape_string($_POST['Father_ID']);
$Mother_ID=mysql_real_escape_string($_POST['Mother_ID']);
$Maiden_Name=mysql_real_escape_string($_POST['Maiden_Name']);
$Mariage_ID=mysql_real_escape_string($_POST['Mariage_ID']);
$Deathdate=mysql_real_escape_string($_POST['Deathdate']);
$Deathplace=mysql_real_escape_string($_POST['Deathplace']);
$Grave_Location=mysql_real_escape_string($_POST['Grave_Location']);
$Email=mysql_real_escape_string($_POST['Email']);
$Phone=mysql_real_escape_string($_POST['Phone']);
$Address=mysql_real_escape_string($_POST['Adress']);
$Bio=mysql_real_escape_string($_POST['Bio']);
$Travail=mysql_real_escape_string($_POST['Travail']);
$Photo=mysql_real_escape_string($_POST['Photo']);
$Fete=mysql_real_escape_string($_POST['Fete']);
$query = "INSERT INTO bouan (ID, Family_ID, First_Name, Last_Name, Gender, Birthday,
Birthplace, Father_ID, Mother_ID, Maiden_Name, Mariage_ID,Deathdate, Deatchplace,
Grave_Location, Email, Phone, Adress, Bio, Travail, Photo, Fete) VALUES
('$ID','$Family_ID','$First_Name','$Last_Name','$Gender','$Birthday','$Birthplace',
'$Father_ID','$Mother_ID','$Maiden_Name','$Mariage_ID','$Deathdate','$Deathplace',
'$Grave_Location','$Email','$Phone','$Address','$Bio','$Travail','$Photo','$Fete')";
mysql_query($query) or die ("Error updating database");
mysql_error();
mysql_close();
All i get in return is:
Connected to MySQLError updating database
whats wrong? (i HAVE done my research, over 2 days fyi)
im sorry that im new to this, cant help it
You should probably
provide mysql_query with the real query
sanitize data before feeding it to sql
use {$_POST['whatever']} when you want to embed it into a string
check the return value of mysql_query
learn a thing or two.
Your first approach looks fine, but for security reasons fetch the posted variables like below $ID=mysql_real_escape_string($_POST['ID']);
mysql_real_escape_string() method will remove the unwanted characters and makes it secure.
At the end try to print the query which you are executing using echo or print statement.
echo $query;
Execute the result on your phpmyadmin. Phpmyadmin will let you know what are the errors in your mysql query and following those instructions you can change your query.
Debugging can be done by printing the results after each line execution wherever you feel something is going wrong.
Use mysql_error() to receive last error. Also I see potential bug in your query:
'`$ID`','`$Family_ID`','`$First_Name`' -- you may try to remove ` sign
try to do these 2 things, first add value attribute to your all input elements
e.g)
<input type="text" name="Family_ID" value="">
because it will not get into $_POST variable if you send blank value in the text box with no value attribute
try to add filed names so that you can track map with values and remove
`
from your field and value.

Categories