Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm creating a form where users can enter information and it will enter into the database on the website:
<?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);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Only problem. It will work for me great because I can manually enter the feilds of my database information
($servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";)
But I am planning on releasing this form out to the public and I want them to not have to edit every single .php file (theres going to be about five php files)
How can I have it so they can "set it up" the first time they install these forms on their site.
You can solve this by having all the database settings inside a separate file, e.g. dbsettings.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
?>
In the file that you would normally do the database connection, instead of just connecting your database using:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
You would include the database settings file first.
<?php
include 'dbsettings.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This is how you do it in PHP. You will see similar setup used in Wordpress, and all other PHP frameworks like Laravel, FuelPHP, CodeIgniter etc (it might be a good idea to use these for bigger projects).
Related
on line 1 of my page i required the connection file before going on to write my code. at this point i think it is supposed to work but can not figure out why it didn't. please some one who knows better help me.
Can you help me with a sample PHP code for inserting data into mysql database using PHP, mysqli?
Thank you again in advance.
You can use this code for insert data to mysql DB using php and mysqli
<?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);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I'm working in a web app with PHP and Postgresql. I`m looking for a PHP code to create a database online using a PHP form that sends the name, template, UTF8, etc.
Something like this for MYSQL but for with Postgresql:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
SERVER RESPONSE
Error: INSERT INTO epiz_19848473_Liste1 (Rowcount, Level1) VALUES (0, 'any Value')
No database selected
PHP CODE
UPDATE
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "Liste1";<--UPDATED
// Create connection
$conn = mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Liste1 <--UPDATED (Rowcount, Level1)
VALUES (0, 'any Value')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This should work, right?
NEW STATUS
Connection failed: Access denied for user 'epiz_19848473'#'192.168.0.%' to database 'Liste1'
The code is perfectly all right , the problem is with mysql configuration .
Please login to mysql as **root user ** and do the following,
GRANT ALL PRIVILEGES ON . TO 'epiz_19848473'#'192.168.0.%' (use ip of php server)
FLUSH PRIVILIGES;
The above allows the user to connect to mysql table from the ip specified.
you did not defined the database name in the connection
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "DATABASE_NAME_HERE";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
else you can define define the database name in the connection
$conn = new mysqli($servername, $username, $password, "DATABASE_NAME_HERE");
Just define the dbname,like this:
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "test"; //You should create this db in mysql
....
You Can Use This:
CREATE TABLE [database_name].[table_name] (
[your_table_things]
);
So, i got this code:
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "TestDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br>";
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO testdata (id,address,count) VALUES (DEFAULT,'$ip',1) ON DUPLICATE KEY UPDATE count=count+1";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " ;
}
$conn->close();
?>
With this code ip-addresses of customers are saved and it should increase count by 1 every time they return to the website. When i visit database.php directly it works like it should in every browser, but when i call the php-file via the index.html-page it counts up twice (most of the times, not always) in Mozilla, other browsers no problem, the code in html-file:
<img style="display: none;" src="http://servername/database.php?">
Anyone know why? Please help, i'm really stuck here
New to the concept of php custom function and was wondering how to build a custom function for INSERT
<?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);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Can anyone help. Will appreciate if I can have link to a tutorial as well.
Here is a helpful tutorial for a begginer with Php. Hope this help:
http://alejoferguson.blogspot.com/2014/08/tutorial-php-mysql-recomendado_27.html
I would not recommend you to have server and password information on the same page your doing the query but rather to have it separately as is explained in this simple tutorial. ALso it provides you with the source code at the end of the tutorial.
Guide yourself by the images in this tutorial.