php mysql简单更新:无法读取值

时间:2013-10-22 13:15:58

标签: php mysql

可能是愚蠢的问题,但...... 我有一个PHP和MySQL的示例,它不起作用... 有3个php文件:

  1. list_records.php
  2. update.php
  3. update_ac.php
  4. 问题是第3个没有从第2个读取变量。我做错了什么?

    以下是我的文件/代码:

    list_records.php

    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="test_mysql"; // Table name 
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td colspan="4"><strong>List data from mysql </strong> </td>
    </tr>
    
    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Lastname</strong></td>
    <td align="center"><strong>Email</strong></td>
    <td align="center"><strong>Update</strong></td>
    </tr>
    
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    
    <tr>
    <td><?php echo $rows['name']; ?></td>
    <td><?php echo $rows['lastname']; ?></td>
    <td><?php echo $rows['email']; ?></td>
    
    
    <td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
    </tr>
    
    <?php
    }
    ?>
    
    </table>
    </td>
    </tr>
    </table>
    
    <?php
    mysql_close();
    ?>
    

    update.php

    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="test_mysql"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // get value of id that sent from address bar
    $id=$_GET['id'];
    
    // Retrieve data from database 
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    $rows=mysql_fetch_array($result);
    ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <form name="form1" method="post" action="update_ac.php">
    <td>
    <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>&nbsp;</td>
    <td colspan="3"><strong>Update data in mysql</strong> </td>
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Lastname</strong></td>
    <td align="center"><strong>Email</strong></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td align="center">
    <input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
    </td>
    <td align="center">
    <input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
    </td>
    <td>
    <input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
    <input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
    </td>
    <td align="center">
    <input type="submit" name="Submit" value="Submit">
    </td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    
    <?php
    // close connection 
    mysql_close();
    ?>
    

    update_ac.php

    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="test_mysql"; // Table name 
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // update data in mysql database 
    $sql="UPDATE $tbl_name SET name='TEST', lastname='$lastname', email='$email' WHERE id='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='list_records.php'>View result</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>
    

3 个答案:

答案 0 :(得分:0)

您需要从POST文件{/ 1}}中获取它们,例如

update_ac.php

答案 1 :(得分:0)

这是您在更新查询中所需的内容

......lastname='".$_POST['lastname']."', email='".$_POST['email']."' WHERE id=".$_POST['id']

每当form发布/向action中提到的文件提交值时,该文件必须访问$_POST数组中表单字段名称的值$_POST['lastname'], $_POST['email'] 。等(如果method使用的POSTform代码中的{{1}})

访问提交的值有三种方式:

  1. 获取http://php.net/manual/en/reserved.variables.get.php
  2. POST http://php.net/manual/en/reserved.variables.post.php
  3. 请求http://php.net/manual/en/reserved.variables.request.php

答案 2 :(得分:0)

你需要在插入或更新数据库之前从客户端获取服务器端的值。为此我们在PHP中有超全局变量,如$ _GET,$ _ POST,$ _ REQUEST等。 所以在update_ac.php中包含这些:     $lastname=$_POST[lastname];     $email=$_POST[email]; 的$ id = $ _ POST [ID];`

如果您不确定从表单中传递值的方法,可以使用$ _REQUEST方法。但请尽量避免使用它,因为它不太安全。