Solved

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

Posted on 2013-02-05
5
354 Views
Last Modified: 2013-02-06
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
Comment
Question by:paddy086
5 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
ID: 38857453
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
 

Author Comment

by:paddy086
ID: 38857496
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
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 250 total points
ID: 38857518
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
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 250 total points
ID: 38857521
Combining...

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

$fullname = $lname. ', '. $finitial;
0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 125 total points
ID: 38857926
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now