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

数据库:db
表名:msg
三个字段:
ID、user和sms

现有一网页使用POST方式提交了一个数据,该网页表单中的两个输入框名称分别为user和sms。

请问,我如何使用PHP来获取这个POST值,并将值以追加的方式添加到数据库中呢?

烦请给出完整的代码。

200分伺候。
qvbfndcwy,那数据库连接的代码麻烦也给下啊

<?
$db_host = "localhost";//链接的数据库地址,也就是主机名字
$db_user = "db";//数据库名字
$db_pass = "数据库密码";
$db_name = "msg";//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db($db_name,$connec) or die ("不能选择数据库: ".mysql_error());
$user=$_POST['user']; //$_post不用大写的就没用得
$sms=$_POST['sms'];
$ID=$_POST['id'];
$db_query='INSERT INTO msg(表名) VALUES $user,$sms,$ID';//插入
mysql db query($db_query);//运行sql语句
?>

上面的程序改改就可以用了,或许有问题,我在网吧,没调试的!
我也是学PHP的,现在还很菜,有时间的话咱交流交流!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-26

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
------------------

<form action="post.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit">
</form>

post.php

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

<?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. ";
}
?>

第2个回答  2009-09-03
$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';
}
第3个回答  2009-09-03
$db_host = "localhost";
$db_user = "数据库名";
$db_pass = "数据库密码";
$db_name = "表名";
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db($db_name,$connec) or die ("不能选择数据库: ".mysql_error());
第4个回答  2009-09-03
<?php
$user=$_POST['user'];
$sms=$_POST['sms'];
?>
然后就是数据库连接了。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网