php - 从 db 检索数据并将其显示在 php 的表中。看到这段代码有什么问题吗?

标签 php database

  $db = mysql_connect("localhost", "root", "");
  $er = mysql_select_db("ram");
  $query = "insert into names values('$name','$add1','$add2','$mail')";
  $result = mysql_query($query);
  print "<p> Person's Information Inserted </p>";
  $result = mysql_query("SELECT * FROM names");
?>

<table border="2">
   <tr>
      <th>Name</th>
      <th>Address Line 1</th>
      <th>Address Line 2 </th>
      <th>E-mail Id </th>
    </tr>
    <? 
    while ($array = mysql_fetch_row($result));
    {
        print "<tr> <td>";
        echo $array[0]; 
        print "</td> <td>";
        echo $array[1]; 
        print "</td> <td>";
        echo $array[2]; 
        print "</td> <td>";
        echo $array[3]; 
        print "</td> </tr>";
    }
?>

最佳答案

试试这个:

<?php

 # Init the MySQL Connection
  if( !( $db = mysql_connect( 'localhost' , 'root' , '' ) ) )
    die( 'Failed to connect to MySQL Database Server - #'.mysql_errno().': '.mysql_error();
  if( !mysql_select_db( 'ram' ) )
    die( 'Connected to Server, but Failed to Connect to Database - #'.mysql_errno().': '.mysql_error();

 # Prepare the INSERT Query
  $insertTPL = 'INSERT INTO `name` VALUES( "%s" , "%s" , "%s" , "%s" )';
  $insertSQL = sprintf( $insertTPL ,
                 mysql_real_escape_string( $name ) ,
                 mysql_real_escape_string( $add1 ) ,
                 mysql_real_escape_string( $add2 ) ,
                 mysql_real_escape_string( $mail ) );
 # Execute the INSERT Query
  if( !( $insertRes = mysql_query( $insertSQL ) ) ){
    echo '<p>Insert of Row into Database Failed - #'.mysql_errno().': '.mysql_error().'</p>';
  }else{
    echo '<p>Person\'s Information Inserted</p>'
  }

 # Prepare the SELECT Query
  $selectSQL = 'SELECT * FROM `names`';
 # Execute the SELECT Query
  if( !( $selectRes = mysql_query( $selectSQL ) ) ){
    echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
  }else{
    ?>
<table border="2">
  <thead>
    <tr>
      <th>Name</th>
      <th>Address Line 1</th>
      <th>Address Line 2</th>
      <th>Email Id</th>
    </tr>
  </thead>
  <tbody>
    <?php
      if( mysql_num_rows( $selectRes )==0 ){
        echo '<tr><td colspan="4">No Rows Returned</td></tr>';
      }else{
        while( $row = mysql_fetch_assoc( $selectRes ) ){
          echo "<tr><td>{$row['name']}</td><td>{$row['addr1']}</td><td>{$row['addr2']}</td><td>{$row['mail']}</td></tr>\n";
        }
      }
    ?>
  </tbody>
</table>
    <?php
  }

?>

注意事项、注意事项和注意事项

在将值传递到数据库之前,您的初始解决方案没有显示任何明显的值处理。这就是 SQL 注入(inject)攻击(或者甚至是通过 SQL 传递的无意错误)发生的方式。不要这样做!

您的数据库似乎没有主键。虽然从技术上讲,这些在所有使用中都不是必需的,但它们是一种很好的做法,并且可以提供一种更可靠的方式来引用表中的特定行,无论是添加相关表还是在该表中进行更改。

您需要在每个阶段检查每个操作是否有错误。大多数 PHP 函数都足够好,可以在错误情况下返回响应。您的工作是随时检查这些条件 - 永远不要假设 PHP 会按照您的期望、方式和顺序执行。事故就是这样发生的……

我上面提供的代码包含很多点,如果发生错误,将返回一条消息。尝试一下,看看是否报告了任何错误消息,查看错误消息,如果适用,返回错误代码并进行一些研究。

祝你好运。

关于php - 从 db 检索数据并将其显示在 php 的表中。看到这段代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4331353/

相关文章:

android - phonegap android 中的数据库未定义错误?

node.js - Sequelize 配置以检索带有详细信息的总数

database - 为什么两个表不能有同名索引?

php - 如何从存在重复节点名称的嵌套集表中获取每个节点的深度

php - Codeigniter 查询显示 num 行 1 但行返回空数组

php - 如何使用 PHP 创建正确的 mySQL CREATE TABLE 语句

php - 在php中获取mysql导出下载url

java - 从PHP页面post到Java程序并输出结果

database - 重复删除和加载相同数据后 H2 数据库大小持续增加

python - 错误 1318 : Incorrect number of arguments for PROCEDURE ComicHub. sp_createUser;预期 3,得到 4