Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

HTML / php INSERT INTO SQL input name only one way example Hughes, p

Hi is there a way i can force the user to input a name in a perticular order and if not dissalow them

HTML FORM
<form action="insert.php" method="POST"> 

<!-- Use <pre> to preserve layout --> 

<pre> 
Pease enter Teachers Name Example follows (Hughes, P) 
Teachers Name:     <input type="text" name="vname" size=20><br>  
</pre> 

<input type="submit" value="Add"> 
<input type="reset" value="Reset"> 

</form> 

Open in new window


PHP INCERT SCRIPT
<?php 

// Read values from form using $_POST (safest) 


$fname=$_POST["vname"]; 


// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 


// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

// Run query 

$sql="INSERT INTO teacher (name) values ('$fname')"; 

if (mysql_query($sql,$db)) 
{ 
        echo "The following Teacher"; 
        echo "<br>Record: $fname  has been added to          
        database.<p>"; 
} 
else 
{ 
        do_error("Failed to add record"); 
} 

function  do_error($error) 
{ 
        echo  $error; 
        die; 
} 

?> 

Open in new window

0
paddy086
Asked:
paddy086
4 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
The best way is to make it two separate fields so you can combine them in PHP the way you want them.  You will still have people who will find a way to do it 'wrong'.
0
 
paddy086Author Commented:
Very true... So if I have two text fields one surname and second one first initial I can combine them both into example Hughes,P

How will I do that
0
 
Scott MadeiraCommented:
A couple things...

1. You need to validate the input before putting it into the database. Therefore you will check format and can then let the user know if they entered it correctly.  If they didn't then you have to redisplay the form, possibly change the text field to red so they know there is an error, etc.

2. You can use a regular expression to validate data format.  If you have a lastname, comma, space Initial format then a regular expression like this should meet your requirements:

// returns 1 if it matches, 0 if it doesn't
$success = preg_match('/^[A-Z][A-Za-z\']+\,\ [A-Z]$/', $name);

http://php.net/manual/en/function.preg-match.php

I agree with Dave though.  Do it as 2 separate fields, validate each one and then combine as desired.  That's easier from a user's perspective because it matches what other sites do.

A design question...  How will you handle the situation where you have Peter Hughes and Paul Hughes on your staff? Last name, first initial will create duplicate data.  If you try to delete one, your delete code (from your other question) will end up deleting both values.
0
 
Scott MadeiraCommented:
Combining...

Assuming $_POST['lname'] and $_POST['finitial'] for last name and first initial...

$fullname = $lname. ', '. $finitial;
0
 
Mark BradyCommented:
The best way is not to confuse people. Ask them to supply their first name and their last name in 2 separate fields. Keep it simple for them. In php build the exact string you want.

$last = ucfirst(strtolower($_POST['lname'])); // make sure the first letter is capitalised
$initial = substr($_POST['fname'], 0, 1); // get the first letter

$name = $last. ',' . ucase($initial); // uppercase first initial

$name now holds what you want for the database.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now