can't get connection with sqlite DB - php

I am quite familiar with PHP and mySQL. But for a change, I wanted to use sqlite as a DB, I just can't get the connection right. I have been following the 2. code example from this website, so my code looks like this:
<html>
<head>
<title>
DB bearbeiten
</title>
</head>
<body>
<form action="insert.php" method="post">
<input type="text" name="name" placeholder="Name">
<input type="text" name="eName" placeholder="E-Number">
<input type="text" name="causes" placeholder="Wirkung">
<input type="number" name="danger" placeholder="Gefahreinstufung">
<button type="submit" name="submit">Eintragen</button>
</form>
<?php
echo '1';
if(isset($_POST['submit'])){
echo '2';
$table = "zusatzstoffe";
$filename = "sqlite:/data/Adrian/zusatzstoffe.db";
$db = new PDO($filename) or die("cannot open DB");
//$db = SQLite3::_construct();
settype($danger, "integer");
$name = $_POST['name'];
$eName = $_POST['eName'];
$causes = $_POST['causes'];
$danger = $_POST['danger'];
$db->exec("INSERT INTO $table (name, number, causes, danger) VALUES ($name, $eName, $causes, $danger)");
}
?>
</body>
</html>
This line doesn't work, I just don't know why: $db = new PDO(filename, '','') or die("can not open DB");:

Related

Insert php form data in mysqli error

I am trying to create PHP form data insert in SQL but getting error.
Even when I write code same to same but I'm still getting an error.
<?php
$un = $_POST['uname'];
$em = $_POST['email1'];
//with or what out these bellow variables
$host = "localhost";
$username = "admin";
$password = "admin";
$database = "test1";
$db = mysqli_connect('$host','$username','$password','$database');
$query = "INSERT INTO users ('username','password') VALUES ('$un','$em')";
$rs = mysqli_query($db,$query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Registration</title>
</head>
<body>
<form action="server1.php" method="post">
<label>Name</label>
<input type="text" name="uname" required="required">
<label>Email</label>
<input type="email" name="email1" required="required">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
The error was "" just inverted comma's, now its works perfectly.
<?php
$un = $_POST['uname'];
$em = $_POST['email1'];
$host = "localhost";
$username = "admin";
$password = "admin";
$database = "test1";
$con = mysqli_connect ("$host","$username","$password","$database");
$query = "insert into users (username,email) values ('$un','$em')";
$run = mysqli_query ($con,$query);
if ($run=TRUE){
echo 'Data Submitted Successfuly';
}
else {
echo 'Error';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Registration</title>
</head>
<body>
<form action="server1.php" method="post">
<label>Name</label>
<input type="text" name="uname" required="required">
<label>Email</label>
<input type="email" name="email1" required="required">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
You can try this way.
$db = mysqli_connect($host, $username, $password) or die ('Unable to connect');
mysqli_select_db($db, $database) or die('Unable to select DB');

PHP connected to (MySQL) database - does not input data

I'm using Cloud9 to create a website. For whatever reason, the data taken from a HTML page will not get inserted into the Database.
I have tested to see if the Database is connected and it is. I would like to be able to get the data to be inserted into the Database.
The HTML code and PHP code is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form
</title>
</head>
<body>
<form action="../php/keithphp/address_submit.php" method="post">
<p>
<label for="address_street">Street</label>
<input type="text" name="address_street" id="address_street">
</p>
<p>
<label for="address_street2">Street 2</label>
<input type="text" name="address_street2" id="address_street2">
</p>
<p>
<label for="address_city">City</label>
<input type="text" name="address_city" id="address_city">
</p>
<p>
<label for="address_county">County</label>
<input type="text" name="address_county" id="address_county">
</p>
<p>
<label for="eircode">Eircode</label>
<input type="text" name="eircode" id="eircode">
</p>
<!-- <p>
<label for="address_geo_latitude">Latitude</label>
<input type="float" name="address_geo_latitude" id="address_geo_latitude">
</p>
<p>
<label for="address_geo_longtitude">Longitude</label>
<input type="float" name="address_geo_longtitude" id="address_geo_longtitude">
</p> -->
<input type="submit" value="Submit">
</form>
</body>
</html>
*****************
<?php
$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "";
$database = "c9";
$dbport = 3306;
// Create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
/*$address_id = $_POST['address_id'];*/
$address_street = $_POST['address_street'];
$address_street2 = $_POST['address_street2'];
$address_city = $_POST['address_city'];
$address_county = $_POST['address_county'];
$address_eircode = $_POST['address_eircode'];
/*$address_geo_latitude = $_POST['address_geo_latitude'];
$address_geo_longtitude = $_POST['address_geo_longtitude'];*/
$sql = "INSERT INTO Address(address_id, address_street, address_street2, address_city, address_county, address_eircode, address_geo_latitude, address_geo_longtitude) VALUES ('$address_id', '$address_street', '$address_street2', '$address_city', '$address_county', '$address_eircode', '$address_geo_latitude', '$address_geo_longtitude')";
$success = $db->query($sql);
if (!$sucess){
die("Could not enter data: ".$db->error);
}
echo "Thank you. Address submitted!"
/*my$db->close();*/
?>
And the result after typing in test data is (no error message after text), I get;
Could not enter data:

Can't insert new records through PHP into mysql database

When I press the submit button I get error.
object not found error.
And the page automatically adds empty entries with auto incremented primary key (without pressing the submit button).
I am still a beginner in PHP, I searched thoroughly but I can't find out what's wrong in code.
<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="Name">Full Name:</label>
<input type="text" name="Name" id="Name">
</p>
<p>
<label for="Code">Code:</label>
<input type="text" name="Code" id="Code">
</p>
<p>
<label for="GPA">GPA:</label>
<input type="text" name="GPA" id="GPA">
</p>
<input type="submit" value="Submit">
</form>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "username", "password", "students");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$full_name = filter_input(INPUT_POST, 'full_name');
$code = filter_input(INPUT_POST, 'code');
$gpa = filter_input(INPUT_POST, 'gpa');
// attempt insert query execution
$sql = "INSERT INTO info VALUES ('$full_name', '$code', '$gpa')";
if (mysqli_query($link, $sql)) {
echo "Records added successfully. $full_name";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
</body>
</html>
Try this:
$full_name = filter_input(INPUT_POST, 'Name');
$code = filter_input(INPUT_POST, 'Code');
$gpa = filter_input(INPUT_POST, 'GPA');
The reason why I wrote that is because your input names contain Name, Code and GPA so you need to write this exactly as your input names (case-sensitive).
Do with isset(). when the submit button clicks only the code runs.
Inside the php you should use the form input name field.
<?php
if(isset($_POST['submit'])){
$link = mysqli_connect("localhost", "username", "password", "students");
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$full_name = filter_input(INPUT_POST, 'full_name');
$code = filter_input(INPUT_POST, 'code');
$gpa = filter_input(INPUT_POST, 'gpa');
//to prevent sql injection attack
$full_name = mysqli_real_escape_string($link, $full_name);
$code = mysqli_real_escape_string($link, $code);
$gpa = mysqli_real_escape_string($link, $gpa);
// attempt insert query execution
$sql = "INSERT INTO info (Name,Code,GPA) VALUES ('$full_name', '$code', '$gpa')";
if (mysqli_query($link, $sql)) {
echo "Records added successfully. $full_name";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
}
?>
<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="Name">Full Name:</label>
<input type="text" name="full_name" id="Name">
</p>
<p>
<label for="Code">Code:</label>
<input type="text" name="code" id="Code">
</p>
<p>
<label for="GPA">GPA:</label>
<input type="text" name="gpa" id="GPA">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
The problem is the input name. You named Full Name input with name="Name", but you declare $full_name = filter_input(INPUT_POST, 'full_name'); in php section. you must change full_name to Name. As well as the Code and GPA input.

Form Data will not insert to database

I have connected my website to my database successfully when i submit the form it goes through but nothing is getting inserted into the database.
Code Below:
<?php
if( $_POST )
{
$con = mysql_connect("server","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("buycruisesdb", $con);
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$query = "
INSERT INTO `website_subscribers`(`name_sub`, `email_sub`) VALUES ([$users_name],[$users_email])";
mysql_query($query);
echo "<h2>Thank you for subscribing!</h2>";
echo $query;
echo $users_name;
echo $users_email;
mysql_close($con);
}
?>
buycruisesdb = database
website_subscribers = table inside the database
name_sub/email_sub = columns inside the table
the form html is below:
!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form action="php/subscriber.php" id="form" method="post" name="form">
<input id="name_sub" name="name" placeholder="Name" type="text">
<input id="email_sub" name="email" placeholder="Email" type="text">
<input type="submit" value="Submit" name="f_submit">
</form>
</body>
</html>
Not sure exactly why this is not inputing anyone have an idea?
it says that it is inserting the proper values and into the proper tables
Image
Square brackets are not valid in MySQL queries. You should be using quotes around the strings.
$query = "INSERT INTO `website_subscribers` (`name_sub`, `email_sub`) VALUES ('$users_name', '$users_email')";
change it to:
$query = "
INSERT INTO website_subscribers (name_sub,email_sub) VALUES ('".$users_name."','".$users_email."') ";
just copy the code and try it out

Unable to insert mysql database through form data

I have created a form where I want to insert mysql database when I submit the values but when I click on the submit button the database isnt updated.. dont know where I am going wrong in my code..
<html>
<head>
<title>Form Data</title>
</head>
<body>
<form action="form.php" method="post">
Server Name: <input type="text" name="server_name"> <br />
IP Address: <input type="text" name="ip_address"> <br />
Server Role: <input type="text" name="server_role"> <br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "abcx";
$dbname = "serverasset_inventory";
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$sql = "INSERT INTO asset_inventory (server_name,ip_address,server_role)
VALUES ('$_POST[server_name]','$_POST[ip_address]'),'$_POST[server_role]')";
$state = mysqli_query($connection,$sql);
mysqli_close($connection);
}
?>
</body>
</html>
Any help?
-A
you are using a extra bracket between query and missing quote in variable's use like below
$sql = "INSERT INTO `asset_inventory` (`server_name`,`ip_address`,`server_role`)
VALUES ('".$_POST['server_name']."','".$_POST['ip_address']."','".$_POST['server_role']."')";
if you want to insert the fields
for example
your sql will be take in variables then insert it in sql
$server_name = $_POST['server_name'];
$ip_address = $_POST['ip_address'];
$server_role = $_POST['server_role'];
insert into asset_inventory (server_name,ip_address,server_role) VALUES ('$server_name','$ip_address','$server_role';

Categories