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);
Related
i am able to enter text in html and am able to submit. but then i get a blank page. whats wrong? is it the code?
here it is
<html>
<body>
<form action ="students.php" method="post">
USN : <input type="text" name="id">
NAME : <input type="text" name="n">
age : <input type="text" name="a">
<input type="submit">
</form>
</body>
</html>
and php code is
<html>
<body>
<?php
$connect=mysqli_connect("localhost","root","1234") or die("cant connect");
mysqli_select_db($connect,"student") or die("cant connect to database");
$usn=$_POST["id"];
$name=$_POST["n"];
$age=$_POST["a"];
$query="insert into student_info (usn,name,age) values('$usn','$name','$age')";
if(mysqli_query($connect,$query))
{
echo "inserted";
}
else
{
echo "could not insert";
}
mysqli_close($connect);
?>
</body>
</html>
as stated in this answer :
https://stackoverflow.com/a/21429652/7091942.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
You are validating incorrectly... You can use mysqli_affected_rows to check if there was a change in the database, instead of putting the query in an if statement, since that shouldn't work.
Also, consider using error_reporting(E_ALL); and look up sanitation, to secure your queries for sql-injection.
I need some help. I have been developing a form which updates a database. It was all working fine using the jQuery Mobile form and then suddenly it has all stopped working. I have stripped the form down the the bare minimum and still its not working. When I click on update the information is placed the the browser as shown below.
update_db.php?niamh=1
but the database is not updated. If I click on refresh it updates and displays success.
If I remove all the jQuery header links it all works ok, so this is a jQuery problem. Sadly this was all working ok a couple of hours ago. code below, can any one please help.
HTML form
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form action="update_db.php" method="GET">
<input type="checkbox" data-role="flipswitch" name="niamh" id="switch" value="1">
<input type="submit" data-inline="true" value="Update">
</form>
</body>
</html>
php update_db.php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="14Odiham"; // Mysql password
$db_name="heating"; // Database name
$tbl_name = "roomControl";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$niamh = (isset($_GET['niamh'])) ? 1 : 0;
// Insert data into mysql
$sql = "UPDATE $tbl_name SET niamh=$niamh WHERE id=1";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>
<?php
//close connection
mysql_close();
?>
Try:
<form action="update_db.php" method="GET" data-ajax="false">
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);
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).
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>