将用户输入与php中的变量进行比较

时间:2011-11-04 07:51:04

标签: php

嗨,我是新手使用PHP,我仍然试图找到我的方式。我有一个表单,我想更新数据库中的信息。我有一个文本框,用户输入id我希望将用户输入的id与数据库中的Id进行比较,以便更新该用户的信息。我一直在寻找,但无法找到我正在寻找的东西。

1 个答案:

答案 0 :(得分:-3)

假设您在文件 formular.html 中有以下公式,必须将信息发送到文件 receive.php

<form action="receive.php" method="post">
   Name : <input type="text" name="user_id" />
   <input type="submit"/>
</form>

根据公式的发送方法发布这一事实。这是 receive.php 的样子

<?php
    // receiving information
    $id = $_POST['user_id'];

    // connecting to database to retrive id to be compare
    $link = mysql_connect("localhost","root","");
    mysql_select_db("sampleDatabase",$link);

    // writing your query
    $query = "SELECT id FROM sampleTable WHERE 1;";

    // executing your query
    $result = mysql_query($query, $link);*

    // fetching the result
    $row = mysql_fetch_array($result);

    // testing the similarity between information from the database and the one from the formular
    if($id == $row['id']) // make a decision
    else // make another decision

    // for dealing whit many line on the result use the following code
    while($row = mysql_fetch_array($result)){
       // make a stuff with $row
    } 

    // may be you could want to do a redirection if so the use the following header function

    header("Location: formular.html");
?>