database query redirecting to home directory - php

I have the simplest database entry that when I hit "submit", it takes me to my home directory and nothing is added to the database. How do I fix it so it inputs the data? I haven't had any problems like this in the past using the same code...
<?php
if(isset($_POST['submit'])) {
$text = $_POST['text'];
$link = mysql_connect("localhost", "******", "********") or die("Couldn't make connection.");
#mysql_select_db("*****") or die("Couldn't select database");
mysql_query("INSERT INTO wall (message) VALUES ('$text')");
}
?>
<html>
<form action="post" name="post" id="post">
<input type="text" id="text" name="text"/>
<input type="submit" name="submit" id="submit" value="submit"/>
</form>
</html>
I know my password and database name are correct, because when I take away the "form" and "isset", it uploads no problem every time I reload the page:
<?php
$link = mysql_connect("localhost", "******", "*******") or die("Couldn't make connection.");
#mysql_select_db("********") or die("Couldn't select database");
mysql_query("INSERT INTO wall (message) VALUES ('test')");
?>

change
<form action="post" name="post" id="post">
to
<form method="post" name="post" id="post">
Action is the page you want the form to submit to
Method is the type (which defaults to get)

Your form<> has no method, try this:
<form method="post" name="post" id="post">
</form>

Related

Proper way to check if URL parameter is empty?

The code works perfectly when I delete this right here:
if (empty($_GET['tag'])){
header("Location: http://www.home.com");
}
I'm trying to say if the url (tag=) is empty then head back to homepage.
This is what the search button looks like:
if (isset($_POST['submit-search'])){
$conn = mysqli_connect("localhost", "root", "", "testdb");
$search = mysqli_real_escape_string($conn, $_POST['search']);
header("Location: tags?tag=".$_POST['search']."");
}
When I do reinsert that code above (if(empty)) it keeps redirecting to the home page.
Here is the HTML:
<form action="tag" autocomplete="on" method="POST">
<input id="search" name="search" type="text" placeholder="Search for tags...">
<input id="search_submit" name="submit-search" type="submit">
</form>

PHP MySQL not submitting or working

I have some PHP and HTML code which should send data from the form to my MySQL database. However, on clicking Submit in the form, the page reloads and nothing happens. No echo or anything. The HTML is in the same file as the PHP file.
PHP
<?php
if(isset($_POST['submit'])){
$usernamep = $_POST['usernameinput'];
$passwordp = $_POST['passwordinput'];
$servername = "localhost";
$username = "USERNAMECENSOR";
$password = "PASSWORDCENSOR";
$dbname = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO accounts (username, password)
VALUES ('$usernamep', '$passwordp')";
// use exec() because no results are returned
$conn->exec($sql);
echo "Success";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
HTML
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>
Note: I know this code is currently subject to SQL injection, and the password is not encrypted. It is temporary starting code in an attempt to get it working first.
You lack the name attribute in the submit button, add name="submit".
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" name="submit" class="button" value="Sign in">
</form>
Your test is wrong. You have no input named "submit" (with attribute name = submit). It should be:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
...
}
A broad test like this will not even need a named submit. The HTML form may even be posted on another event using jQuery or JavaScript.
In your PHP file, you use isset($_POST['submit']). You don't have any form input with name="submit". You could make your submit button be
<input type="submit" name="submit" class="button" value="Sign in">
You have to include your php file into the action of the form tag.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>

Inserting input field value to database and displaying result on same page

I am starting to learn the basics of SQL and PHP codes.
I am trying to create a simple newsletter subscription page. this would require the user to enter their email and submit. That will then get added to my database.
Now, this works well when the HTML and PHP code are separate and the submission occurs but redirects to the PHP page with the echo.
I want to get the message on the same page and I tried merging the PHP code in the page as below
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
mysql_connect("hostname", "username", "password");
mysql_select_db("db name");
$user = $_POST['email'];
$query = "INSERT INTO tablename(columname)VALUES('$email')";
echo "inserted";
}
?>
<html>
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
</html>
Hoever with this code it just doesnt do anything.
What have am I doing wrong here? Appreciate your expert advice.
There are few mistakes in the code, you can fix them by doing the following:
Save the file as a php file first. For example name it "email.php".
Make the form action="email.php"
Don't write two complete separate codes in the same file, one for php file and the other for html file like you did. You can include the html code inside the php code using heredoc syntax which allows you to include a long html code like the following:
echo<<<_HTMLCODE
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
_HTMLCODE;
In the query syntax, add $user instead $email because the variable $user contains the value submitted by the form.
Add a code to excute the inserted query. for example:
mysql_query($query);
So your final code will be like this:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
mysql_connect("hostname", "username", "password");
mysql_select_db("db name");
$user = $_POST['email'];
$query = "INSERT INTO tablename VALUES('$user')";
mysql_query($query);
echo "inserted";
}
echo<<<_HTMLCODE
<form method="POST" action="email.php" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
_HTMLCODE;
?>
I have tried the code above after I added the data of my database on the localhost and after I created a table for the emails and it worked. Here is the edited code with my database access info and the table name in my code editor:
When i opened the table emails in my database, I found the email that I had submitted using the form (your modified code):
(advice: use mysqli instead of mysql)
Please use prepare statements to prevent Sql Injections.
Here is sample code try this.
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connect = new mysqli ("localhost", "root", "usbw", "test");
if (mysqli_connect_errno()) {
echo 'Failed to connect to MySQL:' . mysqli_connect_error();
}
if (isset($_POST['submit'])) {
$email = filter_input(FILTER_VALIDATE_EMAIL, $_POST['email']);
$sql = "INSERT INTO table (email) VALUES (?)";
$stmt = $connect->prepare($sql);
$stmt->bind_param('s', $email);
$result = $stmt->execute();
if ($result) {
$msg = 'Succesfully added';
} else {
$msg = 'OOPS Error Occured';
}
}
?>
<html>
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
</html>

I can't fill in database table

I want to make a database which I can fill from my webpage. I want it to be an Integer input. I created a database and everything.
I connected php and phpmyadmin, but when I insert a number it adds an empty row (null).
Where did I go wrong?
This is the whole code...
This is the Insert.html
<html>
<head>
<title>SITE</title>
</head>
<body>
<form action="insert.html" method="post">
Value1: <input type="text" name="field1-name" />
<input type="Submit" /></form>
</body>
</html>
This is Connect.php
<?php
$username="root";
$password="";
$database="test";
$field1-name=$_POST['Value1'];
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO temp VALUES('$field1-name')";
mysql_query($query);
mysql_close();
?>
And this is Test.php
<?php
include 'insert.html';
include 'connect.php';
$a=$_POST["temp"];
$sql="INSERT INTO temp(val) values ($a);";
?>
One issue you have it with your choice of variable names. You cannot have a dash in your variable name. So $field1-name is an invalid name. You should either remove the dash or change it to an underscore.
You also named your input field1-name but you try to access $_POST['Value1']. Where does Value1 come from?
First fix your form so your input name does not have a dash:
<form action="insert.html" method="post">
Value1: <input type="text" name="field1name" />
<input type="Submit" /></form>
Then fix your variable assignment:
$field1name=$_POST['field1name'];
try changing your form action and form field name. on html file you can't get post values so change it to connect.php
<html>
<head>
<title>SITE</title>
</head>
<body>
<form action="Connect.php" method="post">
Value1: <input type="text" name="field1_name" />
<input type="Submit" /></form>
</body>
</html>
This is Connect.php
<?php
$username="root";
$password="";
$database="test";
$field1_name=$_POST['field1_name'];
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO temp VALUES('$field1_name')";
mysql_query($query);
mysql_close();
?>
In Connect.php you use:
$field1-name=$_POST['Value1'];
There are two problems with that. The variable name might be invalid and $_POST['Value1'] is always empty because the form field is named "field1-name".
Try this:
$value = $_POST['field1-name'];
...
$query = "INSERT INTO temp VALUES('$value')";
Simply put connect.php in action and do whatever you want in connect.php
<html>
<head>
<title>SITE</title>
</head>
<body>
<form action="connect.php" method="post">
Value1: <input type="text" name="field1-name" />
<input type="Submit" /></form>
</body>
</html>
And Connect.php
$username="root";
$password="";
$database="test";
$fieldname=$_POST['field1-name'];
mysql_connect('localhost',$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO temp VALUES ('".$fieldname."')";
mysql_query($query);

How to use action to self page

i am trying to insert the values to table through form.
Here is my code which i have used
Code for insert.php
<?php
// Make a MySQL Connection
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
// Insert a row of information into the table "language"
mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ")
or die(mysql_error());
echo "Data Inserted!";
?>
Html form
<html>
<head></head>
<body>
<form action="insert.php" method="post">New Language:
<input type="text" name="language">
<input type="submit" value="Create Language">
</form>
</body>
</html>
But i dont want to use the insert.php file, instead i want to use the php code in insert.html. i tried using
<form action="">
and also
<form action="#">
and
<form action="<?php echo $PHP_SELF;?>">
but it is not working.
Php code will not run in .html files.
However you can output html in php files.
You could also rewrite your php page to have an html suffix using htaccess
PHP code will not work in .html files.. Better you change insert.html to insert.php and dont give any action in the form.So the self action will be executed and so the form will be submitted to insert.php itself.
<html>
<head></head>
<body>
<form action="" method="post">New Language:
<input type="text" name="language">
<input type="submit" name='save' value="Create Language">
</form>
</body>
</html>
And in php
<?php
if(isset($_POST['save'])){
// Make a MySQL Connection
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
// Insert a row of information into the table "language"
mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ")
or die(mysql_error());
echo "Data Inserted!";
}
?>
Combine two files:
You achive .php files like .html with .htaccess
For example: form.php
<?php
if (isset($_POST)) {
// Make a MySQL Connection
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
// Insert a row of information into the table "language"
mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ") or die(mysql_error());
echo "Data Inserted!";
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
New Language: <input type="text" name="language">
<input type="submit" value="Create Language">
</form>
</body>
</html>
either
<form>
and
<form action="">
obviously works.

Categories