TorresAlGrande
TorresAlGrande

Reputation: 213

Sending 0 values to database

I created my first ajax script which sends data to database. But one problem, there are 0 values in new creatred row. In Firebug, i can see that there are these values on php file.

Parametersapplication/x-www-form-urlencodedDo not sort
currentTasken   
1415
currentUser 
37
Source
currentUser=37&currentTasken=1415

But somehow they can't reach database, here is my php file.

<?php
$currentUser = isset($_POST['$currentUser']);
$currentTasken = isset($_POST['$currentTasken']);
    $con = mysql_connect("localhost", "root", "") or die(mysql_error());
    if(!$con)
        die('Could not connectzzz: ' . mysql_error());
    mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());

    $check = mysql_query("SELECT * FROM dotp_task_log");
    $numrows = mysql_num_rows($check);
    if($numrows >= 1)
    {
        //$pass = md5($pass);

            $ins = mysql_query("INSERT INTO dotp_task_log (task_log_creator, task_log_Task) VALUES ('$currentUser' , '$currentTasken')" ) ;

       if($ins)
            die("Succesfully Created Log!");

        else
            die("ERROR");

    }
    else
    {
        die("Log already exists!");
    }

?>

Here is database

5   0   NULL    NULL    0   0   NULL        0   0   NULL

Upvotes: 2

Views: 178

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10356

You're having 2 bugs in your code.

$currentUser = isset($_POST['$currentUser']);

1: I believe you're mistaken and you meant:

$currentUser = isset($_POST['currentUser']);

2: isset is a function that returns a boolean value in case the variable is defined and set or not. So the $currentUser variable now holds the boolean value (0 or 1) and not the value of the posted data.

You should use it like that:

$currentUser = isset($_POST['currentUser']) ? $_POST['currentUser'] : '';

This is a short version of an if-else condition. In case that the $_POST['currentUser'] is set, the variable $currentUser should hold its value, otherwise - it would have an empty string.

Further notes:

  • Keep in mind that you should sanitize the $_POST data.
  • You're using mysql_* which is deprecated. Consider using mysqli or PDO instead.

Upvotes: 7

Related Questions