i have some code that controls duplicate entries in particular the USER ID. it checks in the database at submit and if that USER ID exists already it gives that notification. now the problem is when i submit and if that USER ID entered already exists in the database, all the other entries on the form are cleared, prompting me to re_enter all the other details again. i find this annoying and retrogressive. i want some help on how better i can do it such that only the USER ID text box returns empty, keeping other details safe/unchanged or indeed alternatively keeping/buffering/caching all details previously entered so that i can also review the duplicate USER ID before changing it.
new_user.php
<h1 align="center">Create New User</h1>
<p align="center" style="color:#F00"><?php if(isset($_GET['dup'])){ echo "That User ID Already Exists!"; } ?> </p>
<form id="form1" method="post" action="add_user.php">
<table width="100%">
<tr>
<td width="204"><div align="right">User ID:</div></td>
<td width="286">
<input type="text" name="user_id" id="user_id" />
</td>
</tr>
<tr>
<td><div align="right">Full Names:</div></td>
<td>
<input type="text" name="fname" id="fname" />
</td>
</tr>
<tr>
<td><div align="right">Gender:</div></td>
<td><select id="sex" name="sex">
<option selected="selected" value="male">Male</option>
<option name="female">Female</option>
</select></td>
</tr>
<tr>
<td><div align="right">NRC Number:</div></td>
<td>
<input type="number" name="nrcno" id="nrcno" min="1" />
</td>
</tr>
<tr>
<td><div align="right">Phone Number:</div></td>
<td>
<input type="number" name="cellno" id="cellno" />
</td>
</tr>
<tr>
<td><div align="right">Email Address:</div></td>
<td>
<input type="email" name="emailad" id="emailad" />
</td>
</tr>
<tr>
<td><div align="right">Position Held:</div></td>
<td>
<input type="text" name="posh" id="posh" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="create" id="create" value="Add User" /></td>
</tr>
</table>
</form>
add_user.php
<?php
$user_id=$_POST['user_id'];
$fname = $_POST['fname'];
$sex= $_POST['sex'];
$name= $_POST['name'];
$nrcno = $_POST['nrcno'];
$cellno= $_POST['cellno'];
$emailad = $_POST['emailad'];
$posh = $_POST['posh'];
require("get_func.php");
checkID($id);
include("connect.php");
mysql_select_db("ceec", $con);
$query = "INSERT INTO user VALUES ('$user_id', '$fname', '$sex','$name', '$nrcno', '$cellno', '$emailad', '$posh')";
if (mysql_query($query)){
header("Location: success.php");
}
else {echo "Nada" . mysql_error(); }
mysql_close($con);
?>
get_func.php
<?php
function checkID($id){
include_once("connect.php");
mysql_select_db("ceec",$con);
$query = "SELECT * FROM user WHERE user_id = '$id'";
$result= mysql_query($query);
if($row = mysql_fetch_array($result))
{
header("Location: new_user.php?dup=true");
break;
}
else {}
}
?>
<input type="text" name="user_id" id="user_id"
<?php if(isset($_POST['user_id'])){echo htmlentities($_POST['user_id'];} ?>/>
Related
When user fills the form, if LinkedIn Id already exists, it should throw an error saying LinkedIn Id already exists..
Form is not saving data if user does not enter ContactID. ContactID is an auto-increment field in MySQL table. Since more than one user works on the same form at the same time, I want ContactID to be auto-filled by the next available number from MySQL table.
Here, is my HTML form code and following, the PHP code. If you require any other details to debug, please ask.
<html>
<body>
<form method="post" action="demo.php">
<link rel="stylesheet" href="contact_css.css">
<!--Create a table -->
<table>
<tr><td><b>Contact Information</b></td>
</tr>
<tr>
<div class="leftside">
<td>ContactID</td>
<td><input type="text" name="ContactID"></td>
</div>
<div class="rightside">
<td>ContactOwner</td>
<!-- <td><input type="text" name="ContactOwner"></td>-->
<td><select name="ContactOwner">
<option value="None">None</option>
<option value="Malik">Malik</option>
<option value="Ankit">Ankit</option>
<option value="Vikrant">Vikrant</option>
</select></td>
</div>
</tr>
<tr>
<div class="rightside">
<td>LeadSource</td>
<td><select name="LeadSource">
<option value="None">None</option>
<option value="Advertisement">Advertisement</option>
<option value="ColdCall">ColdCall</option>
<option value="LinkedIn">LinkedIn</option>
<option value="Web">Web</option>
</select></td>
<!--<td><input type="text" name="LeadSource"></td>-->
</div>
<div class="leftside">
<td>First_name</td>
<td><input type="text" name="First_name"></td>
</div>
</tr>
<tr>
<div class="rightside">
<td>Last_name</td>
<td><input type="text" name="Last_name"></td>
<td>AccountName</td>
<td><input type="text" name="AccountName"></td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="Title"></td>
<td>EmailID</td>
<td><input type="text" name="EmailID"></td>
</tr>
<tr>
<td>Industry</td>
<td><input type="text" name="Industry"></td>
<td>Department</td>
<td><input type="text" name="Department"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="Phone" required></td>
<td>Mobile</td>
<td><input type="text" name="Mobile"></td>
</tr>
<tr>
<td>Today_date</td>
<td><input type="date" name="Today_date"></td>
<td>LinkedIn</td>
<td><input type="text" name="LinkedIn"></td>
</tr>
<tr>
<td>CallStatus</td>
<td><select name="CallStatus">
<option value="None">None</option>
<option value="AnsweringMachine">AnsweringMachine</option>
<option value="Callback">Callback</option>
<option value="NotInterested">NotInterested</option>
<option value="Prospect">Prospect</option>
<option value="WrongContact">WrongContact</option>
<option value="PerformedInternally">PerformedInternally</option>
<option value="LessThan30Employee">LessThan30Employee</option>
</select></td>
<td>Website</td>
<td><input type="text" name="Website"></td>
</tr>
</table>
<!-- Second table-->
<table>
<tr><td><b>Address Information</b></td>
</tr>
<tr>
<div class="leftside">
<td>Street</td>
<td><input type="text" name="Street"></td>
</div>
<div class="rightside">
<td>OtherStreet</td>
<td><input type="text" name="OtherStreet"></td>
</div>
</tr>
<tr>
<div class="leftside">
<td>City</td>
<td><input type="text" name="City"></td>
</div>
<div class="rightside">
<td>State</td>
<td><input type="text" name="State"></td>
</div>
</tr>
<tr>
<td>Zip</td>
<td><input type="text" name="Zip"></td>
<td>Country</td>
<td><input type="text" name="Country"></td>
</tr>
</table>
<!--Third table-->
<table>
<tr><td><b>Description Information</b></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="Description" class="Description"></td>
</table>
<button type="button">Cancel</button>
<button type="button" class="button2" onclick="window.location.href='fetch_data.php'" />View</button>
<button type="button" class="button3" onclick="window.location.href='exm_list.php'" />Edit</button>
<button type="submit">Add</button>
</form>
< /body>
</html>
PHP Code:
<?php
// create a variable
if (isset($_POST)){
$ContactID=$_POST['ContactID'];
$ContactOwner=$_POST['ContactOwner'];
$LeadSource=$_POST['LeadSource'];
$First_name=$_POST['First_name'];
$Last_name=$_POST['Last_name'];
$AccountName=$_POST['AccountName'];
$Title=$_POST['Title'];
$EmailID=$_POST['EmailID'];
$Industry=$_POST['Industry'];
$Department=$_POST['Department'];
$Phone=$_POST['Phone'];
$Mobile=$_POST['Mobile'];
$Today_date=$_POST['Today_date'];
$LinkedIn=$_POST['LinkedIn'];
$CallStatus=$_POST['CallStatus'];
$Website=$_POST['Website'];
$Street=$_POST['Street'];
$OtherStreet=$_POST['OtherStreet'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];
$Country=$_POST['Country'];
$Description=$_POST['Description'];
}
//create connection
$connect=mysqli_connect('localhost','root','','contacts');
$check="SELECT COUNT(*) FROM contact where ContactID='$_POST[ContactID]' ";
$result=mysqli_query($connect,$check);
$data=mysqli_fetch_array($result, MYSQLI_NUM);
if($data[0] > 1){
echo "LinkedIn id already exists";
}
else {
$newUser="INSERT INTO contact(ContactID,ContactOwner,LeadSource,First_name,Last_name,AccountName,Title,EmailID,Industry,Department,Phone,Today_date,LinkedIn,CallStatus,Website,Street,OtherStreet,City,State,Zip,Country,Description)
VALUES('$ContactID','$ContactOwner','$LeadSource','$First_name','$Last_name','$AccountName','$Title','$EmailID','$Industry','$Department','$Phone','$Today_date','$LinkedIn','$CallStatus','$Website','$Street','$OtherStreet','$City','$State','$Zip','$Country','$Description')";
if (mysqli_query($connect,$newUser))
{
echo "Information Added<br/>";
}
else
{
echo "Error adding user in database, ContactID exists.<br/>";
}
}
Try changing:
$check="SELECT COUNT(*) FROM contact where ContactID='$_POST[ContactID]' ";
to
$check="SELECT COUNT(*) FROM contact where ContactID='$ContactID' ";
Also:
Form is not saving data if user do not enters ContactID. *** ContactID
is an autoincrement field in mysql table.
In your insert do not include
ContactID,
since this is an autoincrement field as you mention. What you should do instead is to do this:
//create connection
$connect=mysqli_connect('localhost','root','','contacts');
$check="SELECT COUNT(*) FROM contact where ContactID='$ContactID' ";
$result=mysqli_query($connect,$check);
$data=mysqli_fetch_array($result, MYSQLI_NUM);
if($data[0] > 1){
echo "LinkedIn id already exists";
}else {
$newUser="INSERT INTO contact(ContactOwner,LeadSource,First_name,Last_name,AccountName,Title,EmailID,Industry,Department,Phone,Today_date,LinkedIn,CallStatus,Website,Street,OtherStreet,City,State,Zip,Country,Description)
VALUES('$ContactOwner','$LeadSource','$First_name','$Last_name','$AccountName','$Title','$EmailID','$Industry','$Department','$Phone','$Today_date','$LinkedIn','$CallStatus','$Website','$Street','$OtherStreet','$City','$State','$Zip','$Country','$Description')";
if (mysqli_query($connect,$newUser))
{
echo "Information Added<br/>";
}
}
I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
However, I do not want to use a form action. Is it possible to do it
without creating form action with PHP?
No, that's not possible.
Just do it like this:
<tr>
<form method="post" action="mypage.php">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<?php
if(isset($_POST['finish_product']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
I want to save my data in my phpmyadmin database.But it's not saving. So while saving my data, it refresh the page but data not showing in database
Here is my class by which i want to save data:
class User{
public function __construct(){
$host='localhost';
$user='root';
$password='';
$conn=mysql_connect($host,$user,$password);
if(!$conn){
die("Database Not Connected" . mysql_error());
}
mysql_select_db("db_sign_up");
echo "Database created! ";
}
public function save_user($data){
$sql="INSERT INTO tbl_user(first_name,last_name,email_address,password,mobile_number,address,
city_name,country_name,zip_code)
VALUES('$data[first_name]','$data[last_name]','$data[email_address]','$data[password]',
'$data[mobile_number]','$data[address]','$data[city_name]','$datacountry_name]','$data[zip_code]')";
if(!mysql_query($sql)){
die("sl Error". mysql_error());
}
echo "Saved Successfully!";
//mysql_close($conn);
}
}
here is UI
<?php
require_once './classes/user.php';
$obj=new User();
if(isset($_POST['btn'])){
$obj->save_user($_POST);
}
?>
<html>
<head><title> Database Basic</title></head>
<body>
<form action="sign_up.php" method="post">
<table border="1">
<tr><td>Personal Information</td><td></td></tr>
<tr>
<td> First Name</td>
<td>
<input type="name" name="first_name" value="<?php if(isset($_POST['first_number'])){ echo htmlentities($_POST['first_name']);} ?> "/>
</td>
</tr>
<tr>
<td> Last Name</td>
<td>
<input type="name" name="last_name" value="<?php if(isset($_POST['last_name'])){ echo htmlentities($_POST['last_name']);} ?> "/>
</td>
</tr>
<tr>
<td> Email Address</td>
<td>
<input type="name" name="email_address" value="<?php if(isset($_POST['email_address'])){ echo htmlentities($_POST['email_address']);} ?> "/>
</td>
</tr>
<tr>
<td> Password</td>
<td>
<input type="password" name="password" value="<?php if(isset($_POST['password'])){ echo htmlentities($_POST['password']);} ?> "/>
</td>
</tr>
<tr>
<td> Mobile Number</td>
<td>
<input type="name" name="mobile_number" value="<?php if(isset($_POST['mobile_number'])){ echo htmlentities($_POST['mobile_number']);} ?> "/>
</td>
</tr>
<tr>
<td> Address</td>
<td>
<textarea name="address" rows="4" cols="30"></textarea>
</td>
</tr>
<tr>
<td> City</td>
<td>
<input type="" name="city_name" value="<?php if(isset($_POST['city_name'])){ echo htmlentities($_POST['city_name']);} ?> "/>
</td>
</tr>
<tr>
<td> Country</td>
<td>
<select name="country_name">
<option value=" ">Select Country ...</option>
<option value="bangladesh">Bangladesh</option>
<option value="srilanka">Srilanka </option>
<option value="india">India</option>
</select>
</td>
</tr>
<tr>
<td> Zip Code</td>
<td>
<input type="name" name="zip_code" value="<?php if(isset($_POST['zip_code'])){ echo htmlentities($_POST['zip_code']);} ?> "/>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="btn" value="Save">
</td>
</tr>
</table>
</form>
</body>
You have an error in your INSERT STATEMENT:
you are using countryname variable in your query as:
'$datacountry_name]'
I think this should be like this:
'$data[country_name]'
Side note:
You are using mysql_ its deprecated I suggest you to use mysqli_ or PDO.
secondly your code is open for SQL Injection, you need to prevent with sql injection.
its function receives an array, then check your array. The type fields in the db, and if are properly filled.
field int type = int value
if it is returned as a string, you can convert this:
$string = $data[mobile_number];
$int = (int)$string; // convert string type for int.
or use the type varchar to the field
if not the case, say more about the your code.
I want all details of user to be fetched in textboxes to edit profile of user accordingly based on username stored in session I don't want which statement is wrong I am not able to fetch values in textboxes. Please help me.
Profile.php
<?php
include('connection.php');
//include('validation1.php');
session_start();
if( !empty($_SESSION['username']) && isset($_SESSION['username']))
{
$username=$_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
//$result=mysql_fetch_array($checkinfo);
while($result = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
$name =($result['name']);
$address =($result['address']);
$contact =($result['contact']);
$state =($result['state']);
$city = ($result['city']);
$username =($result['uname']);
$oldpass = ($result['opass']);
}
}
if (isset($_POST['submit']))
{
$name =($_POST['name_txt']);
$address =($_POST['address_txt']);
$contact =($_POST['contact_txt']);
$gender = ($_POST['gender']);
$country = ($_POST['country']);
$state =($_POST['state_txt']);
$city = ($_POST['city_txt']);
$username =($_POST['uname_txt']);
$oldpass = ($_POST['opass_txt']);
$newpass = ($_POST['npass_txt']);
$insquery="UPDATE INTO profile(name, address, contact,gender, country, state, city,username,opassword,npassword) VALUES ('$name','$address','$contact','$gender','$country','$state','$city','$username','$oldpass','$newpass')";
$result=mysql_query($insquery);
// header("location:display.php");
if(!$insquery)
{
echo "Error Save [".mysql_error()."]";
}
/*else
{
// header("location:display.php");
}*/
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<div align="center">
Registration Form
</div>
<div align="right">
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
} ?><a href='logout.php' id='logout'>Logout</a>
</div>
<table>
<tr>
<td><label>Name:</label></td>
<td><input name="name_atxt" type="text" id="name_atxt" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td><label>Address:</label></td>
<td><textarea name="address_txt" cols="40" rows="4" value="<?php echo $address; ?>"></textarea></td>
</tr>
<tr>
<td><label>Contact:</label></td>
<td><input name="contact_txt" type="text" id="contact_ntxt" value="<?php echo $contact; ?>" /></td>
</tr>
<tr>
<td><label>Gender:</label>
<td>
<input type=radio name=gender value="male" id=male >Male</br>
<input type=radio name=gender value="female" id=female >Female</br></td>
</td>
</tr>
<tr>
<td><label>Country:</label></td>
<td><select name="country_select" id="country_select">
<option value="0">--select a country--</option>
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="Australia">Australia</option>
</select></td>
</tr>
<tr>
<td><label>State:</label></td>
<td><input name="state_atxt" type="text" id="state_atxt" value="<?php echo $state; ?>"/></td>
</tr>
<tr>
<td><label>City:</label></td>
<td><input name="city_atxt" type="text" id="city_atxt" value="<?php echo $city; ?>" /></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input name="uname_txt" type="text" id="uname_txt" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label>Old Password:</label></td>
<td><input name="opass_txt" type="password" id="opass_txt" value="<?php echo $oldpass; ?>" /></td>
</tr>
<tr>
<td><label>New Password:</label></td>
<td><input name="npass_txt" type="text" id="npass_txt" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" id="btn1" /></td>
<td><input name="reset_btn" type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
I want values already stored in database should be fetched in textbox of particular user who is in session and it is not fetching values in textbox.
$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
should be
$checkinfo = mysql_query("SELECT * FROM profile WHERE username='$username'");
Also your UPDATE syntax is incorrect
SQL UPDATE Statement:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
I have a class called contacts. In this class I have a method called addContact(). The first statement execute correct, but it seems like it does not get the $db->lastInsertId(). Need some help please. Here is my code:
public function addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone){
$addsuccessfully = true;
$addcontact_id = 0;
try {
$db = database::databaseConnect();
$stmt1 = $db->prepare('INSERT INTO personalinfo (firstname, middlename, lastname) VALUES (:addcontactfirstname, :addcontactmiddlename, :addcontactlastname)');
$stmt1->bindParam(':addcontactfirstname', $addcontactfirstname, PDO::PARAM_STR);
$stmt1->bindParam(':addcontactmiddlename', $addcontactmiddlename, PDO::PARAM_STR);
$stmt1->bindParam(':addcontactlastname', $addcontactlastname, PDO::PARAM_STR);
$successful1 = $stmt1->execute();
$addcontact_id = $db->lastInsertId();
if($successful1){
//$addcontact_id = $db->lastInsertId();
$successful1 = true;
$stmt2 = $db->prepare('INSERT INTO contactinfo (contact_id, streetnumber, streetname, suburbname, cityname, emailhome, emailwork, homephone, cellphone, workphone) VALUES (:addcontact_id, :addcontactstreetnumber, addcontactstreetname, :addcontactsuburb, :addcontactcity, :addcontactemailhome, :addcontactemailwork,:addcontacthomephone, :addcontactcellphone, :addcontactworkphone)');
$stmt2->bindParam(':addcontact_id', $addcontact_id, PDO::PARAM_INT);
$stmt2->bindParam(':addcontactstreetnumber', $addcontactstreetnumber, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactstreetname', $addcontactstreetname, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactsuburb', $addcontactsuburb, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactcity', $addcontactcity, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactemailhome', $addcontactemailhome, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactemailwork', $addcontactemailwork, PDO::PARAM_STR);
$stmt2->bindParam(':addcontacthomephone', $addcontacthomephone, PDO::PARAM_STR);
$stmt2->bindParam(':addcontactcellphone', $addcontactcellphone, PDO::PARAM_STR);
$stmt2->bindParam(':addcontacthomephone', $addcontactworkphone, PDO::PARAM_STR);
$successful2 = $stmt2->execute();
if($successful2){
$successful2 = true;
}
if(!$successful1 && !$successful2){
$addsuccessfully = false;
}
}
if($successful1 === true && $successful2 === true){
$addsuccessfully = true;
}
}
catch (PDOException $e) {
$addsuccessfully = false;
}
return $addsuccessfully;
}
I have a function that I call from my view page. Here is my function:
function addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone){
global $addsuccessfully;
contacts::addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone);
return $addsuccessfully;
}
And here is my page where I call the function. The page do say that the contact could not be added. I know the first query works as it the contact shows in the database, but it does not add the second bit into the contactinfo table of the database. Here is my view page:
<?php
/*The first thing that need to take place on this page is to ensure that the $admin value = 1.
* If the value is not 1 the user will get redirected to the home page. If the value of
* $admin = null, it then indicates that the user is not logged in. The system will then tell the
* user that he need to logon first, but also warn the user that if he is not an admin user he won't be
* allowed access to this page. This is to ensure that the user don't type the url address in
* his browser to try and access this page. This means that only admin users will be able to
* view this page while logged on and will be able to add new users. This will be an admin
* protected page. Protcted so the user must be logged in and and admin user.
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'functions/functions.php';
checkLoggedIn(page::ADDCONTACT);
echo $message;
if ($pageID == 1){
require_once 'includes/adminmenu.php';
if($_POST){
$addcontactfirstname = $_POST['addcontactfirstname'];
$addcontactmiddlename = $_POST['addcontactmiddlename'];
$addcontactlastname = $_POST['addcontactlastname'];
$addcontactstreetnumber = $_POST['addcontactstreetnumber'];
$addcontactstreetname = $_POST['addcontactstreetname'];
$addcontactsuburb = $_POST['addcontactsuburb'];
$addcontactcity = $_POST['addcontactcity'];
$addcontactemailhome = $_POST['addcontactemailhome'];
$addcontactemailwork = $_POST['addcontactemailwork'];
$addcontacthomephone = $_POST['addcontacthomephone'];
$addcontactcellphone = $_POST['addcontactcellphone'];
$addcontactworkphone = $_POST['addcontactworkphone'];
$errors = array();
$homephonelength = false;
$cellphonelength = false;
$workphonelength = false;
//$addsuccessfully = true;
stripUserInput($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber,$addcontactstreetname,$addcontactsuburb,$addcontactcity,$addcontactemailhome,$addcontactemailwork,$addcontacthomephone,$addcontactcellphone,$addcontactworkphone);
if(empty($addcontactfirstname)){
$errors[] = 'First name can\'t be empty!';
}
if(empty($addcontacthomephone) && empty($addcontactcellphone) && empty($addcontactworkphone)){
$errors[] = 'You must enter at least one telephone number!';
}
if(!empty($addcontacthomephone)){
//$phonenumber = $addcontacthomephone;
$homephonelength = chechPhoneLenght($addcontacthomephone);
if($homephonelength === true){
$errors[] = 'The home phone number you entered is too short!';
}
}
if(!empty($addcontactcellphone)){
//$phonenumber = $addcontactcellphone;
$cellphonelength = chechPhoneLenght($addcontactcellphone);
if($cellphonelength === true){
$errors[] = 'The mobile phone number you entered is too short!';
}
}
if(!empty($addcontactworkphone)){
//$phonenumber = $addcontactworkphone;
$workphonelength = chechPhoneLenght($addcontactworkphone);
if($workphonelength === true){
$errors[] = 'The work phone number you entered is too short!';
}
}
if(!empty($addcontactemailhome)){
$email = $addcontactemailhome;
is_valid_email($email);
if (is_valid_email($email) === false){
$errors[] = 'You have entered an invalid home email address!';
}
}
if(!empty($addcontactemailwork)){
$email = $addcontactemailwork;
is_valid_email($email);
if(is_valid_email($email) === false){
$errors[] = 'You have entered an invalid work email address!';
}
}
if(empty($errors)){
//Add the contact
$addsuccessfully = addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone);
if($addsuccessfully === true){
echo 'New contact added successfully!';
}else{
echo 'New contact could not be add. Please go back and try again!';
}
}else{
echo '<b>Please fix the following errors and try again!</b><br>';
foreach ($errors as $key => $error_message){
echo '<font color="red"><em>' . $error_message . '</font></em><br>';
}
?>
<h1>Add new contact</h1>
<p><em>Fields marked with <font color="red">*</font> must be completed.</em></p>
<form action="addcontact.php" method="post">
<table cellpadding="5">
<tr>
<td>
<b>First name:</b> <font color="red">*</font>
</td>
<td>
<input type="text" name="addcontactfirstname" value="<?php echo $addcontactfirstname; ?>" />
</td>
</tr>
<tr>
<td>
<b>Middle name:</b>
</td>
<td>
<input type="text" name="addcontactmiddlename" value="<?php echo $addcontactmiddlename; ?>" />
</td>
</tr>
<tr>
<td>
<b>Last name:</b>
</td>
<td>
<input type="text" name="addcontactlastname" value="<?php echo $addcontactlastname; ?>" />
</td>
</tr>
<tr>
<td>
<b>Street number:</b>
</td>
<td>
<input type="text" name="addcontactstreetnumber" value="<?php echo $addcontactstreetnumber; ?>" />
</td>
</tr>
<tr>
<td>
<b>Street name:</b>
</td>
<td>
<input type="text" name="addcontactstreetname" value="<?php echo $addcontactstreetname; ?>" />
</td>
</tr>
<tr>
<td>
<b>Suburb:</b>
</td>
<td>
<input type="text" name="addcontactsuburb" value="<?php echo $addcontactsuburb; ?>" />
</td>
</tr>
<tr>
<td>
<b>City:</b>
</td>
<td>
<input type="text" name="addcontactcity" value="<?php echo $addcontactcity; ?>" />
</td>
</tr>
<tr>
<td>
<b>Email (H):</b>
</td>
<td>
<input type="text" name="addcontactemailhome" value="<?php echo $addcontactemailhome; ?>" />
</td>
</tr>
<tr>
<td>
<b>Email (W):</b>
</td>
<td>
<input type="text" name="addcontactemailwork" value="<?php echo $addcontactemailwork; ?>" />
</td>
</tr>
<tr>
<td colspan="2">
<font color="blue"><em><b>NOTE:</b> You must enter at least one telephone number.</em><br> The number must include the area code e.g 065553322!</font>
</td>
</tr>
<tr>
<td>
<b>Phone (H):</b>
</td>
<td>
<input type="text" name="addcontacthomephone" value="<?php echo $addcontacthomephone; ?>" />
</td>
</tr>
<tr>
<td>
<b>Mobile:</b>
</td>
<td>
<input type="text" name="addcontactcellphone" value="<?php echo $addcontactcellphone; ?>" />
</td>
</tr>
<tr>
<td>
<b>Phone (W):</b>
</td>
<td>
<input type="text" name="addcontactworkphone" value="<?php echo $addcontactworkphone; ?>" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Add contact" value="<?php echo $addcontactfirstname; ?>" />
</td>
</tr>
</table>
</form>
<?php
}
}else{
?>
<h1>Add new contact</h1>
<p><em>Fields marked with <font color="red">*</font> must be completed.</em></p>
<form action="addcontact.php" method="post">
<table cellpadding="5">
<tr>
<td>
<b>First name:</b> <font color="red">*</font>
</td>
<td>
<input type="text" name="addcontactfirstname" />
</td>
</tr>
<tr>
<td>
<b>Middle name:</b>
</td>
<td>
<input type="text" name="addcontactmiddlename" />
</td>
</tr>
<tr>
<td>
<b>Last name:</b>
</td>
<td>
<input type="text" name="addcontactlastname" />
</td>
</tr>
<tr>
<td>
<b>Street number:</b>
</td>
<td>
<input type="text" name="addcontactstreetnumber" />
</td>
</tr>
<tr>
<td>
<b>Street name:</b>
</td>
<td>
<input type="text" name="addcontactstreetname" />
</td>
</tr>
<tr>
<td>
<b>Suburb:</b>
</td>
<td>
<input type="text" name="addcontactsuburb" />
</td>
</tr>
<tr>
<td>
<b>City:</b>
</td>
<td>
<input type="text" name="addcontactcity" />
</td>
</tr>
<tr>
<td>
<b>Email (H):</b>
</td>
<td>
<input type="text" name="addcontactemailhome" />
</td>
</tr>
<tr>
<td>
<b>Email (W):</b>
</td>
<td>
<input type="text" name="addcontactemailwork" />
</td>
</tr>
<tr>
<td colspan="2">
<font color="blue"><em><b>NOTE:</b> You must enter at least one telephone number.</em><br> The number must include the area code e.g 065553322!</font>
</td>
</tr>
<tr>
<td>
<b>Phone (H):</b>
</td>
<td>
<input type="text" name="addcontacthomephone" />
</td>
</tr>
<tr>
<td>
<b>Mobile:</b>
</td>
<td>
<input type="text" name="addcontactcellphone" />
</td>
</tr>
<tr>
<td>
<b>Phone (W):</b>
</td>
<td>
<input type="text" name="addcontactworkphone" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Add contact" />
</td>
</tr>
</table>
</form>
<?php
}
}
if ($pageID == 0){
return header('Location: ./');
}
?>
From the docs on PDO
string PDO::lastInsertId ([ string $name = NULL ] ) Returns the ID of
the last inserted row, or the last value from a sequence object,
depending on the underlying driver. For example, PDO_PGSQL() requires
you to specify the name of a sequence object for the name parameter.
Note:
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even
support the notion of auto-increment fields or sequences.
Without seeing the schema there is no way to know but it is possible that you don't have an auto increment field in your database so the insert ID isn't being returned. In that case your second block of code would fail but the first would succeed.