Php adding an entry to the database. The lesson will be especially useful for beginners
Comments moved from the blog
SERGEY
09/14/2016 at 01:25
Good afternoon!
I am interested in the following question: what is the easiest way to organize the storage of data and program settings without using a database? I don't want to be tied to MySQL or Access..
ADMIN
09/14/2016 at 22:14
Hello!
Properties.Settings
App.Config
XML file
serialization
Try to choose one of these from the list.
NICHOLAY
09/16/2016 at 02:28
Hello, how can I delete the selected row in dataGridVIew from dataGridVIew and phpMyAdmin.
PhpMyAdmin? This is just a shell for working with a database, can you explain?
NICHOLAY
09/18/2016 at 02:24
It is necessary that the selected row be deleted from the DataGridView and from the database.
ADMIN
09/19/2016 at 07:00
How to delete a row in the database Mysql data- added an article.
NICHOLAY
09/20/2016 at 09:20
Thanks a lot.
DIMA
09.20.2016 at 10:24
Hello, is it possible this method Implemented not through DataGridView, but through ComboBox? If so, how? Thank you.
ADMIN
09/22/2016 at 03:21
Hello. Example:
GENNADY
09.22.2016 at 18:25
why should I enter such text into the database System.Windows.Forms.TextBox, Text: ge
By the way, this (ge) at the end it is written by gene, even though the text is specified in the table settings. The word of gene should have fit further, I display this table in my program and it turns out that it displays all this unnecessary text for me
ADMIN
09/24/2016 at 04:17
Most likely the SQL query is written incorrectly, for example:
In textBox1 enter the name: Gena.
Sql query: “Insert into table name values(textBox1, ..)”; Result: System.Windows.Forms.TextBox
And you need to pass: “Insert into table name values(textBox1.Text, ..)”;
Result: Gena
GENNADY
09.24.2016 at 18:41
That is how it is. Thank you
SERGEY
09.25.2016 at 11:51
Hello. How to implement adding to the database via textBox?
ADMIN
09.26.2016 at 20:53
Everything is the same in principle. For example, let's take the very last example, it needs:
//create parameters and add them to the collection cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@LastName", textBox2.Text);
now the parameters: Name and LastName receive the values entered in the textboxes and transfer them to the database
LINARA
09.27.2016 at 17:45
Hello, how can I select a row in dataGridVIew and phpMyAdmin?
ADMIN
09/29/2016 at 02:06
I don't know how to highlight a line in phpMyAdmin. And in dataGridView, for example, this can be done using the SelectionChanged event.
P.S.H.
09/30/2016 at 03:48
2Linara:
If you want to edit rows that way, take a tool a la HediSQL, configure and change the rows.
2admin
Good day! Thanks for the materials - everything is very cool)
Question: I add data with the following request (it’s a test one):
String sql = "INSERT INTO users (`FIO`, `Tour`, `Count`, `Cost`, `Date`, `Passport`, `Birth`) VALUES ("Kolyan", "Moscow", "1+1 ", 1100, "2011-11-11", "1111 1111", "9.11.1900");";
The data is entered ok, but in the database (mysql) instead of the Cyrillic alphabet it appears “????”.
Visual studio says that System.String is a Unicode sequence.
Also tried:
ALTER DATABASE `test` COLLATE "koi8r_general_ci"; ALTER TABLE `users` COLLATE="koi8r_general_ci"; ALTER DATABASE `test` COLLATE "utf8_unicode_ci"; ALTER TABLE `users` COLLATE="utf8_unicode_ci";
But it doesn't help...
What can be wrong? Different VS and DB encodings? Or what?
Could you direct me what to read/change.
Thank you
ADMIN
10/01/2016 at 09:49
Hello.
In the database (and in the table) the mapping is utf_general_ci
Is there such a comparison? Perhaps utf8_general_ci?
Usually they create a Mysql database by choosing the utf8_general_ci comparison, so there are no problems with the Cyrillic alphabet, unless, of course, crooks come from the client to the server.
COLLATION is used for comparison, but in this case the encoding (charset) is important. Therefore, first you need to make sure that it is set correctly on the server, for example in utf8, and not latin1.
When connecting via the .net connector (by default), latin1 is used, so sometimes you need to explicitly specify the utf8 encoding in the connection string:
MySqlConnection mycon; mycon = new MySqlConnection("server=127.0.0.1;uid=vasya;pwd=123;database=test;Charset=utf8;"); //MySqlConnectionStringBuilder: mysqlCSB.CharacterSet = "utf8";
P.S.H.
10/01/2016 at 11:34
You're right, I peed myself, utf8_general_ci!
Yes it helped, ;Charset=utf8;
Thank you so much!
SERGY
10/02/2016 at 11:02
Thanks for the working example. Question
I created a text field in which I would like to enter the IP address of the database, but I don’t know how to insert this data here
String conStr = "server=@textBox2;user=test;" +
"database=test;password=test;";
Please tell me how to insert data from text fields in a windows form into this design...
ADMIN
10/03/2016 at 11:50
"[email protected];user=...
In general, it is better to use properties instead of such a string, as in this article, or the String.Format() method
OLGA2203
05/15/2017 at 20:14
String Connect = “Server=127.0.0.1;Port=3306;Database=base;Data Source=localhost;user=root;”; MySqlConnection con = new MySqlConnection(Connect); con.Open(); //Establish a connection to the database. MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = @”INSERT INTO tovar(ID,Category,Name,TradeMark,Price,Photo,Size,Color,Material,Count) VALUES (@pr, @Category, @Name, @TradeMark, @Price, @Photo, @Size, @Color, @Material, @Count)”; cmd.Parameters.AddWithValue(“@pr”,counter); cmd.Parameters.AddWithValue(“@Category”, comboBox1.SelectedItem.ToString()); cmd.Parameters.AddWithValue(“@Name”, textBox1.Text); cmd.Parameters.AddWithValue(“@TradeMark”, textBox2.Text); cmd.Parameters.AddWithValue(“@Price”, Convert.ToInt32(textBox4.Text)); cmd.Parameters.AddWithValue(“@Photo”, textBox3.Text); cmd.Parameters.AddWithValue(“@Size”, textBox6.Text); cmd.Parameters.AddWithValue(“@Color”, textBox5.Text); cmd.Parameters.AddWithValue(“@Material”, textBox8.Text); cmd.Parameters.AddWithValue(“@Count”, Convert.ToInt32(textBox7.Text)); cmd.Connection = con; cmd.ExecuteNonQuery(); MessageBox.Show(“The addition was successful”, “The addition was successful”, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
The error “Column ‘ID’ cannot be null” is displayed, I remove the addition to the ID column - the same thing is written about the next column, etc.
If I enter any constant values in parentheses into VALUES, the row is added to the database.
Tell me, please, what is the problem? I need to record exactly the data and values entered through the form into the database
In this post I want to tell you, how to transfer the entered data into the form to the Database. And so we create a simple form where we will have two fields: the username and his Email:
This form can be used to register a new user, to send out news, to collect statistics, or for anything... In general, the user enters his data into this form: name and email, clicks on the button and then the data goes into the PHP script:
$name = $_POST["name"]; $email = $_POST["email"]; $result = mysqli_query("INSERT INTO user (name, email) VALUES ("$name", "$email")"); if ($result) ( echo "Data saved successfully!"; ) else ( echo "An error occurred, please try again."; )
What's going on in this script? Let's figure it out now!
The data entered into the form POST method go to the php script (which is written above), and using global array$_POST data is formed into the $name and $email variables:
$name = $_POST["name"]; $email = $_POST["email"];
After the variables are ready to be entered into the database, we create a request. But first, your scripts must already be connected to the database; I wrote in this thread how to connect to the database: . The request itself looks like this:
$result = mysqli_query("INSERT INTO user (name, email) VALUES ("$name", "$email")");
In this code, we have indicated that the following variables will be added to the name and email cells that are in the user table: $name and $email.
Next, if everything went well, we will receive a message from the condition:
Data saved successfully!
If any problems arise and the data has not been entered, we will receive an error message:
An error occurred, please try again.
That's all!
*** *** *** *** ***
If desired, you can add more fields for entering information, for example, we need to add a field for entering the user's city. We already have a ready-made script (written above), now we’ll just add a field Your city, let's call the variable: $city . And so on in the data entry form, after:
Your email:
add:
Your city:
IN php script, after:
$email = $_POST["email"];
add:
$city = $_POST["city"];
And of course we add it in the request too, like this:
$result = mysqli_query("INSERT INTO user (name, email, city) VALUES ("$name", "$email", "$city")");
This is what you should end up with:
Input form:
Script:
$name = $_POST["name"]; $email = $_POST["email"]; $city = $_POST["city"]; $result = mysqli_query("INSERT INTO user (name, email, city) VALUES ("$name", "$email", "$city")"); if ($result == true) ( echo "Data saved successfully!"; ) else ( echo "An error occurred, please try again."; )
As you can see, nothing complicated! If necessary, you can add another field, and another, and another...
The lesson will be based on form feedback , necessary on almost any website.======================================== ?>
Step one: Creating a database in MySQL
Opening phpMyAdmin(included in the basic package Denwer`a), and create a database called " test_base", select the encoding " cp1251_general_ci".
======================================== ?>
Step two: Creating a table in MySQL by using SQL query
You can, of course, create a table using standard means phpMyAdmin, but this way the structure of the table being created will be clearly visible.
Create a table called " test_table" and with six fields named:
"name" - user names will be stored here;
"email" - addresses will be stored here electronic mailboxes users;
"theme" - the subject of the message will be stored here;
"message" - messages will be stored here;
"data" - the date the message was sent will be stored here;
"id" - identification number of the record (line), key field.SQL query:
create table test_table(
id int(11) not null auto_increment,
name varchar(255) not null,
email varchar(255) not null,
theme varchar(255) not null,
message text not null,
data date not null,
primary key (id)
);======================================== ?>
Step three: Creating a Form
index.html:
Form saved in MySQL
Example of a form with data saving in MySQL