Ok, so I have tested if the database is connected and it is. But, my problem is that the form won't go onto the output.php page to be able to save it to the database, I'm getting a NOT FOUND error. I have included a screenshot of the error, the code seems right to me but I can't figure out why it can't find the page when it's clearly in the FOLDER. Thanks for your time in advance.
form.php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
$fname = $surname = "";
?>
<h2>Fill out user details</h2>
<form action="output.php" method="post" >
First Name: <input id="fname" type="text" name="fname" >
<br><br>
Surname <input id="surname" type="text" name="surname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
output.php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php $fname=$_POST["fname"];
$surname=$_POST["surname"];?>
<!--Connect to the database-->
<?php
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully"; ?>
<?php
$sql="INSERT INTO 'user'.'user_tbl'('user_fname','user_surname') VALUES ('$fname','$surname')";
mysqli($conn,$sql);
?>
<?php
header("Location: http://localhost/mysite/");
?>
</body>
</html>
There are a few things wrong with your code.
You are using the wrong identifiers for your table and columns, being single quotes.
Use bacticks
$sql="INSERT INTO `user`.`user_tbl`(`user_fname`,`user_surname`) VALUES ('$fname','$surname')";
Then there is this line mysqli($conn,$sql); it should be mysqli_query($conn,$sql);
or
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
Then this
$conn = new mysqli($servername, $username, $password);
there should be a 4th parameter for it being for the database
$conn = new mysqli($servername, $username, $password, $database);
however, I see no variables set for any of these.
Here is an example taken from http://php.net/manual/en/function.mysqli-connect.php
$link = mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($link));
or using your variables and the one that was missing, and replacing with your own credentials
$servername = "localhost"; // or what your service provider said to use
$username = "username"; // your username
$password = "password"; // if one is needed. Leave blank if no password is needed
$database = "your_database"; // your database name
$conn = new mysqli($servername, $username, $password, $database);
Plus, your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
Checking for errors would have signaled the error, if you had used mysqli_query instead of just mysqli
or die(mysqli_error($conn)) to mysqli_query()
However, you are doing what is called "outputting before header" with HTML above your PHP
the header being
header("Location: http://localhost/mysite/");
you need to place your HTML under PHP.
Actually, you don't need the following tags for your SQL/PHP
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
and
</body>
</html>
Just the PHP/SQL.
Related
when i hit the submit button, nothing happens. perhaps the database is not connected. i am trying to make a form using php and html. i am using xampp, i wrote the code in notepad++ and i saved form.php in htdocs. i don't know what is wrong. maybe the names i used for the variables.
this is the html code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="C:\xampp\htdocs\form.php">
Nume de utilizator : <input type="text" name="nume_de_utilizator" placeholder="Enter Your Name" >
Email : <input type="text" name="email" placeholder="Enter Your Email">
Parola: <input type="password" name="parola">
<input type="submit" value="submit" >
</form>
</body>
</html>
this is form.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "autentificare";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$nume_de_utilizator = mysqli_real_escape_string($conn, $_POST['nume_de_utilizator']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$parola = mysqli_real_escape_string($conn, $_POST['parola']);
// Attempt insert query execution
$sql = "INSERT INTO utilizatori (nume_de_utilizator, email, parola) VALUES ('$nume_de_utilizator', '$email', '$parola')";
if(mysqli_query($conn, $sql))
printf("%d Row inserted.\n", mysqli_affected_rows($con));
else
{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);}
// Close connection
mysqli_close($conn);
?>
and this is the "autentificare", the database
my database
Try to see the "online link"
eg: "http://localhost:8080/form.php"
Do a simple echo msg - file to check and after replace
action="C:\xampp\htdocs\form.php" with action=http_link
Here is a my html form code: The problem is that i can't figure out how to succesfuly submit the form to mysql dtbs using xampp. (Data aren't sent to dtbs).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" value="Submit">
</body>
</html>
and now my php code:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Guests (firstname)
VALUES ('?')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Data is not sent in the mysql dtbs! i've been trying for 2 days solving this but nothing... please help!
Kind regards, Thanos
$sql = "INSERT INTO Guests (firstname) VALUES ('?')";
'?' is to substitute in an integer, string, double or blob value.
You placed the '?', but forgot to prepare it using bind_param. More importantly, you have to pass $firstname value into $stmt->bind_param("s", $firstname);
Updated Code
$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);
if ($sql->execute() === TRUE) {
Read
Prepared Statements in MySQLi
how to insert into mysql using Prepared Statement with php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" name="submitForm" value="Submit">
</body>
</html>
**test.php file**
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitForm'])){
$firstname = $_POST['firstname'];
$sql = "INSERT INTO Guests (firstname)
VALUES ('{$firstname}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}else{
echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
}
$conn->close();
?>
For some reason I am getting "Notice: Undefined index: adata" when I try to input data and mysql data file gets a blank space input (no text input just an empty field.
So can you tell me guys what I did wrong, because I watched multiple tutorials, guide and read multiple posts regarding this matter yet still I could not resolve it.
<?php
if (isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$dbcon = mysqli_connect($servername, $username, $password, $dbname);
mysql_select_db($db);
// Check connection
$adata = $_POST['test'];
$sql = ("INSERT INTO data (data) VALUES ('$adata')");
if (!mysqli_query($conn, $sqlinsert)) {
die('error inserting new data');
} //end of second if
} //end of if statement
?>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Data Into DB</title>
</head>
<body>
<h1>Insert new data into DB</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Add Data:
<input type="text" name="test">
<br />
<input type="submit" value="add new data" />
</form>
</body>
</html>
Be careful with your variables ;
$adata = $_POST['test'];
$sql = ("INSERT INTO data (data) VALUES ('$adata')");
if (!mysqli_query($conn, $sql)) { // it was $sqlinsert and must be $sql due your definition.
die('error inserting new data');
I am on a localserver learning mysql and php on my local server MAMP, below i will put the code to all three of my files index.php, signup.php and dbh.php. I am trying to create a log in system but it does not send the information in the database but i do not get an error message either when running it runs fine but does not add the information that i entered in the sign up form.
My database name is drugcity and my username is drugcity_data and my password is admin
So please help been trying for hours!
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Drug City</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="signup.php" method="POST">
<input type="text" name="usrname" placeholder="Username Here"><br>
<input type="password" name="pwd" placeholder="Password Here"><br>
<button>Sign Up</button>
</form>
</body>
</html>
signup.php
<?php
include 'dbh.php';
$username = $_POST['usrname'];
$pass = $_POST['pwd'];
$sql = "INSERT INTO users(usrname, pwd) VALUES ('$username','$pass')";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
?>
dbh.php
<?php
$user = 'drugcity_data';
$password = 'admin';
$db = 'drugcity';
$host = 'localhost';
$port = 8889;
$conn = mysqli_connect($link, $host, $user, $password, $db, $port);
?>
I think, you have wrong connection to DB.
You have
$conn = mysqli_connect($link, $host, $user, $password, $db, $port);
Why you have there $link variable? Try it without $link, so
$conn = mysqli_connect($host, $user, $password, $db, $port);
Try removing $link from your $conn = mysqli_connect($link, $host, $user, $password, $db, $port);
The first variable of mysqli_connect should be your host.
Now your host is empty so it doesn't know what to connect to..
I need to get a user's comment and store in the database table column 1 and display the entered comment in different table. The code works fine with no errors, but the comment does not get stored in the database.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if (isset($_POST['Submit'])) $Comment = isset($_POST['Comment']) ? $_POST['Comment'] : '';
$sql = "INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res = $conn->query($sql);
if ($res) {
echo "Successful";
echo "<br />";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
</body>
</html>
try this.Notice the opening and closing bracket.
<head>
<title></title>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "escalation";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}else {
echo "ERROR";
}
}
?>
</body>
</html>
Another info and a sort of advice though it does not concern to your question is please use prepared statement that will help prevent sql injection.
You can read php manual about mysqli prepared statement here .
You might also want to check PDO prepared statement click here.
You might also want to check this this full helper class for your crud operation that i personally created.That also uses PDO prepared statement.
Hope that helps somebody.
Try this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server="localhost";
$username="root";
$password="";
$database="escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
}
?>
</body>
</html>
Did you check if the $_POST is giving the output you want. If yes ,then try the mysql command on the commandline and see if it shows any error ... Your command should work, i tried it in my commandline, it worked.
Either you havent set the correct data types and lengths of values in the database table or try this:
A safe command which always works is
"INSERT INTO `css`(`Dis_Cmt`)VALUES('.$Comment.')"
First thing I notice:
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
should be
(!$conn)
Put a echo before the INSERT. If its show up, the problem maybe the
query or the table field;
Insert any value in css table using the
phpmyadmin for testing;
Use prepared statement to avoid sql
injection. Below, a example.
$Comment = $_POST['Comment'];
$sql = "INSERT INTO css (Dis_Cmt) VALUES (?)";
$statement = $conn->prepare($sql);
$statement->bind_param('s', $Comment);
if($statement){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
$statement->close();
Good luck!