Solved

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

Posted on 2013-02-05
5
357 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 83

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article discusses four methods for overlaying images in a container on a web page
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

770 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