Inserting Form values into Database through PHP - 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));

Related

Why I am not getting data in my database?

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();
}
?>

Can't make CMS admin for my blog

This is the php program I created to insert data into the database.
<?php
include '../includes/config.php';
//Input Data Process
$row = mysqli_fetch_array($query);
if (isset($_POST["post"])) {
$title = $_POST["title"];
$description = $_POST["description"];
$article = $_POST["article"];
mysqli_query($conn, "INSERT INTO post VALUES('','$title','$description','$article')");
header("location:index.php?article");
}
$query = mysqli_query($conn, "SELECT * FROM post");
?>
But when I hit the post button nothing happens in the database.
This is config.php file.
<?php
//Database Connection
global $conn;
$servername = "localhost";
$username = "root";
$password = "";
$db = "my_blog";
$conn = mysqli_connect($servername, $username, $password, $db);
//Check Connection
if (!$conn) {
die("Connection Failed : ".mysqli_connect_error());
}
?>
You are missing the column names in your INSERT. For example:
INSERT INTO post (column1, column2, column3, column4) VALUES ('', '$title', '$description', '$article')
Note: Technically, you can skip the column names if you are adding values for all the columns of the table. In this case, you also need to make sure that the order of the values is in the same order as the columns in the table.

php delete record using id

This program is meant to delete a record when given the id.
php:
if ($_GET['type']=="file"){
$servername = "localhost";
$username = "****";
$password = "****";
$dbname = "****";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error($conn)) {
die("Connection failed: " . mysqli_connect_error($conn));
}
$sql = "SELECT id,user, FROM CreationsAndFiles WHERE id =".$_GET['id']." LIMIT 1";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
if ($row['user'] == $login_session){
$sql = "DELETE FROM CreationsAndFiles WHERE id=".$_GET['id'];
if(mysqli_query($conn, $sql)){echo "deleted";}
}
mysqli_close($conn);
//header("location: index.php?page=CreationsAndFiles");
}
the header is type=file&id=9
there is a record where id=9
It for no apparent reason will not work.
Your SQL syntax is wrong;
SELECT id,user, FROM CreationsAndFiles...
^ extra comma
should be simply
SELECT id,user FROM CreationsAndFiles...
You may want to sanitize your input though, for example simply entering type=file&id=id will most likely do bad things.

Inputted values not saved while inserting data from html form into sql database (html,php,sql)

The problem: insert.php connects fine, but only inserts empty values ('') when I hit 'save' on the html form. The text I type, which I'm trying to insert, isn't saved. Somewhere a connection isn't being made and that data is lost but I can't figure out exactly where. Any help?
HTML insert form (collecting data for 2 parameters, 'user' and 'thread')
<form action="insert.php" method="post">
user: <input type="text" name="user"><br>
thread: <input type="text" name="thread"><br>
<input type="submit" value="Save">
</form>
PHP code to connect to SQL, insert inputted values
<?php
$user = $_POST['user'];
$thread = $_POST['thread'];
$servername = "##.##.###";
$username = "harwoodjp";
$password = "~";
$dbname = "332";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error) {
die("SQL (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <br/>";
$sql = mysql_connect($servername,$username,$password);
mysql_connect($servername,$username,$password);
mysql_select_db("332project");
//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);
echo "<script>window.location='select.php'</script>"; //select.php displays the full table
//close MySQL
mysql_close($sql);
?>
try this
<?php
$user = $_POST['user'];
$thread = $_POST['thread'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
//create connection
$conn = mysql($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error) {
die("SQL (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <br/>";
$sql = mysql_connect($servername,$username,$password);
mysql_select_db("db");
//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);
echo "<script>window.location='select.php'</script>"; //select.php displays the full table
//close MySQL
mysql_close($sql);
?>
It might be because the default form posting method is GET.
Either change your $_POST to $_GET or add method="POST" to your form tag.

Trouble connecting data to MySQL

I can't seem to find out why my PHP code won't allow me to store the info in MySQL when I click the submit button. Can someone tell me whats wrong with the code below?
I want to get it to the point where users submit their registration form and their info can get moved over to a MySQL database. Then, I want the users to be taken to my index.html file.
Any help is appreciated.
Thanks
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$mysql_database = "21st";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Could not connect database");
mysqli_select_db($conn, $mysql_database) or die("Could not select database");
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if ($password == $password2) {
$sql = "INSERT INTO members (username, email, password) VALUES ('$userrname','$email','$password')";
} else {
echo "Your passwords must match";
}
?>
Try this using mysqli_query to actually run the query. When you set $sql all that did was set a variable named $sql to the query. Also note you set the variable $userrname in the query when it should be $username as set from the $_POST directly above. My adjusted version of your code below:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$mysql_database = "21st";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Could not connect database");
mysqli_select_db($conn, $mysql_database) or die("Could not select database");
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if ($password === $password2) {
// Set the query.
$sql = "INSERT INTO members (username, email, password)"
. " VALUES (?, ?, ?)"
;
// Bind the values to the query.
mysqli_stmt_bind_param($sql, 'sss', $username, $email, $password);
// Run the query.
mysqli_query($conn, $sql);
// Free the result set.
mysqli_free_result($sql);
// Close the connection.
mysqli_close($conn);
}
else {
echo "Your passwords must match";
}

Categories