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.

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

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线