problem in insert query using mysql and php - php

The problem is in insert query.If we insert a single data in the db it is inserted for multiple time in that the primary key(auto increment) is increased but the value give by the user didn't store in db why?
<html>
<head>
<title>comment</title>
</head>
<body>
<?php
require('db.php');
?>
<form action="db.php" method="get">
<input type="textarea" name="textarea" rows="4" value="" >
<input type="submit" name="submit" value="submit">
</form>
<?php
$comment = isset($_GET['textarea']) ? $_GET['textarea'] : '';
$sql="INSERT INTO comment(comments) VALUES('$comment')";
mysqli_query($con,$sql);
?>
</body>
</html>
the actual table name is comment and the column name is comment_id and comments.
if a user ask some question in textarea(in HTML design) that should be inserted in comments column.

You should not submit the form to the same file you are requiring on line 7.
Rather submit the form to itself. Try this:
<html>
<head>
<title>comment</title>
</head>
<body>
<?php
require('db.php');
if(!isset($_GET['submit']))
{
?>
<form method="get">
<input type="textarea" name="textarea" rows="4" value="" >
<input type="submit" name="submit" value="submit">
</form>
<?php
} else {
$comment = isset($_GET['textarea']) ? $_GET['textarea'] : '';
$sql="INSERT INTO comment(comments) VALUES('$comment')";
mysqli_query($con,$sql);
}
?>
</body>
</html>
Also like #aynber mentioned in the comments, take advantage of prepared statements and bind_param, to secure your app a bit.

Related

Resetting the session variable in php

I have the following form on "test.php".
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "order.php" looks like this:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.
I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.
Change the new value to SESSION ON your order.php page like below:-
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
Also change test.php code like this:-
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

How can I generate 128 bar code from a text box in php?

I have a script of 128 barcode, and it is using random number to generate a barcode, how can I make this by using textbox instead of rand in php?
This my code
<!DOCTYPE html>
<html>
<head>
<title>asd</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$bcode = $_GET['id'];
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
include 'Code128.php';
$code = isset($_GET['code']) ? $_GET['code'] :$bcode;
header("Content-type: image/svg+xml");
echo draw($code);
}
?>
<form method="POST">
<input type="text" name="id">
<input type="submit" name="submit">
</form>
</body>
</html>
A textbox can't randomly generate a number...
I've solve the problem,
<form method="Get" action="barcode.php">
<input type="text" name="barcode">
<input type="submit">
</form>
barcode.php

Conflict On Two Post on Single Page Form(HTML) and PHP

I need to keep two forms for login and Logout in one single page including PHP and HTML like
<html>
<body>
<form action="" method="post">
<input type="text" name="username">
<input type="submit" name="SubmitButton" value="Get in"/>
</form>
<form action="" method="post">
<input type="submit" name="logoutButton" value="Logout"/>
</form>
</body>
</html>
on PHP part I have
<?php
if(isset($_POST['SubmitButton'])){
$inputuser = $_POST['username'];
$_SESSION['user'] = 'A';
}
if(isset($_POST['logoutButton'])){
unset($_SESSION['user']);
header('Location: http://somewhere.com/');
}
As I said I have to keep everything on one single page but this looks like causing conflict between the POST(s) can you please let me know how to stop this and target each Post properly?

I can't transport HTML information to PHP

My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>

Why does it always echo the "Not set!" though I enter data?

Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!

Categories