Why I am not getting data in my database? - php

I am trying to POST my data in my database but unable to do... Which mistake I did here? I have simply try to print Query but it prints without any value....
If I did any mistake then let me know..
And suggest me what can I do now...
<?php
if(isset($_POST['firstname'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sms";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Could not connect to the database due to the following error --> ".mysqli_connect_error());
}
//echo "success";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$mobno = $_POST['mobno'];
$dob = $_POST['dob'];
$sql1="INSERT INTO `register`(`firstname`, `lastname`, `email`, `mobno`, `dob`) VALUES ('$firstname','$lastname','$email','$mobno','$dob')";
echo $sql1;
if($conn->query($sql1) == true){
// echo "Successfully inserted";
// Flag for successful insertion
$insert = true;
}
else{
echo "ERROR: $sql1 <br> $conn->error";
}
// Close the database connection
$conn->close();
}
?>

Related

How to query variable from database using php

Good Day developers outthere! 😊😊
I just wanna ask what is the problem with my code, I'm trying to make a webpage using html,css,php and database. Now I already created a php in my html form and my database is already connected, but everytime I submit the information in the html form I created, nothing appeared in my database.
<?php
if(isset($_POST['save'])){
$FName = $_POST['FName'];
$MName = $_POST['MName'];
echo "Successfully Added";
$sql= "INSERT INTO 'tbstudinfo' (Transaction_Number, First_Name, `Middle_Name') VALUES ('000',$FName,$MName)";
} else{
echo "<p>Insertion Failed.</p>";
}
?>
Just as #executable mentioned, you are defining query in your code but not executing it.
Define Connection Object (Mysqli, PDO..)
Prepare Query and Bind Variables
Execute your query
Here's an example using prepared statements
<?php
$servername = "localhost";
$username = "username";
$password = "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['save']) ){
// prepare and bind
$stmt = $conn->prepare("INSERT INTO 'tbstudinfo' (Transaction_Number, First_Name, Middle_Name) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $transaction_number, $FName, $MName);
// set parameters and execute
$transaction_number = '000';
$FName= $_POST['FName'];
$MName= $_POST['MName'];
$stmt->execute();
echo "Successfully Added";
}else{
echo "<p>Nothing Posted</p>";
}
W3Schools and PHP.Net both have pretty good examples about how to use prepared statements to make your SQL Query more secure from SQL Injections.
You simply don't execute your query. Using MySQLi :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "dbthesis";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['save'])){
$FName = $_POST['FName'];
$MName = $_POST['MName'];
$sql = "INSERT INTO tbstudinfo (Transaction_Number, First_Name, Middle_Name) VALUES ('000', '$FName', '$MName')";
if ($conn->query($sql) === TRUE) {
echo "Successfully Added";
} else {
echo "<p>Insertion Failed.</p>";
}
}
$conn->close();
You only making a query, not running query. This this code
$FName = $_POST['FName'];
$MName = $_POST['MName'];
$sql = "INSERT INTO tbstudioinfo (Transaction_Number, First_Name, Middle_Name) VALUES ('000','$FName','$MName')";
// code below runs your query
if (mysqli_query($conn, $sql)) {
echo "Successfully Added";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

No database selected - php and phpmyadmin

I am trying to connect my php code to my database and once I input and submit things like username, email address, and password in my html form. They will be sent to my database and shown in specific columns and there will be text like "New record has been created successfully" on my page. But for now, an error occurs instead.
The following is my php code
<?php
$visitorname = $emailaddress = $visitorpassword = $confirmpassword = "";
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$visitorname = input($_POST["visitorname"]);
$emailaddress = input($_POST["emailaddress"]);
$visitorpassword = input($_POST["visitorpassword"]);
$confirmpassword = input($_POST["confirmpassword"]);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
}
function input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if($visitorpassword == $confirmpassword){
// Create connection
$conn = new mysqli($servername, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$id = mysqli_insert_id($conn);
$sql = "INSERT INTO registereduser (id, username, emailaddress,
hashedpassword) VALUES ($id, $visitorname, $emailaddress,
$hashed_password)";
if ($conn->query($sql) === TRUE){
echo "New record created successfully";
}else{
echo "Errors " . $sql . "<br>" . $conn->error;
}
$conn->close();
}else{
echo "Passwords do not match";
}
?>
The following is the error output shown on my web page:
Connected successfullyErrors INSERT INTO registereduser (id, username, emailaddress, hashedpassword) VALUES (0, Josh5577, josh1998#hotmail.com, $2y$10$RMasEdVOskmcXbmfchLeBeUzLa5l38jXFaCQN7vMEhR8A0mU/iiC6)
No database selected
Please use below code for connection
$conn = new mysqli($servername, $username, $password, $dbname);
You can try to change
// Create connection
$conn = new mysqli($servername, $dbname);
to
// Create connection
$conn = new mysqli($servername, $username, $password);
mysqli_select_db($conn, $dbname);

Form data is not getting inserted in MySQL Database

Form data is not getting inserted in MYSQL . Form successfully posts data (I have checked with var dump). Please help me with this.
This is my action PHP.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname= "test";
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("connection failed:" .$conn->connect_error);
}
else{
echo "Connected successfully";
}
$selected = mysqli_select_db($conn,$dbname);
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
//$gender = $_POST['male'];
//$gender = $_POST['female'];
$sql = "INSERT INTO test1 (first_name,last_name,email,mob,home_address) VALUES ('$first_name','$last_name','$email','$mobile','$address')";
var_dump($sql);
?>
You are missing the last part which executes the query to save the data:
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

Inserting Form values into Database through PHP

This is my etaNavServer.php file. These are the values I have to insert into database. My table name is user and database name is srilanka, and these are the values I have to insert.
<?php if(isset($_POST['continue']))
{
$eta_type = $_POST['eta_type'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$title1=$_POST['title1'];
$sql="INSERT INTO user(applicationtype,surname,givenname,title,) VALUES
('$eta_type','$lastname','$firstname','$title1')";
$query = mysqli_query($con, $sql);
if($query){
echo "<h4 style='color:green'>Services Added Successfully.</h4>";
}
else
{
echo "Failed";
}
}
connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "srilanka";
$con = new mysqli($servername, $username, $password, $dbname);
?>
You have syntax error in
$sql="INSERT INTO user(applicationtype,surname,givenname,title,) VALUES
('$eta_type','$lastname','$firstname','$title1')";
remove extra , after title
$sql="INSERT INTO user(applicationtype,surname,givenname,title) VALUES
('$eta_type','$lastname','$firstname','$title1')";
Note: You have only closing brace } in connection, assuming this is just a typo
EDIT
$query = mysqli_query($con, $sql) or die(mysqli_error($con));

Data entered 2 times after run the query in database

I have faced another problem please help.
when i query to my db 1 time, 2 rows entered into the db!
Here is the code:
<?php
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];
$name = mysqli_real_escape_string($conn, $name);
$famil = mysqli_real_escape_string($conn, $famil);
$email = mysqli_real_escape_string($conn, $email);
$amount = mysqli_real_escape_string($conn, $amount);
$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";
mysqli_query($conn,$sql);
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
and the result after 2 times run this code:
3 mohsen gholi ***#yahoo.com 235354346
4 mohsen gholi ***#yahoo.com 235354346
5 mohsen gholi ***#yahoo.com 235354346
6 mohsen gholi ***#yahoo.com 235354346
Remove this line as said by Fred
mysqli_query($conn,$sql);
and just use:
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}

Categories