kaaai3
kaaai3

Reputation: 39

deleting the displayed key from database in 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"] . " \r";
        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
}

?>

Upvotes: 0

Views: 71

Answers (1)

CodeBird
CodeBird

Reputation: 3858

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.

Upvotes: 1

Related Questions