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?

Edited by iNko
Link to comment
Share on other sites

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";}
}
}

Edited by floridaflatlander
Link to comment
Share on other sites

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

Edited by iNko
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.