【问题标题】:Notice: Undefined index error in php注意:php中未定义的索引错误
【发布时间】:2013-03-20 20:20:52
【问题描述】:

我的表中使用分隔符 / 存储了数据。我需要将它们分开并需要将它们存储在不同的表中。在这样做的同时,我得到:

注意:未定义索引:/opt/lampp/htdocs/disease.php 第 21 行中的 VSX1

我该如何解决这种错误?

<html>
<body>
    <?php
        $username = "root";
        $password = "****";
        $hostname = "localhost"; 
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
        $selected = mysql_select_db("disease",$dbhandle) or die("Could not select disease");
    $result = mysql_query("SELECT * FROM `primary_data` LIMIT 0, 30");
        while($row = mysql_fetch_array($result))
        {
    $string = $row['gene_name'];
    $tok = strtok($string, "/");
    while ($tok !== false) {
    mysql_query("insert into temp_gene gene_name   values('".$_POST[$tok]."')");
        $tok = strtok("/");
    }

        }
        mysql_close($dbhandle);
    ?>
    </table>    
    </body>
    </html>

【问题讨论】:

  • 您正在做的事情非常不安全。如果您还没有被黑客入侵,那么您将会被黑客入侵。学习使用 PDO 或类似的准备好的/参数化查询。另外,如果可以的话,不要在表格中存储带分隔符的数据。

标签: php mysql xampp


【解决方案1】:

您从一个表传输数据并将其保存到另一个表。您根本不需要 $_POST 变量! 当然,必须很好地转义数据。

while (($tok = strtok($string, '/')) !== false) {
    $tok = mysql_real_escape_string($tok);
    mysql_query("INSERT INTO temp_gene(gene_name) VALUES('{$tok}')");
}

【讨论】:

    【解决方案2】:

    我赞同 Brad:s 关于使用 PDO 和准备好的语句的建议 - 它更加优雅和高效。

    这是给你的一些代码......因为我不知道你想对标记化做什么等。我没有写出如何处理 $gene_name 的逻辑,但我相信你会这样做 =)

    看看http://www.php.net/manual/en/book.pdo.php 我还建议您使用 Doctrine 作为 PDO 之上的包装器/ORM,它使事情变得非常简单:http://www.doctrine-project.org/

    $dsn = "mysql:dbname={$db_name};host={$db_host}";
    try {
        // init db handler.
        $db = new PDO( $dsn, $db_username, $password );
    
        // Execute selecting query.
        $select_sql = "SELECT gene_name FROM `primary_data` LIMIT 0, 30";
        $select_stmt = $db -> prepare( $sql );
        $select_stmt -> execute();
    
        // Bind row column 1 (1-indexed) to $gene_name.
        $select_stmt -> bindColumn( 1, $gene_name );
    
        // Prepare insert query to temp_gene.
        $temp_sql = "INSERT INTO temp_gene(gene_name) VALUES(?)";
        $temp_stmt = $db -> prepare( $temp_sql );
    
        // Begin transaction, it is more efficient to use transactions as your actual queries becomes 1 instead of O(rows).`enter code here`
        $db -> beginTransaction();
    
        while ( $row = $select_stmt -> fetch( PDO::FETCH_BOUND ) ) {
            $string =& $gene_name;
    
            // do your tokenizing logic here... also do escaping for security.
    
            $temp_stmt -> execute( array( $your_value_string ) );
        }
    
        // Commit the inserts.
        $db -> commit();
    } catch (PDOException $e) {
        die( $e->getMessage() );
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多