dsgtew3241 2014-10-28 10:38
浏览 17
已采纳

在php中删除数据库中显示的键

so i have this key giveaway script, now i want to get the displayed key deleted from the database. how do i get this to work within the code i wrote?

so $key is the key that will be send to you and display in the browser, but i want this key to get deleted out of the database after it is send and displayed so it cannot get shown a second time to another user.

<?php
 //fill in mail

echo "
<form method='post' action=" . $_SERVER['PHP_SELF'] . ">
    Email: <input name='email'></input><br>
    <input type='submit' value='Get your key' name='submit'> </input><br><br>
</form>";

    if(empty($_POST["email"]))
    {
        echo "Please enter an email adress.";
    }
    else{
        // get key from database
        $key = dispres();

        //mail key to input mail
        $to      = $_POST["email"];
        $subject = 'Your test key';
        $message = 'Your key is: ' . $key;
        $headers = 'There you go!';

        mail($to, $subject, $message, $headers);

        echo "Your code has been sent to your email: " . $_POST["email"] . " ";
        echo $key;
    }



function dispres(){

//database connect
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'pwd';

$database = 'c3keys';
$table = 'test';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");


//grab random key from database

$result = mysql_query("SELECT * FROM {$table} order by RAND() LIMIT 1");

$row = mysql_fetch_row($result);

$result = implode('|',$row);



return $result;

//delete key from database


//had this as a begin
mysqli_query("DELETE FROM test WHERE test='$key'");

//remember ip adress for 1 use only
}

?>
  • 写回答

1条回答 默认 最新

  • douxiji8707 2014-10-28 13:49
    关注

    1- you're using both mysql and mysqli stick with one...

    2- your delete query is after the return so it will never run.

    3- what is $key? I don't see where you set it.

    4- STOP USING mysql_* these functions are deprecated. Use mysqli or PDO with prepared statements.

    Explanation point 1:

    In your code at the beginning of your dispres function, you have mysql_connect, mysql_select_db and mysql_query at the end for your delete query you used mysqli_query notice the i in the last function, you can't use mysqli here, as you had connected with mysql adapter.

    Explanation point 2:

    You have return $result; then after that you have mysqli_query("..."); PHP won't execute that line of code because for PHP and any other programming language when they see a return this means the function is done, nothing more to do, so you can't have any line that you want executed after the return

    Explanation point 3:

    In your whole dispres function there isn't $key so basically $key is empty, I think you mean using $result in that query. And even if you use $result your delete won't work, because $result is a string where you joined all your fields into it and added | pipe sign between each one and the other. so column test will never be equal to your $result. You should replace $key by the actual value of the test column you want to delete maybe like this:

    $row = mysql_fetch_row($result);
    $test_i_want_to_delete=$row[0];//Where 0 is the number of column of `test` in your db table, starting counting from 0
    $result = implode('|',$row);
    mysql_query("DELETE FROM test WHERE test='$test_i_want_to_delete'");
    

    Explanation point 4:

    If you're just starting to learn PHP it would be much better for you not to learn any deprecated functions that will be totally removed in future releases. So at the place of using mysql_* functions, take a look at PDO or mysqli, and especially look at how to use prepared statements.

    I hope my answer helps clear out some stuff.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?