Jump to content

Checking If Textfield Isnt Empty When Inserting Data Into Database


iNko

Recommended Posts

Got this code for inserting data into my database:

<?php
session_start();
require('config.php');
if(!isset($_SESSION['login']))
{
header('location: login.php');
}

if(isset($_POST['submitted'])){
$name= $_POST['name'];
$sqlinsert = "INSERT INTO Users (name) VALUES ('$name')";

if(!mysql_query($sqlinsert)) {
die('Couldnt create name');
}
$newrecord = "Name created";
}
?>

<form action="newuser.php" method="POST">
<input type="hidden" name="submitted" value="true" />
<label>Name</label>
<input type="text" name="name"/>
<input name="" type="submit" value="Create name">
</form>

<?php
echo $newrecord;
?>

Im thinking i need to write something like if ( $name != ''){ //insert } else { //show that field is empty }

But what if i have multiple fields? am i going in the right dirrection with this?

Here's a quick poor mans way to do what I think you're doing.

 

if(isset($_POST['submitted'])){
$error = FALSE;

// Check for a name:
if (empty($_POST['name'])) {
$error [] = 1; // Or $error [] = '<h5 style="color: red;">* You forgot to enter a name.</h5>'; and then print this array
} else {
$name= $_POST['name'];
$name = mysqli_real_escape_string($dbc, trim($name));
}

// Check for a something:
if (empty($_POST['somethnig'])) {
$error [] = 1;
} else {
$something= $_POST['something'];
$something = mysqli_real_escape_string($dbc, trim($something));
}

if (!$error) {
$sqlinsert = "INSERT INTO Users (name) VALUES ('$name')";
$r = mysqli_query($dbc, $sqlinsert); // or die("Error: ".mysqli_error($dbc));

if ($r) {$newrecord = "Name created";}
}
}

Re quoting my post (idk why but i cant find the edit button anymore)

<?php
session_start();
require('config.php');
if(!isset($_SESSION['login']))
{
header('location: login.php');
}

if(isset($_POST['submitted'])){
$name= $_POST['name'];
$surname= $_POST['surname'];
$sqlinsert = "INSERT INTO Users (name, surname) VALUES ('$name', '$surname')";

if(!mysql_query($sqlinsert)) {
die('Couldnt create user');
}
$newrecord = "User created";
}
?>

<form action="newuser.php" method="POST">
<input type="hidden" name="submitted" value="true" />
<label>Name</label>
<input type="text" name="name"/>
<label>Surname</label>
<input type="text" name="surname"/>
<input name="" type="submit" value="Create user">
</form>

<?php
echo $newrecord;
?>

i added a 2nd textfield and i also found this validation code:


$username = trim($_POST['username']); // cut spaces around

if(!preg_match('/ {2,}/', $username) // check for more than one space in the middle
{
// Show some error message...
}
else if(!preg_match('/^[a-z0-9_ ]+$/', $username)) // check for valid characters
{
// Show some error message...
}
else
{
// Username is good.
}

 

so i was thinking, can i do something like:

$name= trim($_POST['name']);
$surname= trim($_POST['surname']);

if(!preg_match('/ {2,}/', $name || $surname)
{
// Show some error message...
}
else if(!preg_match('/^[a-z0-9_ ]+$/', $name || $surname))
{
// Show some error message...
}
else
{
$sqlinsert = "INSERT INTO Users (name, surname) VALUES ('$name', '$surname')";

if(!mysql_query($sqlinsert)) {
die('Couldnt create user');
}
$newrecord = "User created";
}
}

 

EDIT: thx floridaflatlander, ill check ur code right now

Only one modification I'd do to your name/surname validator: Remove the underscore, and add a period and a dash instead. So that people with dashed double names, or initials, in their names can register too. ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.