Heim > Backend-Entwicklung > PHP-Tutorial > PHP表单数据写入MySQL代码,php表单写入mysql_PHP教程

PHP表单数据写入MySQL代码,php表单写入mysql_PHP教程

WBOY
Freigeben: 2016-07-12 08:51:28
Original
882 Leute haben es durchsucht

PHP表单数据写入MySQL代码,php表单写入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>如果提交了表单  
        //数据库连接参数  </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
        //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止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>打开数据库连接  </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>选择数据库  </span>
        <span>mysql_select_db</span>(<span>$db</span>) or <span>die</span>("Unable to select database!"<span>);  
          
        </span><span>//</span><span>构造一个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>执行该查询  </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> "记录已经插入, mysql_insert_id() = ".<span>mysql_insert_id</span><span>();  
          
        </span><span>//</span><span>关闭当前数据库连接  </span>
        <span>mysql_close</span>(<span>$connection</span><span>);  
    }  
</span>?> 
Nach dem Login kopieren

参考:http://www.cnblogs.com/roucheng/p/phpmysql.html

上面的代码用不同格式,不知道哪种格式更好

  1. 插入操作

      
  2.     if(!isset($_POST['submit'])){  
  3.         //如果没有表单提交,显示一个表单  
  4. ?>  
  5.     
    ""
     method="post">  
  6.     国家:"text" name="country" />  
  7.     动物名称(英文):"text" name="animal" />  
  8.     动物名称(中文):"text" name="cname" />  
  9.     "submit" name="submit" value="提交表单" />  
  10.       
  11.     }  
  12.     else  
  13.     {  
  14.         //如果提交了表单  
  15.         //数据库连接参数  
  16.         $host = "localhost";  
  17.         $user = "root";  
  18.         $pass = "zq19890319";  
  19.         $db = "phpdev";  
  20.           
  21.         //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止SQL注入  
  22.         $country = empty($_POST['country'])? die("请输入国家名称"):  
  23.         mysql_escape_string($_POST['country']);  
  24.         $animal = empty($_POST['animal'])? die("请输入英文名"):  
  25.         mysql_escape_string($_POST['animal']);  
  26.         $cname = empty($_POST['cname'])? die("请输入中文名"):  
  27.         mysql_escape_string($_POST['cname']);  
  28.           
  29.         //打开数据库连接 hovertree.com 何问起  
  30.         $connection = mysql_connect($host$user$passor die("Unable to connect!");  
  31.           
  32.         //选择数据库  
  33.         mysql_select_db($dbor die("Unable to select database!");  
  34.           
  35.         //构造一个SQL查询  
  36.         $query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname')";  
  37.           
  38.         //执行该查询  
  39.         $result = mysql_query($queryor die("Error in query: $query. ".mysql_error());  
  40.           
  41.         //插入操作成功后,显示插入记录的记录号  
  42.         echo "记录已经插入, mysql_insert_id() = ".mysql_insert_id();  
  43.           
  44.         //关闭当前数据库连接  
  45.         mysql_close($connection);  
  46.     }  
  47. ?> 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1130484.htmlTechArticlePHP表单数据写入MySQL代码,php表单写入mysql h1插入操作/h1 ? php if (! isset ( $_POST ['submit' ])){ // 如果没有表单提交,显示一个表单 ? form action="...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage