Solved

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

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

730 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