I can't fill in database table - php

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

Related

why apache2 server is not processing below php file

OS: Ubuntu, Apache2, mysql-server
I hosted one index.html on localhost in Apache2 server with one dc.php file
<!DOCTYPE html>
<head>
</head>
<body>
<form method="post" name="form1" action="dc.php">
Username <input type="text" name="username" id="username"/>
Password <input type="text" name="password" id="password"/>
<input type="submit" value="Submit">
</form>
<form method="post" name="form2" action="cancel.html">
</body>
and here is code for dc.php file
<?php
session_start();
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$mysqlPassword="mypass#"; // Mysql pass
$db_name="mydb1"; // Database name
$tbl_name="mytbl1"; // Table name
mysql_connect("$host", "$username","$mysqlPassword")or die("Cannot connect to MySQL database.");
mysql_select_db("$db_name")or die("MySQL database unavailable.");
$password=$_POST['username'];
$confirm=$_POST['password'];
if ($password != $confirm) {
header("location:error.html");
break;
}
mysql_query("INSERT INTO mytbl1(password, confirm) VALUES('$password', '$confirm');");
echo "Updating records... $password";
ob_end_flush();
?>
so when i clicking on submit button instead of executing php file, the browser stops with url
http://localhost/dc.php
with blank page, and the values are not inserted in mysql.
your action of form is dc.php so if you submit your form the page goes to dc.php , your problem because an error occurred in your code you can add this lines to first of your code to see what excatly make error [in dc.php]
error_reporting(E_ALL);
ini_set("display_errors", 1);

Not inserting the message into table

In my code below, it should clear the table Banner, which it does, then insert a message into it, which it does not
This is my code, thanks
<?php
error_reporting(-1);
mysql_connect('localhost', 'username', 'password');
mysql_select_db("induadmi_db");
if($_POST['submit']){
$bannermsg = $_POST['newBanner'];
mysql_query("TRUNCATE `Banner`");
mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
<html>
<body>
<form method="post">
<input type="text" id="newBanner" name="newBanner' placeholder="Enter Message"/>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
This is the error I get by the way:
Notice: Undefined index: newBanner in
/home/induadmi/public_html/por/newbanner.php on line 5
Here is Your Error In HTML
<html>
<body>
<form method="post">
<input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
name="newBanner' is Error name="newBanner"
your error means $_POST['newBanner'] has not been defined. so put $bannermsg = $_POST['newBanner']; inside if($_POST['submit']){ condition
try like this by checking whether $_POST exixts
<?php
error_reporting(-1);
mysql_connect('localhost', 'username', 'password');
mysql_select_db("induadmi_db");
if(isset($_POST['submit']))
{
$bannermsg = $_POST['newBanner'];
mysql_query("TRUNCATE `Banner`");
mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
try this
<?php
error_reporting(-1);
mysql_connect('localhost', 'username', 'password');
mysql_select_db("induadmi_db");
if (isset($_POST['submit'])) {
$bannermsg = $_POST['newBanner'];
mysql_query("TRUNCATE `Banner`");
mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
<html>
<body>
<form method="post">
<input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
You need to check the results of mysql_query to see if the call succeeded, and if it didn't, to check mysql_error to see what the error is.
http://us2.php.net/manual/en/function.mysql-query.php
http://us2.php.net/manual/en/function.mysql-error.php
You did not close the quotes properly on your <form> ..
name="newBanner' place
^----------
That is why you were getting the Undefined:index Notice.. Always make use of the isset construct.
Also...Your TRUNCATE syntax is wrong..
It should be
mysql_query("TRUNCATE TABLE `Banner`");
The fixed code..
<?php
if(isset($_POST['submit'])){
error_reporting(-1);
mysql_connect('localhost', 'induadmi_main', '$admiNiNd/U');
mysql_select_db("induadmi_db");
$bannermsg = $_POST['newBanner'];
mysql_query("TRUNCATE TABLE `Banner`");
mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
<html>
<body>
<form method="post" action="">
<input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, Prepared Statements of MySQLi or PDO_MySQL extension should be used to ward off SQL Injection attacks !

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.

nothing happens after check login script php jquery

I am trying to right a simple login script. When I execute the "checklogin.php" script (upon being called by the login form) - nothing happens. I stay on index.php, and I still see the form (which should have been removed by some jquery). If I open firebug I see this:
POST http://localhost/~Eamon/checklogin.php 200 OK 2ms jquery.js (line 8724)
GET http://localhost/~Eamon/templates/login_success.php 302 Found 1ms jquery.js (line 8724)
GET http://localhost/~Eamon/index.php 200 OK 1ms
GET http://localhost/~Eamon/reqscripts/jquery.js?_=1371173056318 200 OK 1ms jquery.js (line 8724)
GET http://localhost/~Eamon/js/application.js?_=1371173056319 200 OK 0
There aren't any errors - but I'm wondering if all those "GET" requests are correct. Should any of them be "POST"?
Here is my code:
index.php
<!DOCTYPE html>
<html>
<head>
<title>it IT</title>
<script src="reqscripts/jquery.js"></script>
<script src="js/application.js"></script>
</head>
<body>
<form id="login" method="post" action="checklogin.php">
<h1>Member Login</h1>
<p>Username:<input name="myusername" type="text" id="myusername"></p>
<p>Password:<input name="mypassword" type="password" id="mypassword"></p>
<input type="submit" name="Submit" value="Login">
</form>
</body>
</html>
checklogin.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql->bind_param('ss',$myusername,$mypassword);
$sql->execute();
$sql->store_result();//apply to prepare statement
$numRows = $sql->num_rows;
if($numRows === 1){
$_SESSION['username'] = $myusername;
}
else {
echo "Wrong Username or Password";
}
session_destroy();
?>
js/application.js
$(document).ready(function() {
$("#login").submit(function(e) {
e.preventDefault();
$.post("checklogin.php", $(this).serialize(), function(){
$("#showuser").load("templates/login_success.php");
$("#login").remove();
});
});
});
templates/login_success.php
<?php
session_start();
if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
Log out
</body>
</html>
UPDATE
Here is the source code from after an attempted log in. I will try to show why I think that $_SESSION['username'] is never being set (but why?)
<!DOCTYPE html>
<html>
<head>
<body>
<div id="showuser">
<title>it IT</title>
<script src="reqscripts/jquery.js">
<script src="js/application.js">
<form id="login" action="checklogin.php" method="post">
<h1>Member Login</h1>
<p>
Username: <input id="myusername" type="text" name="myusername">
</p>
<p>
Password: <input id="mypassword" type="password" name="mypassword">
</p>
<input type="submit" value="Login" name="Submit">
</form>
<form id="register" action="checkreglogin.php" method="post">
<h1>Member Registration</h1>
<p>
Username: <input id="rmyusername" type="text" name="rmyusername">
</p>
<p>
Password: <input id="rmypassword" type="password" name="rmypassword">
</p>
<p>
Email: <input id="myemail" type="text" name="myemail">
</p>
<input type="submit" value="Register" name="Submit">
</form>
<div id="showuser"></div>
</div>
</body>
</html>
Notice how the entire page is in the division...but there is also and empty "showuser" div at the bottom. This confuses me. I think that, when checklogin.php is called by the form - something goes wrong, and the user never gets saved in $_SESSION['username']. Therefore...when login_success.php gets called - $_SESSION['username'] is set to null...and index.php (all of it) loads on $.post request...because that is what would happen when $_SESSION['username'] is null according to login_success.php. Essentially...login_success.php becomes index.php somewhere along the way. Enlighten me!
Figured it out, thanks to #fattomhk for the comment. I had to put the session_destroy() line in checklogin.php into the else block...like so:
...
else {
echo "Wrong Username or Password";
session_destroy();
}
?>
The session was getting destroyed after the checklogin.php script ran - thus the session username was never being set...and login_success.php checks for a username before loading the page...if there is no username - it loads index.php (which is why the page wasn't changing upon log in).

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