MySQL after insert check the first 14 characters - php

I am trying something to check the barcodes from the parts, because of the risk of mixing.The barcodes is made up 24 characters, the first 14 characters is the part number as the last 10 characters is the counter, is it a solution to check the first 14 characters?
Here is an example of barcode: 308739420D52AD1702600131
Mixing parts is because next barcode that is from another part:
308739420B0LAD1702600131
Therefore, my question is: Can I check when entering the first 14 characters in the database?
I can say so show me the script that I use today for introducing barcodes in the database:
<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<h1>Barcode checker</h1>
<div id="login"><hr/>
<form action="" method="post">
<label>Barcode :</label>
<input type="text" name="TAK0010barcode" id="name" required="required" placeholder="Please enter barcode"/><br /><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
}
?>
</body>
</html>
Thank you.

You could use PHP function substr():
$control = '308739420D52AD';
$test = substr($_POST["TAK0010barcode"], 0, strlen($control));
if($control == $test){
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
// etc...
Documentation: http://php.net/manual/en/function.substr.php
UPDATE: Extended example, replace all within your php code with this:
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$control = '308739420D52AD'; // YOUR BAR CODE IDENTIFIER
$test = substr($_POST["TAK0010barcode"], 0, strlen($control)); // BEGINNING OF THE POSTED BAR CODE.. SET TO SAME LENGTH AS BAR CODE IDENTIFIER
if($control == $test){ // CHECK IF STRINGS ARE ALIKE
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
} else { // STRINGS DO NOT MATCH!
echo "<span style='color: red; font-size: 40px; font-weight: bold'>The barcode don't match the part number. Please use a barcode starting with ".$control."</span>";
}
}
?>

Related

Connect php with phpmyadmin

I'm building a simple user login page when I put in my username and password nothing happens, it seems like its not connected to the database
here's my html code with some CSS and my php code that is below this code:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>Intacs Login</title>
<!-- inner css -->
<style>
#login_logo {
margin: 15px 10px 10px 0;
display: block;
margin-left: auto;
margin-right: auto;
}
.container{
background-color: white;
border-radius: 30px;
border-color: lightgray;
border-style:outset;
width: 320px;
margin-top: 30px;
margin:auto;
padding-top: 50px;
padding-bottom: 50px;
}
#login_table{
margin-top: auto;
margin-left: auto;
margin-right: auto;
}
#login_remember{
text-align: center;
}
</style>
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="/js/jquery-3.3.1.slim.min.js"></script>
<script src="/js/popper.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<!-- HTML code -->
<div class="container">
<div id="login_form">
<div id="login_block">
<img id="login_logo" src="img/Intacs Master Logo.jpg"
width="187" height="63"> <!--div container-->
</div>
</div>
<table id="login_table" cellspacing="1" cellpadding="3" border="0">
<tbody>
<tr>
<form method="post" action="connect.php"> <!--form to connect to php file called connect.php -->
<th>Username: </th>
<td>
<input type="text" name="username" id="usernamefield" class="loginfield" validate="/^[a-z0-9_]{2,}$/i" valmsg="Please enter a valid username." value="" size="14" maxlength="32">
</td>
</tr>
<tr>
<th>Password: </th>
<td nowrap="nowrap">
<input type="password" name="password"
id="passwordfield" class="loginfield" validate="/^[^\s]{4,}$/"
valmsg="Please enter a valid password." value="" size="14"
maxlength="32">
</td>
</form>
</tr>
</tbody>
</table>
<div id="login_remember"><input type="checkbox" name="remember" value="1"> Remember me</div>
<br />
<center><input type="submit" value="Submit"></center>
</body>
</html>
<?php
function Db() {
$host = "localhost:8888";
$username = "root";
$password = "";
$db = "intacslogin";
$conn = new mysqli($host, $username, $password, $db);
if(!$conn){
die("Could not connect");
}
}
if(isset($_POST['login'])){
$uid = trim ($_POST['username']);
$pwd = trim($_POST['password']);
if($uid ==""){
$err[] = "Username is missing";
} else if($pwd == ""){
$err[] = "Password is missing";
} else{
$db = Db();
$uid = $db->real_escape_string($uid);
$pwd = $db->real_escape_string($pwd);
$sql = "SELECT * FROM users
WHERE username = '$uid'
and password = '$pwd'";
$result = $db->query($sql);
}
}
?>
Please Help thanks and happy coding thanks for your help.
Its a small personal project
<?php
//THIS FUNCTION SHOULD RETURN A CONNECTION TO BE USED IN A QUERY
function Db() {
$host = "localhost:8888";
$username = "root";
$password = "";
$db = "intacslogin";
//MAKE SURE U CONFIRM A SUCCESSFULL CONNECTION LIKE SO
if ($conn = new mysqli($host, $username, $password, $db)) {
return $conn;
}else {
return false;
}
//if(!$conn){
// die("Could not connect");
//}
}
if(isset($_POST['login'])){
$uid = trim ($_POST['username']);
$pwd = trim($_POST['password']);
if($uid ==""){
$err[] = "Username is missing";
} else if($pwd == ""){
$err[] = "Password is missing";
} else{
if (!$conn) {
//you can die() becouse it returned false
}else {
//proced when we a valid connection
//dont use $db use $conn
$uid = $conn->real_escape_string($uid);
$pwd = $conn->real_escape_string($pwd);
$sql = "SELECT * FROM users
WHERE username = '$uid'
and password = '$pwd'";
$result = $conn->query($sql);
}
}
}
?>

Need help styling PHP in HTML

This is my PHP coding from which I am grabbing data from my DB with a select where statement and outputting it. I am trying to change the text properties such as font, colour and size on the print statement can anybody assis with how to do this?
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
print " - Name: " . $row["fname"]. " " . $row["sname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
You can style lists using HTML <ul> and <li> elements.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Stylists</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<ul>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<li>Name: " . $row["fname"]. " " . $row["sname"]. "</li>";
}
echo "</ul>";
} else {
echo "0 results";
}
mysqli_close($conn);
?></body>
</html>
And in css/style.css put something like this:
li {
color: red;
font-weight: bold;
font-size: 12pt;
}
You can also put the style inline in the head section:
<style type="text/css">
li {
color: red;
font-weight: bold;
font-size: 12pt;
}
</style>
You can output your markdown like this:
print "<div class = 'sampleclass'> - Name: " . $row["fname"]. " " .
Then style it by including the below in an attached css file
.sampleclass {
font-size: 15px;
}
This is only about HTML and CSS, you could output directly your strings in HTML tags with echo. The script needs to be encapsulated into the HTML's body and a pre-defined CSS classes for styling:
<!doctype html>
<html>
<head>
....
<style type="text/css">
.redBold {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<?php
....
echo '<span class="redBold">This text is red and bold</span>';
?>
</body>
</html>
Of course you can also use inline styling:
<span style="color: red; font-weight: bold;">Also this works</span>
You can always use CSS file and define the values.
For example:
body {
font-family: arial;
font-size: 15px;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
php txt
</body>
</html>
Also, you can use <span> tags and style them.
For example print this span using 'print' of php:
<span style="font-family: arial; font-size: 15px;">text</span>

Display search results on the same page in a table with PHP

I am using a form to search in a database and I would like to know how to display the search results in a table, on the same page (the page can refresh, I don't mind).
My form looks like this:
<form id="searchform" method="post" action = 'search4.php' target = '_blank'>
<input id="name" style="height: 25px; width: 140px; position: fixed; top: 150px; left: 50px" name="name" type="text" >
<input type="submit" value="Search" class="btn btn-primary btn" style="color: white; font-style: normal; background-color: blueviolet; position: fixed; top: 148px; left: 220px">
</form>
search4.php is the script that does the searching in the database and looks like this:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'official_db';
$mysqli = new mysqli($servername, $username, null, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
if (!get_magic_quotes_gpc() ) {
$Name = addslashes($_POST['name']);
} else {
$Name = $_POST['name'];
}
session_start();
$results = "SELECT * FROM b2b_interfaces WHERE Name LIKE CONCAT ('%', $name, '%')";
$resultSet = $mysqli->query($results);
$numRows = $resultSet->num_rows;
if ($numRows > 0) {
while ($row = $resultSet->fetch_object()) {
echo "{$row->name} {$row->address} {$row->county} <br>";
}
} else {
echo "No Results";
}
?>
In the main script I also have defined a table, but I do not know how to have access to the results from search4.php. I would try something like this:
<tbody>
<?php
if ($numRows > 0) {
while ($row = $resultSet->fetch_object()) {
?>
<tr>
<td><?php echo "{$row->name} " ?></td>
<td><?php echo "{$row->address} " ?></td>
<td><?php echo "{$row->county} " ?></td>
</tr>
<?php
}
}
?>
</tbody>
You can place the search script on the same page, so target the search form to the current page, and place the script on top of the page.

Updating a PHP variable through HTML

I want to be able to change the PHP variables $dispname and $banstat in the code below with a HTML form.
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
$banstat = '1';
$dispname = "Brendan";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE display_name=$dispname";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
<li id="cid_4" class="form-input-wide" data-type="control_head">
<div class="form-header-group">
<div class="header-text httal htvam">
<h2 id="header_4" class="form-header">
Ban Tool
</h2>
</div>
</div>
</li>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> method="post">
Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>
<input type="submit">
</form>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
</ul>
</div>
<input type="hidden" id="simple_spc" name="simple_spc" value="51970718174158" />
<script type="text/javascript">
document.getElementById("si" + "mple" + "_spc").value = "51970718174158-51970718174158";
There were some multiple issues in your code.
Try
<?php
if (isset($_GET['p']) && $_GET['p'] == "login")
{
/* Database Connection Info */
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
/* getting form values */
$banstat = mysqli_real_escape_string($conn, $_GET['banstat']);//'1';
$dispname = mysqli_real_escape_string($conn, $_GET['dispname']);
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url='$banstat' WHERE display_name='$dispname'";
if ($conn->query($sql) === TRUE)
{
echo "Record updated successfully";
}
else
{
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="get">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
<li id="cid_4" class="form-input-wide" data-type="control_head">
<div class="form-header-group">
<div class="header-text httal htvam">
<h2 id="header_4" class="form-header">
Ban Tool
</h2>
</div>
</div>
</li>
Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
<input type="submit">
</form>
</ul>
</div>
Errors in your code
You open two forms.
You are using post method in form but using get method in php code.
Your variables were not updating.
You need get $_POST and set variable
$dispname = $_POST['name'];
Errors in your code:
You open two forms and just close one
If you want use post instead get check $_POST
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST) {
//update variables and update bd
}
}
?>
Okay, so I took both of the other answers provided from #javi and #Hassan and used them to re-write it, I took the majority of the code from Hassan's and then changed a few things using Javi's and put this together. I figured maybe someone else could use this. Thanks again guys.
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST) {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$banstat = $_POST['banstat'];
$dispname = $_POST['dispname'];
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE user_login='$dispname'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
Display Name: <input type="text" name="dispname"><br>
Ban Status: <input type="text" name="banstat"><br>
<input type="submit">
</form>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
</ul>

No database selected: login php code

<?php
session_start();
$con=mysqli_connect("localhost","xxx","xxxxxx","xxx");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$eadd = $_POST['eadd'];
$pass = $_POST['pass'];
$eadd = htmlspecialchars(stripslashes(strip_tags($eadd)));
$pass = htmlspecialchars(stripslashes(strip_tags($pass)));
if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
if(!$sql){
die('There was an error in query '. mysql_error());
}
$count = mysql_numrows($sql) or die(mysql_error());
if ($count<=0)
{
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Incorrect Email Address and Password! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
else
{
//have them logged in
$_SESSION['account'] = $eadd;
header('location:home.php');
}
mysqli_close($con);
} else {
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Invalid Email Address! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
?>
Why there is an error "No database selected"? My mysqli_connect is correct. I have another register php code, using which I can register some email address using that connection.But here in login php code, I can't login with the user email address.
From the above code its look like you are using mysqli_connect for database connection and mysql_query for query execution. Use mysqli_query instead of mysql_query . Like this
mysqli_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
Actually, you need to select a database after authentication.
We need to follow these steps while making a database connection.
Create a connection
Select the database
Fire the query
Use query result
Close the connection
Sample Code is here
Firstly, You need to add this line in your code after login
$db_select = mysql_select_db("myDatabase", $con);
Secondly, you need to pass $con as second parameter after selecting your database So your query statement becomes like this
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'", $con);

Categories