php inserting last inserted values to the database - php

I have my login table in mysql is like this
id
fname
lname
email
contactno
userid
password
acctype
status
Now my form is like this
<form name="frm" method="post" action="registerform.php">
<table id="new-account" class="create-an-account" width="100%" border="1" cellspacing="10px" cellpadding="10px">
<tr>
<td width="45%">
<label for="firstname">First Name</label>
<input type="text" style="width:230px;" name="Firstname" id="Firstname" /></td>
<td width="10%"></td>
<td width="45%">
<label for="lastname">Last Name:</label>
<input type="text" style="width:230px;" name="LastName" id="LastName" />
</td>
</tr>
<tr>
<td>
<label for="">Account Type</label>
<select class="select" name="at" id="ValidSelection" style="width:245px;" >
<option value="0">Select Account Type</option>
<option value="agent">agent</option>
<option value="admin">admin</option>
</select>
</td>
</tr>
<tr>
<td><label for="">Email Id:</label></td>
</tr>
<tr>
<td><input type="text" name="email" id="ValidEmail" style="width:230px;"/></td>
</tr>
<tr>
<td><label for="">Contact Number</label></td>
</tr>
<tr>
<td><input type="text" name="contact" id="ValidNumber" style="width:230px" /></td>
</tr>
<tr>
<td><label for=""><strong>Choose Your Login Id:</strong></label>
<input type="text" style="width:230px;" name="LoginId" id="LoginId"/>
</td>
</tr>
<tr>
<td><label for=""><strong>Password: <br /></strong></label></td>
</tr>
<tr>
<td><input type="password" style="width:230px;" name="Password" id="ValidPassword" /></td>
</tr>
<tr>
<td><label for="">Confirm Password:</label></td>
</tr>
<tr>
<td>
<input type="password" style="width:230px;" name="ConfirmPassword" id="ValidConfirmPassword"
/>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="signup" value="Create Account" style="margin-top:20px" /></td>
</tr>
</table>
and for insert data my php code is like this
<?php
if(isset($_REQUEST['signup'])) {
mysql_query("insert into login (`fname`,`lname`,`email`,`contactno`,`userid`,`password`,`acctype`,`status`) values('".$_REQUEST['Firstname']."','".$_REQUEST['LastName']."','".$_REQUEST['email']."','".$_REQUEST['contact']."','".$_REQUEST['LoginId']."','".$pswd."','".$_REQUEST['at']."','active')");
}
?>
Now here when I am reloading the page it is automatically inserting the last entered values to the database. So here can someone kindly tell me what is the issue here? Any help and suggestions are welcome.

If you reload the page after submitting a form, it will keep the POST data. To solve this follow the below things :
You can redirect to some other page after inserting the data, use header("location:new_page.php")
You can unset REQUEST, use unset($_REQUEST) after insert

after inserting the $_POST data use redirect to avoid this situation .
even you can redirect to same page like this :-
header('Location: '.$_SERVER['PHP_SELF']);
exit;

Related

How to Insert Multiple Checkboxes and their Textfields Into MySQL Database

I am new in the field of PHP.
I am working on a form to get information from a patient regarding a specific disease.
In this form i have multiple check-boxes and text fields with each check-box. If one check bos is checked then values of its text fields and checkbox values has to insert in database.
Please tell me the code to insert checked values along with textfields into database.
<form>
<table>
<tr>
<td colspan="4">Past Medical History:</td>
</tr>
<tr valign="top">
<td colspan="4" height="290"><table border="0" width="100%">
<tbody>
<tr>
<td width="26%"><div align="center">Problem</div></td>
<td width="18%"><div align="center">From (Year)</div></td>
<td width="56%"><div align="center">Details</div></td>
</tr>
<tr>
<td><input name="chkBP" id="chkBP" value="BP" type="checkbox" />
Blood Pressure</td>
<td><div align="center">
<input name="txtBPfrom" id="txtBPfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtBPDetail" id="txtBPDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkDiabetes" id="chkDiabetes" value="Diabetes" type="checkbox" />
Diabetes</td>
<td><div align="center">
<input name="txtDiabetesfrom" id="txtDiabetesfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtDiabetesDetail" id="txtDiabetesDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkHighCholes" id="chkHighCholes" value="HighCholesterol" type="checkbox" />
High Cholesterol</td>
<td><div align="center">
<input name="txtHighCholesfrom" id="txtHighCholesfrom" size="15" value="" type="text"/>
</div></td>
<td><input name="txtHighCholesDetail" id="txtHighCholesDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkArthritis" id="chkArthritis" value="Arthritis" type="checkbox" />
Arthritis</td>
<td><div align="center">
<input name="txtArthritisfrom" id="txtArthritisfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtArthritisDetail" id="txtArthritisDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkAsthma" id="chkAsthma" value="Asthma" type="checkbox" />
Asthma</td>
<td><div align="center">
<input name="txtAsthmafrom" id="txtAsthmafrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtAsthmaDetail" id="txtAsthmaDetail" size="40" value="" type="text" /></td>
</tr>
<tr>
<td><input name="chkCirculation" id="chkCirculation" value="Circulation" type="checkbox" />
Circulation</td>
<td><div align="center">
<input name="txtCirculationfrom" id="txtCirculationfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtCirculationDetail" id="txtCirculationDetail" size="40" value="" type="text" /></td>
</tr>
</table></td>
</tr>
</form>
You will need to establish a connection to the database.
When the form is posted collect this data and insert into the database accordingly using $_POST.
Helpful example can be found here to connect
And to insert data
$link = mysqli_connect("localhost","root","","web_table");
mysqli_query($link,"INSERT INTO web_formitem (`ID`, `formID`, `caption`, `key`, `sortorder`, `type`, `enabled`, `mandatory`, `data`)
VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)")
or die(mysqli_error($link));
first add any method attribute to your form tag like, get or post
<form>
to
<form action= "" method="post">
and add a submit button too in your form
now on submit your form will post your form value
and you can catch them by php as to insert in database
<?php
if(isset($_POST['submit_btn_name']))
{
//your database connect
//catch all value, for example
$val=$_POST['check_value'];
//your insert query
}
?>
A checkbox will only post when it's checked.
A textfield will always get posted even when it's empty.
Use a form:
<form name="contactform" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Add a submit button:
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
PHP:
<?php
//var_dump($_POST);
$chkBP = $_POST['chkBP'];
$txtBPfrom = $_POST['txtBPfrom'];
$txtBPDetail = $_POST['txtBPDetail'];
//echo "-- $txtBPfrom $txtBPDetail --";
if ($chkBP == "BP"){
//echo"Bloodpressure = checked";
$sql="INSERT INTO patient_details (from, detail)
VALUES ('$txtBPfrom', '$txtBPDetail')";
mysql_query($sql);
}else{
echo"Bloodpressure = not checked";
}
?>
Demo: here

updating mysql by id

I'm new to PHP and after looking on the web for the last hour I couldn't find out what was wrong with my code so I come to you. Im trying to have a place to update my data collected from a from. I get this error. Undefined index: id in /Users/mm1/Desktop/php/backend form/edit_ac.php on line 12. Here is what I have so far. Can someone please help..
<?php require("database.php"); ?>
<?php require("functions.php"); ?>
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$id= mysql_prep($_GET['id']);
$last= mysql_prep($_POST['last']);
$first= mysql_prep($_POST['first']);
// update data in mysql database
$sql="UPDATE $tbl_name SET first='{$first}', last='{$last}' WHERE id='{$id}'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='edit_info.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
Here is the form (I know it all isn't complete I'm just trying to get it to work first.)
<?php require("database.php"); ?>
<?php // Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<form name="form1" method="post" action="edit_ac.php">
<table width="700" border="0" cellpadding="5">
<tr>
<td colspan="2" class="center"><h3>PARTICIPANT IDENTIFICATION</h3></td>
</tr>
<tr>
<td width="218">First</td>
<td width="456"><input name="first" type="text" maxlength="50" id="first" value="<? echo $rows['first']; ?>" /></td>
</tr>
<tr>
<td width="218">Last</td>
<td width="456"><input name="last" type="text" maxlength="50" id="last" value="<? echo $rows['last']; ?>" /></td>
</tr>
<tr>
<td width="218">Middles</td>
<td width="456"><input name="middle" type="text" maxlength="50" id="middle" value="<? echo $rows['middle']; ?>"/></td>
</tr>
<tr>
<td>Address</td>
<td><input name="address" type="text" maxlength="50" id="address" value="<? echo $rows['address']; ?>"/></td>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" maxlength="50" id="city" value="<? echo $rows['city']; ?>"/></td>
</tr>
<tr>
<td>State</td>
<td><input name="state" type="text" maxlength="15" id="state" value="<? echo $rows['state']; ?>"/></td>
</tr>
<tr>
<td>Zip</td>
<td><input name="zip" type="text" maxlength="15" id="zip" value="<? echo $rows['zip']; ?>" /></td>
</tr>
<tr>
<td>Home Phone</td>
<td><input name="home_phone" type="text" id="home_phone" value="<? echo $rows['home_phone']; ?>" /></td>
</tr>
<tr>
<td>Daytime Phone</td>
<td><input name="daytime_phone" type="text" id="daytime_phone" value="<? echo $rows['daytime_phone']; ?>" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input name="email_address" type="text" id="email_address" value="<? echo $rows['email_address']; ?>" /></td>
</tr>
<tr>
<td>Birthday</td>
<td><input name="month" type="text" id="month" size="3" maxlength="2" value=""/> / <input name="day" type="text" id="day" size="3" maxlength="2" value=""/> / <input name="year" type="text" id="year" size="5" maxlength="4" value=""/></td>
</tr>
<tr>
<td>Social Security Number</td>
<td><input name="ss_1" type="text" id="ss_1" size="5" maxlength="3" value=""/> - <input name="ss_2" type="text" id="ss_2" size="5" maxlength="2" value=""/> - <input name="ss_3" type="text" id="ss_3" size="5" maxlength="4" value="" /></td>
</tr>
<tr>
<td colspan="2" class="center"><h3>PHOTO IDENTIFICATION </h3></td>
</tr>
<tr>
<td>Type of ID</td>
<td>
<input type="text" name="type_of_id" id="type_of_id" value=""/></td>
</tr>
<tr>
<td>ID Number</td>
<td>
<input type="text" name="id_number" id="id_number" value="" /></td>
</tr>
<tr>
<td>Issuing Jurisdiction</td>
<td><input type="text" name="issuing_state" id="issuing_state" value=""/></td>
</tr>
<tr>
<td>Expiration Date</td>
<td><input type="text" name="expiration_date" id="expiration_date" value=""/></td>
</tr>
<tr>
<td>Issue Date</td>
<td><input type="text" name="issue_date" id="issue_date" value="" /></td>
</tr>
<tr>
<td colspan="2" class="center"><h3>ESTABLISHING YOUR ACCOUNT</h3></td>
</tr>
<tr>
<td colspan="2" class="center">Designate Account Type</td>
</tr>
<tr>
<td>
Acount Type
</td>
<td>
<label>
<input type="radio" name="traditional" value="1" id="traditional" />
Traditional</label>
<br />
<label>
<input type="radio" name="roth" value="1" id="roth" />
Roth</label>
<br />
<label>
<input type="radio" name="sep" value="1" id="sep" />
SEP</label>
<br />
<label>
<input type="radio" name="simple" value="1" id="simple" />
SIMPLE</label>
<br /></td>
</tr>
<tr>
<td colspan="2" class="center">Fund Your Account</td>
</tr>
<tr>
<td colspan="2"><input name="rollover" type="radio" value="1" id="rollover" />I will rollover cash from an existing IRA or qualified retirement plan
</td>
</tr>
<tr>
<td>Prior Custodian Plan Name</td>
<td><input name="prior_plan_name" type="text" maxlength="50" id="prior_plan_name" value=""/></td>
</tr>
<tr>
<td>Expected Rollover Amount</td>
<td><input name="rollover_amount" type="text" maxlength="50" id="rollover_amount" value=""/></td>
</tr>
<tr>
<td colspan="2"><input name="transfer" type="radio" value="1" id="transfer" /> I will transfer assets from another IRA and have attached a TRANSFER AUTHORIZATION form </td>
</tr>
<tr>
<td colspan="2"><input name="contribution" type="radio" value="1" id="contribution"/> I have attached a contribution check as follows</td>
</tr>
<tr>
<td>IRA Cash Contribution for the Year</td>
<td><input name="cash_contributions_1" type="text" id="cash_contributions_1" value=""/></td>
</tr>
<tr>
<td>in the amount of</td>
<td><input name="amount_1" type="text" id="amount_1" value=""/></td>
</tr>
<tr>
<td>IRA Cash Contribution for the Year</td>
<td><input name="cash_contributions_2" type="text" id="cash_contributions_2" value="" /></td>
</tr>
<tr>
<td>in the amount of</td>
<td><input name="amount_2" type="text" id="amount_2" value="" /></td>
</tr>
<tr>
<td>Employer OR Employee (circle one) SEP/SIMPLE Contribution for the Year</td>
<td><input name="employer_contributions" type="text" id="employer_contributions" value="" /></td>
</tr>
<tr>
<td>in the amount of</td>
<td><input name="amount_3" type="text" id="amount_3" value="" /></td>
</tr>
<tr>
<td></td>
<td style="text-align:right"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</table>
<?php
// close connection
mysql_close();
?>
Add the id into the form as a hidden field.
<input type="hidden" name="id" value="<?php print $rows['id'] ?>">
An undefined index simply means that the index of a certain array doesn't exist. So in this case $_GET has not index of id.
Perhaps you should be checking if the variables are set before anything.
if(!empty($_GET['id']) && !empty($_POST['last']) && !empty($_POST['first'])){
// other code
}
You may also be using the wrong superglobal, like GeoPhoenix said, are you sure it's not $_POST['id'] you're looking for?
If you aren't including the parameters in the call to the page you are getting errors because you turned error reporting all the way up in the first few lines:
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
Often PHP (and therefore PHP tutorials) assume a lower setting where PHP will either throw a warning or ignore index errors on arrays. Therefore when you hit:
$id= mysql_prep($_GET['id']);
$last= mysql_prep($_POST['last']);
$first= mysql_prep($_POST['first']);
It throws an error, but your references might assume it fails quietly.
Your form appears to provide the ID number in the id_number instead of just id. So switching to $id=mysql_prep($_GET['id_number']); is likely to get more effective results.
Since you are new to PHP I also strongly suggest changing your PHP library away from the old mysql lib to either mysqli or PDO. Changes people are suggesting will likley get you the results you expect but not fix the multiple SQL Injection vulnerabilities this script has.
I don't see anything in your form that has the name "id".
If your using $_get['id'] you must have a link or submit button that tranfers to a url that looks like this.
<a href="phpfile.php?id=PUT_VALUE_HERE>
Notice the url has the variable name id after the question mark and you tell what the variable is after the =.
Otherwise if your not going to put your variable in a url. You use $_POST['id'].
In order for $_POST['id'] to work you need an input in your form that has the name attribute set to id. For example
<input name="id" type="text">
I do not see anything in your form with a name attribute set to "id".

Adding more field to PHP form (user can add more than one address)

My users can enter more than one address but I want an actual button that generates the extra fields as only one address is compulsory and empty fields will look ugly! I also need the extra addresses to go into mysql database.
How would I do with Javascript if PHP is not possible
Some code that may help:
<td width="732" valign="top"><p>
<h3 class="main">Address Details</h3>
<p class="normal"> You are able to add up to 3 addresses but only 1 is compulsory.
However it would be helpful if you could insert 3 addresses:
<ul>
<li>Permanent home address</li>
<li>Postal address (where you will be from June to September)</li>
<li>Local address (where you currently live)</li>
</ul>
<?php
if(!empty($err)) {
echo "<div class=\"msg\">";
foreach ($err as $e) {
echo "* $e <br>";
}
echo "</div>";
}
?>
<br>
<form action="address.php" method="post" name="regForm" id="regForm" >
<table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td>Street<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Street" type="text" id="Street" class="required" size="50">
</tr>
<tr>
<td>Line 2
</td>
<td><input name="Line2" type="text" id="Line2" class="required" size="50">
</tr>
<tr>
<td>Line 3
</td>
<td><input name="Line3" type="text" id="Line3" class="required" size="50">
</tr>
<tr>
<td>Town<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Town" type="text" id="Town" class="required" size="30">
</tr>
<tr>
<td>Postcode<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Postcode" type="text" id="Postcode" class="required" size="10">
</tr>
<tr>
<td>Country <font color="#CC0000">*</font></span></td>
<td><select name="Country" class="required" id="select8">
<option value="" selected></option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
(etc)
</select></td>
</tr>
<tr>
<td>Telephone Number<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Tele" type="text" id="Tele" class="required" >
</tr>
<tr>
<td>Fax<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Fax" type="text" id="Fax" class="required" >
</tr>
<tr>
<td>Mobile<span class="required"><font color="#CC0000">*</font></span>
</td>
<td><input name="Mobile" type="text" id="Mobile" class="required" >
</tr>
<tr>
<td>Type <font color="#CC0000">*</font></span></td>
<td><select name="Type" class="required" id="select8">
<option value="" selected></option>
<option value="H">Home</option>
<option value="P">Postal</option>
<option value="L">Local</option>
</table>
<p align="center">
<input name="doAddress" type="submit" id="doAddress" value="Submit">
to fix your parse error - put ; at the end of line $numrows = 0 + $_GET['numrows']
to generate more fields dynamically - use javascript, really, it's way better for this task than php

Error Placing on same page PHP

some of my code in addUser.php it pure html
addUser.php
<form action="process/addNewUser.php" method="POST" id="userForm">
<table width="79%" border="0" cellspacing="6" cellpadding="1"
class="tabcont">
<tr>
<td width="47%" align="right">Title:</td>
<td width="53%">
<select name="title"><option value='0'>- - select - -</option>
</select>
</td>
</tr>
<tr>
<td width="47%" align="right">
First Name:</td>
<td width="53%"><input type="text" id="firstname" name="firstName" class="required" /></td>
</tr>
<tr>
<td align="right">Middle Name:</td>
<td><input type="text" id="middlename" name="middleName" class="name" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="password" value="" readonly="readonly"
id="password" class="required" " /></td>
</tr>
<tr>
<td align="right" colspan="2">
<input name="addNewUser" type="submit" class="submit" id="submit" value="Submit" />
</td>
</tr>
</table>
</form>
addNewUser.php
Here i am doing validations and displaying error messages and if it is success sending him to another page.
But i want to show an error message on addUser.php instead of validations page. Please give me a sample code how i can do it.
addNewUser.php
Add the message in the session
$_SESSION["ErrorMsg"]="Name already exists";
And in your addUser.php , you put the following code in the place you need to show the msg :
if($_SESSION["ErrorMsg"])
{
echo $_SESSION["ErrorMsg"];
$_SESSION["ErrorMsg"]="";
}

Load Returned mySQL Values Into Form

I wonder whether someone may be able to help me please.
I've put together a form and php code (below) that allows an administrator to search for member records from a mysql database using the email address as the search criteria.
HTML Form
<form name="memberpasswordresetform" id="memberpasswordresetform" method="post" action="search.php">
<div class="container">
<p align="justify">Member Details </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>New Password</strong></td>
<td> </td>
<td><input name="newpass" type="password" id="newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm New Password </strong></td>
<td> </td>
<td><input name="conf_newpass" type="password" id="conf_newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><input type="submit" name="save" value="save" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
PHP Script
<?php
include("admin/link.php");
include("admin/opendb.php");
mysql_select_db ("userdetails");
$term = $_POST['email'];
$sql = mysql_query("select forename, surname, email address from userdetails where emailaddress like '%$email%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['forename'];
echo '<br/> Last Name: '.$row['surname'];
echo '<br/><br/>';
}
?>
The search functionality works fine, but I can't work out how to populate the forename and surname fields on my form from the records retrieved from my database. I've been looking for, and found examples on how to do this, if I want to simply show the data as a table, but I can't find any that explain how to populate the fields in a form.
I just wondered whether it would be at all possible that someone could provide some guidance please on how I can do this.
Many thanks
The previous answer will work fine if short tags are enabled on the server. You should stick to the long syntax as below in case you change hosting providers at a later date.
<input name="fname" type="text" id="fname" size="30" value="<?php echo $row['surname']; ?>" />
just set the value of your input element
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?= $row['surname'] ?>" /></td>
</tr>

Categories