how to save the $_post in the table - php

im just askin how to create a form that will save the input in the $_post thingy into the table much appreciated thanks!! or can you help me fix this code or create a new one to work please? i need your help please thanks again!
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";
$id = $_POST['id'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$input = $_POST['input'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', 'input')";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["input"])) {
$input = "";
} else {
$input = test_input($_POST["input"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
echo "<input type='text' name='id'>";<br><br>
echo "<input type='text' name='name'>";
echo "<input type='text' name='comment'>";
echo "<input type='text' name='input'>";
echo "<input type='submit' name='submit'>";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>`

You miss $ sign for input variable
$sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', '$input')";

Add the <form> tag.
<form name="form_name" action="your_page.php" method="post">
<input type='text' name='id'>
<input type='text' name='name'>
<input type='text' name='comment'>
<input type='text' name='input'>
<input type='submit' name='submit'>
</form>
Updated:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$input = $_POST['input'];
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["input"])) {
$input = "";
} else {
$input = test_input($_POST["input"]);
}
$sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', 'input')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
<form name="form_name" action="" method="post">
<input type='text' name='id'>
<input type='text' name='name'>
<input type='text' name='comment'>
<input type='text' name='input'>
<input type='submit' name='submit'>
</form>

Related

how to throw validation error

registration_from.php
<!DOCTYPE HTML>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="" method="POST">
Name:
<input type="text" name="name">
<br/> <br/>
Username:
<input type="text" name="username">
<br/> <br/>
Password:
<input type="password" name="password">
<br/> <br/>
Email:
<input type="text" name="email">
<br/> <br/>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
<?php
require('connect.php');
require('validation.php');
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(isset($_POST["submit"])){
if($query = mysqli_query($connect,"INSERT INTO users
(`id`,`name`,`username`, `password`, `email`) VALUES ('','".$name."',
'".$username."', '".$password."', '".$email."')")){
echo "Success";
}else{
echo "Failure" . mysqli_error($connect);
}
}
?>
validation.php
<?php
// define variables and set to empty values
$nameErr = $emailErr = $userErr = $passwordErr = "";
$name = $email = $username =$password = "";
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["username"])) {
$userErr = "Username is required";
} else {
$username = test_input($_POST["username"]);
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password= test_input($_POST["password"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
connect.php
<?php
$connect = mysqli_connect("localhost", "root", "","php_forum")
or die("Error " . mysqli_error($connect));
?>
I'm developing a simple Registration from with four inputs i.e., Name, username, password, email.when the user fills out the form and click submit button then all the filled data should go n save in data base which is working fine in my case, but when the user wont fill any data and if user simply clicks a submit button then error message should be shown like "ALL FIELDS ARE NECESSARY", but where in my case even if i click submit button without entering any values the mesage i'm getting as success and all the null values are getting stored in the data base which should not happen, my output should be if i fill the forms n click submit button then all the data should be stored in database and if i click submit button without filling out any value then error should throw that "all field to be filled" and no null value should be stored in data base, please can any one guide me what changes i should do so that to get my desired output.
If you don't mind adding a little more code, you code do like:
In your registration_form.php
<?php
require('validation.php'); // Require first to do validation before queries
require('connect.php');
// Remove the part where you set variables to $_POST params
// Variables are already set inside validation.php
/**
* Then, I recommend moving queries to **connect.php**
* to have all your sql functions inside one file.
* Also moving the inserting of data to a function for easy grouping/calling
*/
if (isset($_POST["submit"]) {
// Check if validation does not fail
if ($emailErr == "" || $nameErr == "" || $userErr == "" || $passwordErr == "") {
// Call to insert function
doInsert($name, $email, $username, $password);
} else {
echo $emailErr . " " . $nameErr . " " . $userErr . " " . $passwordErr;
}
}
?>
In your connect.php
function doInsert($name, $email, $username, $password) {
$connect = mysqli_connect("localhost", "root", "","php_forum")
or die("Error " . mysqli_error($connect));
$sql = "INSERT INTO users(`id`,`name`,`username`, `password`, `email`)
VALUES ('','".$name."', '".$username."', '".$password."', '".$email."')";
$query = mysqli_query($connect, $sql);
if ($query) {
echo "Success";
} else {
echo "Failure " . mysqli_error($connect);
}
}
Please add error in session and print session in form file.
In validation.php
$nameErr = $emailErr = $userErr = $passwordErr = "";
$name = $email = $username =$password = "";
if (isset($_POST['submit'])) {
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
if($name == '' || $email == '' || $username == '' || $password == "")
{
echo "ALL FIELDS ARE NECESSARY";
exit();
}
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["username"])) {
$userErr = "Username is required";
} else {
$username = test_input($_POST["username"]);
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password= test_input($_POST["password"]);
}
}
registration_from.php
if(isset($_SESSION['error]) && !empty($_SESSION['error])){
echo $_SESSION["error"]
}
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

unable to insert url in mysql database using php

Html code:
<input type="file" class="form-control" name="video[]" placeholder=" Enter Image file">
<div><h2>OR</h2></div>
<input type="text" class="form-control" name="yurl" placeholder=" Enter youtube embedded code">
<button type='submit' name="save" class='btn btn-success waves-effect waves-light'><i class='fa fa-upload'></i> Save</button>
php code:
<?php
if(isset($_POST['save'])){
$yurl = ($_POST['yurl']);
$new_data=mysqli_real_escape_string($connection, stripslashes($yurl));
echo $yurl;
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video ,youtube)
VALUES ('$name', '$yurl')";
if (mysqli_query($connection,$sql)) {
?>
<script>
window.location.href = "view_gal_video.php";
</script>
<?php
}
}
}
?>
Url which I want to insert: https://youtu.be/sA0-QXbLnaE
both video and link get inserted
when i only want to insert link not a video it fails but don't gives any error
Updated code with database connection.
if(isset($_POST['save'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$yurl = $_POST['yurl'];
//$video='test';
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video, youtube)VALUES ('$name', '$yurl')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}

PHP Script submitting blank information to my SQL Database, not passing the inputted info through

I am attempting to learn some html/php. I have created a form that i want to submit info to a MYSQL database. I have created the database and created the forms etc. The problem i have is that when the form is submitted it is submitting blank info to the table. If i replace the variables with "123" that is posted to the database so it seems to not be pulling the info from the index to the form. Cannot work out why it is posting blank info, any suggestions? My index page is :
<html>
<head>
<style type="text/css">
.sms_image
{
text-align: right-side;
}
</style>
<script src="//www.powr.io/powr.js" external-type="html"></script>
<div class="powr-hit-counter" id="b6cbafa4_1487845849" align="right-side" </div>
<p class="sms_image"><img src="http://images.knowledge- action.co.uk/sites/default/files/sms_logo_short_0.jpg" height="100" width="170"> </img><br></p>
<title> Simply Mail Solutions </title>
</head>
<body background="https://media.licdn.com/media/AAEAAQAAAAAAAAYCAAAAJDQ1YTQ0MTNlLWI2MD ItNGYxOS05MjMxLWFmOTZhNjgyMjNhMA.png">
<font color="white">Welcome to a random test page</font>
<br>
<br>
<form action="yourform-processor.php" name="FirstAttempt" method="POST" enctype="text/plain">
<font face="impact" color="white">Client ID:</font>
<input type="text" name="client_id" ><br>
<br>
<font face="impact"color="white">Domain:</font>
<input type="text" name="domain"><br>
<br>
<font face="impact" color="white">Comments:</font>
<input type="textarea" name="comment" style="width: 568px; height: 273px"> <br>
<br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
<br>
<br>
</form>
<footer>
<p>Posted by: Dylan Cunliffe</p>
</footer>
</body>
</html>"
My PHP form that posts to the database is:
<?php
$servername = "localhost";
$username = "Dylanc";
$password = "xxx";
$dbname = "FirstAttempt";
$errors = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//first validate user input
if (empty($_POST['client_id'])) {
echo "enter client id";
$errors++;
} else {
$client_id = $_POST["client_id"];
}
if (empty($_POST['domain'])) {
echo "enter domain";
$errors++;
} else {
$domain = $_POST["domain"];
}
if (empty($_POST['comment'])) {
echo "enter comment";
$errors++;
} else {
$comment = $_POST["comment"];
}
if ($errors <= 0) {
//fields are not empty save to db
$sql = $conn->prepare("INSERT INTO FirstAttempt (client_id,domain,comment) VALUES(?,?,?) ");
$sql->bind_param("ssss", $client_id, $domain, $comment);
if ($sql->execute()) {
echo "New record created successfully";
} else {
//report bacck the error
}
}
$conn->close();
?>
Any suggestions would be greatly appreciated.
Best and simple clean solution use mysqli prepared statments, or use pdo prepared statements.
MYSQLI Prepared :
<?php
$servername = "localhost";
$username = "Dylanc";
$password = "xxxx";
$dbname = "FirstAttempt";
$errors = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//first validate user input
if (empty($_POST['client_id'])) {
echo "enter client id";
$errors++;
} else {
$client_id = $_POST["client_id"];
}
if (empty($_POST['domain'])) {
echo "enter domain";
$errors++;
} else {
$domain = $_POST["domain"];
}
if (empty($_POST['comment'])) {
echo "enter comment";
$errors++;
} else {
$comment = $_POST["comment"];
}
if ($errors <= 0) {
//fields are not empty save to db
$sql = $conn->prepare("INSERT INTO FirstAttempt (client_id,domain,comment) VALUES(?,?,?) ");
$sql->bind_param("ssss", $client_id, $domain, $comment);
if ($sql->execute()) {
echo "New record created successfully";
} else {
//report bacck the error
}
}
$conn->close();
?>
with PDO prepared statements
<?php
$servername = "localhost";
$username = "Dylanc";
$password = "xxxx";
$dbname = "FirstAttempt";
$charset = 'utf8';
$dsn = "mysql:host=$servername;dbname=$dbname;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$conn = new PDO($dsn, $username, $password, $opt);
//first validate user input
//first validate user input
if (empty($_POST['client_id'])) {
echo "enter client id";
$errors++;
} else {
$client_id = $_POST["client_id"];
}
if (empty($_POST['domain'])) {
echo "enter domain";
$errors++;
} else {
$domain = $_POST["domain"];
}
if (empty($_POST['comment'])) {
echo "enter comment";
$errors++;
} else {
$comment = $_POST["comment"];
}
if ($errors <= 0) {
$stmt = $conn->prepare("INSERT INTO FirstAttempt(client_id, domain, comment) VALUES(?,?,?)");
if ($stmt->execute(array(
$client_id,
$domain,
$comment
))) {
echo "New record created successfully";
} else {
// error in your code.
}
}
?>
NB: If we want to insert any data from external sources (like user input), it is very important that the data is sanitized and
validated.
Update :
<form action="yourform-processor.php" name="FirstAttempt" method="POST">
<font face="impact" color="white">Client ID:</font>
<input type="text" name="client_id" ><br>
<br>
<font face="impact"color="white">Domain:</font>
<input type="text" name="domain"><br>
<br>
<font face="impact" color="white">Comments:</font>
<input type="textarea" name="comment" style="width: 568px; height: 273px"> <br>
<br>
<input type="submit" value="Send" name="submit">
<input type="reset" value="Reset">
<br>
<br>
</form>
yourform-processor.php
<?php
$servername = "localhost";
$username = "Dylanc";
$password = "xxx";
$dbname = "FirstAttempt";
$errors = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submit'])) {
//first validate user input
if (empty($_POST['client_id'])) {
echo "enter client id";
$errors++;
} else {
$client_id = $_POST['client_id'];
}
if (empty($_POST['domain'])) {
echo "enter domain";
$errors++;
} else {
$domain = $_POST['domain'];
}
if (empty($_POST['comment'])) {
echo "enter comment";
$errors++;
} else {
$comment = $_POST['comment'];
}
if ($errors <= 0) {
//fields are not empty save to db
$sql = $conn->prepare("INSERT INTO FirstAttempt (client_id,domain,comment) VALUES(?,?,?) ");
$sql->bind_param("ssss", $client_id, $domain, $comment);
if ($sql->execute()) {
echo "New record created successfully";
} else {
//report bacck the error
}
}
$conn->close();
}
?>

$_SERVER['PHP_SELF'] doesn't work?

Everything works out just fine until I changed the below code from
<form method='post' action='re.php'>
to
<form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>
It gives me HTTP error 500. I have done some googling but still have no idea why this is happening! Thanks so much for help!
Below is the whole script
<?php
$hostname = 'localhost';
$username = 'Ciara';
$password = 'mypass';
$database = 'users';
$conn = mysqli_connect($hostname,$username, $password, $database);
if (!$conn) {
die ("Databas connect failed: " . mysqli_connect_error());
} else {
echo "Connected successfully. <br>";
}
echo <<<_END
<html>
<head>
<title>Register</title>
</head>
<body><center>
<form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>
Firstname: <input type='text' name='firstname'><br>
Lastname: <input type='text' name='lastname'><br>
E-mail: <input type='text' name='email'><br>
Gender: <input type='radio' value='female' name='gender'>Female
<input type='radio' value='male' name='gender'>Male<br>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' value='Submit Form'>
</form></center>
_END;
if (isset($_POST['firstname']) &&
isset($_POST['lastname']) &&
isset($_POST['email']) &&
isset($_POST['gender']) &&
isset($_POST['username']) &&
isset($_POST['password'])) {
$firstname = get($conn, 'firstname');
$lastname = get($conn, 'lastname');
$email = get($conn, 'email');
$gender = get($conn, 'gender');
$username = get($conn, 'username');
$password = get($conn, 'password');
$query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')";
if (mysqli_query($conn, $query)) {
echo "You have successfully registered! <br>";
} else {
echo "Failed to register." . mysqli_error($conn);
}
}
mysqli_close($conn);
echo "</body></html>";
function get($conn, $var) {
return mysqli_real_escape_string($conn, $_POST[$var]);
}
?>
You can't add PHP inside your HTML when you're using echo
<?php
$hostname = 'localhost';
$username = 'Ciara';
$password = '0950767';
$database = 'users';
$conn = mysqli_connect($hostname,$username, $password, $database);
if (!$conn) {
die ("Databas connect failed: " . mysqli_connect_error());
} else {
echo "Connected successfully. <br>";
}
?>
<html>
<head>
<title>Register</title>
</head>
<body><center>
<form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'>
Firstname: <input type='text' name='firstname'><br>
Lastname: <input type='text' name='lastname'><br>
E-mail: <input type='text' name='email'><br>
Gender: <input type='radio' value='female' name='gender'>Female
<input type='radio' value='male' name='gender'>Male<br>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' value='Submit Form'>
</form></center>
<?php
if (isset($_POST['firstname']) &&
isset($_POST['lastname']) &&
isset($_POST['email']) &&
isset($_POST['gender']) &&
isset($_POST['username']) &&
isset($_POST['password'])) {
$firstname = get($conn, 'firstname');
$lastname = get($conn, 'lastname');
$email = get($conn, 'email');
$gender = get($conn, 'gender');
$username = get($conn, 'username');
$password = get($conn, 'password');
$query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')";
if (mysqli_query($conn, $query)) {
echo "You have successfully registered! <br>";
} else {
echo "Failed to register." . mysqli_error($conn);
}
}
mysqli_close($conn);
echo "</body></html>";
function get($conn, $var) {
return mysqli_real_escape_string($conn, $_POST[$var]);
}
?>
Check this code :).

php /mysql form validation issue

so my php validation has two check functions, first it checks for an empty name or password entry and second if user enters unwanted characters while entering his name, the problem is that if the user does enter an unwanted character or numbers in the 'name' entry the form is still saved to the database which it should not, nothing is saved to the database if the users leaves both the name and password field empty and the error is shown, which means half of my validation check is working fine . can any one help me out ?
<?php
// Connect to data base
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// define variables and set to empty values
$nameErr = $userpasswordErr = "";
$name = $userpassword = "";
//check for error
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//validate name and password
if (
empty($_POST["name"]) ||
empty($_POST["userpassword"] ||
!preg_match("/^[a-zA-Z ]*$/",$name))
) {
$nameErr= "* Incorrect username or password ";
} else {
$name = test_input($_POST["name"]);
$userpassword = test_input($_POST["userpassword"]);
$sql = "INSERT INTO users(name,email)
VALUES ('$name','$userpassword')";
if ($conn->query($sql) === true) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<span class="error"></span>
<form method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
name : <input type="text" name="name" value="<?php echo $name;?>"/>
<span class="error"><?php echo $nameErr ;?> </span>
<br/><br/>
password : <input type="text" name="userpassword" value="<?php echo $userpassword ; ?>">
<span class="error"><?php echo $userpasswordErr ;?> </span>
<br/><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
The $name in regular expression check should be $_POST['name']
if(empty($_POST["name"]) || empty($_POST["userpassword"] || !preg_match("/^[a-zA-Z ]*$/",$_POST["name"])))
{
$nameErr= "* Incorrect username or password ";
}

Categories