dpp34603 2013-12-28 07:54
浏览 49
已采纳

将ID号添加到数据库PHP

This script hashes a list of words with MD5 and stores it in the database along with its plain text:

<?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. I want the script to begin the number of ID from the last number that is stored in the database.

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. What's wrong with my script?

  • 写回答

3条回答 默认 最新

  • douzhaocheng4533 2013-12-28 08:01
    关注

    In your table, set the ID field to autoincrement. Then remove the whole ID section from your code, and let the database handle it

    delete: $lastid=mysql_insert_id();

    Delete: $lastid=$lastid+1;

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

    Delete: $i++;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧