


PHP form data is written into MySQL code, and PHP form is written into mysql_PHP tutorial
Jul 12, 2016 am 08:51 AMPHP form data is written into MySQL code, and PHP form is written into mysql
<h1>插入操作</h1> <?<span>php </span><span>if</span>(!<span>isset</span>(<span>$_POST</span>['submit'<span>])){ </span><span>//</span><span>如果沒有表單提交,顯示一個表單 </span> ?> <form action="" method="post"><span> 國家:</span><input type="text" name="country" /><span> 動物名稱(英文):</span><input type="text" name="animal" /><span> 動物名稱(中文):</span><input type="text" name="cname" /> <input type="submit" name="submit" value="提交表單" /> </form> <?<span>php } </span><span>else</span><span> { </span><span>//</span><span>如果提交了表單 //數(shù)據(jù)庫連接參數(shù) </span> <span>$host</span> = "localhost"<span>; </span><span>$user</span> = "root"<span>; </span><span>$pass</span> = "zq19890319"<span>; </span><span>$db</span> = "phpdev"<span>; </span><span>//</span><span> 何問起 hovertree.com //取得表單中的值,檢查表單中的值是否符合標(biāo)準(zhǔn),并做適當(dāng)轉(zhuǎn)義,防止SQL注入 </span> <span>$country</span> = <span>empty</span>(<span>$_POST</span>['country'])? <span>die</span>("請輸入國家名稱"): <span>mysql_escape_string</span>(<span>$_POST</span>['country'<span>]); </span><span>$animal</span> = <span>empty</span>(<span>$_POST</span>['animal'])? <span>die</span>("請輸入英文名"): <span>mysql_escape_string</span>(<span>$_POST</span>['animal'<span>]); </span><span>$cname</span> = <span>empty</span>(<span>$_POST</span>['cname'])? <span>die</span>("請輸入中文名"): <span>mysql_escape_string</span>(<span>$_POST</span>['cname'<span>]); </span><span>//</span><span>打開數(shù)據(jù)庫連接 </span> <span>$connection</span> = <span>mysql_connect</span>(<span>$host</span>, <span>$user</span>, <span>$pass</span>) or <span>die</span>("Unable to connect!"<span>); </span><span>//</span><span>選擇數(shù)據(jù)庫 </span> <span>mysql_select_db</span>(<span>$db</span>) or <span>die</span>("Unable to select database!"<span>); </span><span>//</span><span>構(gòu)造一個SQL查詢 </span> <span>$query</span> = "INSERT INTO symbols(country, animal, cname) VALUE('<span>$country</span>', '<span>$animal</span>', '<span>$cname</span>')"<span>; </span><span>//</span><span>執(zhí)行該查詢 </span> <span>$result</span> = <span>mysql_query</span>(<span>$query</span>) or <span>die</span>("Error in query: <span>$query</span>. ".<span>mysql_error</span><span>()); </span><span>//</span><span>插入操作成功后,顯示插入記錄的記錄號 </span> <span>echo</span> "記錄已經(jīng)插入, mysql_insert_id() = ".<span>mysql_insert_id</span><span>(); </span><span>//</span><span>關(guān)閉當(dāng)前數(shù)據(jù)庫連接 </span> <span>mysql_close</span>(<span>$connection</span><span>); } </span>?>
Reference: http://www.cnblogs.com/roucheng/p/phpmysql.html
The above code uses different formats, I don’t know which format is better
Insert operation
- ?if(!isset($_POST['submit' ])){
- //If no form is submitted, display a form
- ?>
- method="post">
- Country: "text" name="country" />
- Animal name (English): "text" name="animal" />
- Animal name (Chinese): "text" name="cname" />
- "submit" name="submit" value="submit Form" />
- }
- else
- {
- //If the form is submitted
- //Database connection parameters
- $host = "localhost";
- $user = "root";
- $pass = "zq19890319";
- $db = "phpdev";
- ?
- //Get the value in the form, check whether the value in the form meets the standards, and escape it appropriately to prevent SQL injection
- ?$country = empty($_POST[ 'country'])? die("Please enter the country name"):
- mysql_escape_string($_POST['country']);
- ?$animal = empty($_POST[ 'animal'])? die("Please enter your English name"):
- mysql_escape_string($_POST['animal']);
- ?$cname = empty($_POST[ 'cname'])? die("Please enter your Chinese name"):
- mysql_escape_string($_POST['cname']);
- ?
- //Open the database connection hovertree.com He asked
- ?$connection = mysql_connect($host, $user, $pass) or die("Unable to connect!");
- ?
- //Select database
- mysql_select_db($db) or die( "Unable to select database!");
- ?
- //Construct a SQL query
- ?$query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname ')";
- ?
- ?//Execute the query?
- $result = mysql_query($query) or die("Error in query: $query. ".mysql_error());
- ?
- //After the insertion operation is successful, the record number of the inserted record is displayed
- ?echo "The record has been inserted, mysql_insert_id() = ".mysql_insert_id();
- ?
- //Close the current database connection
- mysql_close($connection);
- }
- ?>

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

html2pdf is a JavaScript package that allows developers to convert html to canvas, pdf, images, and more. It takes html as parameter and adds it to pdf or desired document. Additionally, it allows users to download the document after adding html content. Here we will access the form and add it to the pdf using the html2pdfnpm package. We will see different examples to add form data to pdf. Syntax User can follow the following syntax to pass html form data as text and send it to html2pdf. varelement=document.getElementById('form');html2

In this article, we will learn how to allow multiple files uploads in HTML forms. We use multiple attributes to allow multiple file uploads in HTML forms. Several properties are available for email and file input types. Ifyouwanttoallowausertouploadthefiletoyourwebsite,youneedtouseafileuploadbox,alsoknownasafile,selectbox.Thisiscreatedusingthe<in

PHP file upload tutorial: How to use HTML forms to upload files In the process of website development, the file upload function is a very common requirement. As a popular server scripting language, PHP can implement the file upload function very well. This article will introduce in detail how to use HTML forms to complete file uploads. 1. HTML form First, we need to use an HTML form to create a file upload page. In the HTML form, the enctype attribute needs to be set to "multipart/form-

How to handle HTML forms using Java? HTML form is one of the commonly used interactive elements in web pages, through which users can input and submit data. Java, as a powerful programming language, can be used to process and validate HTML form data. This article will introduce how to use Java to process HTML forms, with code examples. The basic steps for processing HTML form data in Java are as follows: monitor and receive POST requests from HTML forms; parse the parameters of the request; process data according to needs

In web development, it is often necessary to use regular expressions to match strings. In HTML, the form tag is a very important tag, so if we need to get all the form tags in the page, then regular expressions become a very useful tool. This article will introduce using regular expressions in PHP to match all form tags in HTML. 1. The form tag in HTML The form tag is a very important tag in HTML. It is used to create forms. The form is used

ThemethodattributeinHTMLformsdetermineshowdataissenttotheserver,usingeitherGETorPOST.GETappendsdatatotheURL,haslengthlimits,andissuitablefornon-sensitiverequestslikesearches.POSTsendsdatainthebody,offersbettersecurity,andisidealforsensitiveorlargedat

The key to designing complex HTML forms lies in content organization rather than encoding. To improve user experience and reduce error rates, the following steps must be followed: 1. Use and divide logical blocks to enhance structural clarity, accessibility and maintenance; 2. Clear the binding relationship between controls and tags, and ensure that each input box has corresponding tags through for and id; 3. Use hidden fields and conditions to display reasonably, combine CSS/JS to control the display status and process data validity; 4. Design hierarchical error prompts to avoid relying on color only to distinguish. It is recommended to add icons, highlight borders and set summary areas.

HTML forms support multiple input types to suit different data entry requirements. 1. Text input is used for basic data entry, such as text and password, and interaction can be enhanced through placeholder, maxlength, required and other attributes; 2. HTML5 introduces special input types such as email, number, date, tel, and url to improve data accuracy and usability; 3. Selection and operation controls include checkbox, radio, submit, and button, for multiple selection, single selection and submission operations; 4. Hidden is used to pass uneditable data, and file is used for file upload and can limit file types. When using it, you need to consider browsing
