


Code for writing PHP form data to MySQL database, php form mysql database_PHP tutorial
Jul 12, 2016 am 08:50 AMThe code for writing PHP form data into MySQL database, PHP form mysql database
Without further ado, I will post the code directly for you. The specific code is as follows:
<h插入操作</h <?php if(!isset($_POST['submit'])){ //如果沒(méi)有表單提交,顯示一個(gè)表單 ?> <form action="" method="post"> 國(guó)家:<input type="text" name="country" /> 動(dòng)物名稱(英文):<input type="text" name="animal" /> 動(dòng)物名稱(中文):<input type="text" name="cname" /> <input type="submit" name="submit" value="提交表單" /> </form> <?php } else { //如果提交了表單 //數(shù)據(jù)庫(kù)連接參數(shù) $host = "localhost"; $user = "root"; $pass = "zq"; $db = "phpdev"; // 何問(wèn)起 hovertree.com //取得表單中的值,檢查表單中的值是否符合標(biāo)準(zhǔn),并做適當(dāng)轉(zhuǎn)義,防止SQL注入 $country = empty($_POST['country'])? die("請(qǐng)輸入國(guó)家名稱"): mysql_escape_string($_POST['country']); $animal = empty($_POST['animal'])? die("請(qǐng)輸入英文名"): mysql_escape_string($_POST['animal']); $cname = empty($_POST['cname'])? die("請(qǐng)輸入中文名"): mysql_escape_string($_POST['cname']); //打開(kāi)數(shù)據(jù)庫(kù)連接 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //選擇數(shù)據(jù)庫(kù) mysql_select_db($db) or die("Unable to select database!"); //構(gòu)造一個(gè)SQL查詢 $query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname')"; //執(zhí)行該查詢 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //插入操作成功后,顯示插入記錄的記錄號(hào) echo "記錄已經(jīng)插入, mysql_insert_id() = ".mysql_insert_id(); //關(guān)閉當(dāng)前數(shù)據(jù)庫(kù)連接 mysql_close($connection); } ?>
The above code uses different formats, I don’t know which format is better
<h插入操作</h <?php if(!isset($_POST['submit'])){ //如果沒(méi)有表單提交,顯示一個(gè)表單 ?> <form action="" method="post"> 國(guó)家:<input type="text" name="country" /> 動(dòng)物名稱(英文):<input type="text" name="animal" /> 動(dòng)物名稱(中文):<input type="text" name="cname" /> <input type="submit" name="submit" value="提交表單" /> </form> <?php } else { //如果提交了表單 //數(shù)據(jù)庫(kù)連接參數(shù) $host = "localhost"; $user = "root"; $pass = "zq"; $db = "phpdev"; //取得表單中的值,檢查表單中的值是否符合標(biāo)準(zhǔn),并做適當(dāng)轉(zhuǎn)義,防止SQL注入 $country = empty($_POST['country'])? die("請(qǐng)輸入國(guó)家名稱"): mysql_escape_string($_POST['country']); $animal = empty($_POST['animal'])? die("請(qǐng)輸入英文名"): mysql_escape_string($_POST['animal']); $cname = empty($_POST['cname'])? die("請(qǐng)輸入中文名"): mysql_escape_string($_POST['cname']); //打開(kāi)數(shù)據(jù)庫(kù)連接 hovertree.com 何問(wèn)起 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //選擇數(shù)據(jù)庫(kù) mysql_select_db($db) or die("Unable to select database!"); //構(gòu)造一個(gè)SQL查詢 $query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname')"; //執(zhí)行該查詢 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //插入操作成功后,顯示插入記錄的記錄號(hào) echo "記錄已經(jīng)插入, mysql_insert_id() = ".mysql_insert_id(); //關(guān)閉當(dāng)前數(shù)據(jù)庫(kù)連接 mysql_close($connection); } ?>
The above is the code for writing PHP form data to the MySQL database introduced by the editor. I hope it will be helpful to everyone!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

The most direct way to find the last occurrence of a substring in PHP is to use the strrpos() function. 1. Use strrpos() function to directly obtain the index of the last occurrence of the substring in the main string. If it is not found, it returns false. The syntax is strrpos($haystack,$needle,$offset=0). 2. If you need to ignore case, you can use the strripos() function to implement case-insensitive search. 3. For multi-byte characters such as Chinese, the mb_strrpos() function in the mbstring extension should be used to ensure that the character position is returned instead of the byte position. 4. Note that strrpos() returns f

The reason why header('Location:...') in AJAX request is invalid is that the browser will not automatically perform page redirects. Because in the AJAX request, the 302 status code and Location header information returned by the server will be processed as response data, rather than triggering the jump behavior. Solutions are: 1. Return JSON data in PHP and include a jump URL; 2. Check the redirect field in the front-end AJAX callback and jump manually with window.location.href; 3. Ensure that the PHP output is only JSON to avoid parsing failure; 4. To deal with cross-domain problems, you need to set appropriate CORS headers; 5. To prevent cache interference, you can add a timestamp or set cache:f

MySQL supports transaction processing, and uses the InnoDB storage engine to ensure data consistency and integrity. 1. Transactions are a set of SQL operations, either all succeed or all fail to roll back; 2. ACID attributes include atomicity, consistency, isolation and persistence; 3. The statements that manually control transactions are STARTTRANSACTION, COMMIT and ROLLBACK; 4. The four isolation levels include read not committed, read submitted, repeatable read and serialization; 5. Use transactions correctly to avoid long-term operation, turn off automatic commits, and reasonably handle locks and exceptions. Through these mechanisms, MySQL can achieve high reliability and concurrent control.

Character set and sorting rules issues are common when cross-platform migration or multi-person development, resulting in garbled code or inconsistent query. There are three core solutions: First, check and unify the character set of database, table, and fields to utf8mb4, view through SHOWCREATEDATABASE/TABLE, and modify it with ALTER statement; second, specify the utf8mb4 character set when the client connects, and set it in connection parameters or execute SETNAMES; third, select the sorting rules reasonably, and recommend using utf8mb4_unicode_ci to ensure the accuracy of comparison and sorting, and specify or modify it through ALTER when building the library and table.

The most direct way to connect to MySQL database is to use the command line client. First enter the mysql-u username -p and enter the password correctly to enter the interactive interface; if you connect to the remote database, you need to add the -h parameter to specify the host address. Secondly, you can directly switch to a specific database or execute SQL files when logging in, such as mysql-u username-p database name or mysql-u username-p database name

The setting of character sets and collation rules in MySQL is crucial, affecting data storage, query efficiency and consistency. First, the character set determines the storable character range, such as utf8mb4 supports Chinese and emojis; the sorting rules control the character comparison method, such as utf8mb4_unicode_ci is case-sensitive, and utf8mb4_bin is binary comparison. Secondly, the character set can be set at multiple levels of server, database, table, and column. It is recommended to use utf8mb4 and utf8mb4_unicode_ci in a unified manner to avoid conflicts. Furthermore, the garbled code problem is often caused by inconsistent character sets of connections, storage or program terminals, and needs to be checked layer by layer and set uniformly. In addition, character sets should be specified when exporting and importing to prevent conversion errors
