简体   繁体   English

向数据库PHP添加ID号

[英]Adding ID number to database PHP

This script hashes a list of words with MD5 and stores it in the database along with its plain text: 此脚本使用MD5散列单词列表,并将其及其纯文本存储在数据库中:

<?php
    $dict = $_POST["dict"];
if (!isset($_POST['submit'])){
    echo "<form method=\"post\" action=\"\">";
    echo "Wordlist: <input type=\"text\" name=\"dict\"><br>";
    echo "<input type=\"submit\" value=\"GET DIC\" name=\"submit\">";
    echo "</form>";
}else{
    $file = fopen($dict, "r");
    while (!feof ($file)){
    $load[$i] = fgets($file, 1024);
    mysql_connect("xxx","xxx","xxx");
    mysql_select_db("xxx");
    $word = $load[$i];
    $md5 = md5($word);
    $lastid=mysql_insert_id();
    $lastid=$lastid+1;
    mysql_query("INSERT INTO md5 VALUES ('$lastid', '$md5', '$word');");
    $i++;
    $limit = count($load);
    $width2 = $limit;
}
    fclose($file);
    echo "[+]Loaded ".$limit." word.";
    echo "<br>Done.";
}
?>

I'm having troubles generating the ID for each row. 我在为每一行生成ID时遇到麻烦。 I want the script to begin the number of ID from the last number that is stored in the database. 我希望脚本从存储在数据库中的最后一个数字开始ID的数字。

for example: 例如:

ID : MD5 : Country
1  : HASH: Africa
2  : HASH: Russia

When I execute the script I want it to start from ID number 3 and carries on until the list is finished. 当我执行脚本时,我希望它从ID号3开始,一直进行到列表完成为止。 What's wrong with my script? 我的脚本怎么了?

In your table, set the ID field to autoincrement. 在表中,将“ ID”字段设置为“自动递增”。 Then remove the whole ID section from your code, and let the database handle it 然后从代码中删除整个ID部分,然后由数据库处理

delete: $lastid=mysql_insert_id(); 删除: $lastid=mysql_insert_id();

Delete: $lastid=$lastid+1; 删除: $lastid=$lastid+1;

Change to: mysql_query("INSERT INTO md5 (md5, country) VALUES ( '$md5', '$word');"); 更改为: mysql_query("INSERT INTO md5 (md5, country) VALUES ( '$md5', '$word');");

Delete: $i++; 删除:$ i ++;

I would suguest that you use auto incremement that will save you a lot of lime. 我建议您使用自动校正功能,这样可以节省大量石灰。 Because Auto increment starts to add from the previous number as you said that it will be 3. 因为自动增量会从您说的前一个数字开始添加,所以您会说是3。

Consider using prepared statements (with mysqli or PDO). 考虑使用准备好的语句(使用mysqli或PDO)。 Otherwise you will get into trouble if any word will contain an apostrophe. 否则,如果任何单词包含撇号,您将遇到麻烦。 Additionally, mysql_ functions are deprecated and will be removed in future. 此外,不建议使用mysql_函数,以后将删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM