Solved

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

Posted on 2013-02-05
5
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Viewers will learn how the fundamental information of how to create a table.

631 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