注意:php中未定义的索引错误

时间:2013-03-30 04:31:59

标签: php mysql xampp

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

  

注意:未定义的索引:第21行的/opt/lampp/htdocs/disease.php中的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>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我是第二个布拉德:使用PDO&amp; amp;准备好的陈述 - 它更加优雅和高效。

这里有一些代码供你使用...因为我不知道你想用tokensizing做什么等等。我没有写过如何处理$ gene_name的逻辑,但我确定你这样做=)

看看http://www.php.net/manual/en/book.pdo.php 我还建议你在PDO上使用Doctrine作为包装器/ 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() );
}