How to use action to self page - php

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.

Related

problem in insert query using mysql and 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.

PHP Submit button doesn't have any effect (PhpStorm)

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

PHP Checkbox from db, unchecked after submit

I have this code. I need that all the checkbox (taken by database) that I choose remain checked even after submitting the page.How can I do that?
?>
<?php
function connetti(){
$conn=mysql_connect("localhost","user","pass");
mysql_select_db('colours');
return $conn;
}
?>
<html>
<head>
<title>Scelta colori</title>
<meta charset="utf-8">
</head>
<body>
<h1>Scelta colori</h1>
<h1>Benvenuto <?php echo $_SESSION['user']; ?></h1><br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
$conn=connetti();
$sql="SELECT tonalita FROM colori";
$risultato=mysql_query($sql);
while ($row=mysql_fetch_array($risultato)){
$valore=$row['tonalita'];
echo('<input type="checkbox" value="'.$valore.'" name="colori[]">'.$valore.'</input><br>');
}
mysql_free_result($risultato);
mysql_close($conn);
?>
<input type="submit" value="Invia">
<input type="reset" value="Annulla">
</form>
</body>
</html>
u can use, after submitting the form, the $_POST vars and check it with $valore
<?php
echo('<input type="checkbox" value="'.$valore.'" name="colori['.$valore.']"'.((isset($_POST["colori"][$valore])&&$_POST["colori"][$valore]==$valore)?' checked="checked"':"").'>'.$valore.'</input><br>');
?>
untestet but should work
edit: got the error on the name-attr and fixed issue on not settet index. testet on php5

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);

database query redirecting to home directory

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>

Categories