为了账号安全,请及时绑定邮箱和手机立即绑定

PHP的POST方法和操作数据库的代码

PHP的POST方法和操作数据库的代码

PHP
绝地无双 2019-04-09 09:04:13
表名:msg三个字段:ID、user和sms现有一网页使用POST方式提交了一个数据,该网页表单中的两个输入框名称分别为user和sms。请问,我如何使用PHP来获取这个POST值,并将值以追加的方式添加到数据库中呢?烦请给出完整的代码。
查看完整描述

4 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

php获取post参数的几种方式

1、$_POST['paramName'] 只能接收Content-Type: application/x-www-form-urlencoded提交的数据

 

2、file_get_contents("php://input") 适用大多数类型的Content-type

php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype="multipart/form-data"。

 

3、$GLOBALS['HTTP_RAW_POST_DATA']; 总是产生 $HTTP_RAW_POST_DATA 变量包含有原始的 POST 数据。此变量仅在碰到未识别 MIME 类型的数据时产生。$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用。

如果post过来的数据不是PHP能够识别的,你可以用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,比如 text/xml 或者 soap 等等。

demo:

应用

a.htm------------------

1

2

3

4

5

<form action="post.php" method="post">

<input type="text" name="user">

<input type="password" name="password">

<input type="submit">

</form>

post.php

-----------------

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

<?php

 

 if (!empty($_POST))

 {

    // Array of post values for each different form on your page.

    $postNameArr = array('F1_Submit', 'F2_Submit', 'F3_Submit');        

 

    // Find all of the post identifiers within $_POST

    $postIdentifierArr = array();

         

    foreach ($postNameArr as $postName)

    {

        if (array_key_exists($postName, $_POST))

        {

             $postIdentifierArr[] = $postName;

        }

    }

 

    // Only one form should be submitted at a time so we should have one

    // post identifier.  The die statements here are pretty harsh you may consider

    // a warning rather than this. 

    if (count($postIdentifierArr) != 1)

    {

        count($postIdentifierArr) < 1 or

            die("\$_POST contained more than one post identifier: " .

               implode(" ", $postIdentifierArr));

 

        // We have not died yet so we must have less than one.

        die("\$_POST did not contain a known post identifier.");

    }

          

    switch ($postIdentifierArr[0])

    {

    case 'F1_Submit':

       echo "Perform actual code for F1_Submit.";

       break;

 

    case 'Modify':

       echo "Perform actual code for F2_Submit.";

       break;

            

    case 'Delete':

       echo "Perform actual code for F3_Submit.";

       break;

    }

}

else // $_POST is empty.

{

    echo "Perform code for page without POST data. ";

}

?>


 


查看完整回答
反对 回复 2019-04-10
?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

<?php
$user=$_POST['user'];
$sms=$_POST['sms'];
?>
然后就是数据库连接了。

查看完整回答
反对 回复 2019-04-10
?
互换的青春

TA贡献1797条经验 获得超6个赞

$setting = array();
$setting['mysqlHost'] = 'localhost';
$setting['mysqlUser'] = 'root';
$setting['mysqlPass'] = '****';

$link = mysql_connect($setting['mysqlHost'], $setting['mysqlUser'], $setting['mysqlPass']) or die('Could not connect: ' . mysql_error());
mysql_select_db('db', $link) or die('Could not select database');

function dbEscape($link, $text)
{
if(get_magic_quotes_gpc()) {
$text = stripslashes($text);
}
return mysql_real_escape_string($text, $link);
}

function isExistUser($link, $user, $sms) {
$user = dbEscape($link, $user);
$sms = dbEscape($link, $sms);
$result = mysql_query("SELECT * FROM `msg` WHERE `user`='$user' and `sms` = '$sms'", $link);
$num = mysql_num_rows($result);
mysql_free_result($result);
return $num > 0;
}
//=============code start==================
$user = isset($_REQUEST['user']) ? $_REQUEST['user'] : '';
$sms = isset($_REQUEST['sms']) ? $_REQUEST['sms'] : '';

if (isExistUser($link, $user, $sms)) {
echo <<<script
<script>
alert('Exist the same user');
history.back();
</script>
script;
}
else {
$sql = "INSERT INTO `db`.`msg` (`user`, `sms`) VALUES ('$user', '$sms')";
mysql_query($sql, $link) or die('Failur');
mysql_close($link);
echo 'O le';
}


 


查看完整回答
反对 回复 2019-04-10
  • 4 回答
  • 0 关注
  • 1170 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号