1

I have read through many similar questions and had a look at many ajax php tutorials. But I still cannot find for what reason my array is unable to be passed to my PHP.

I post my JS array here:

    function passName() {

    $.ajax(
    {
        type: 'POST',
        url: 'eDBase.php',
        data: 'array=' + arrays,
        success: function(){

        }

    });
}

Where the data is recieved in PHP through:

$arrays = $_POST(['passed'],true);

if ($arrays !== "") {
    echo($arrays);
}

$arraypass = "INSERT INTO clientinfo(first_name, last_name, emails) 
VALUES($arrays[1],$arrays[2],$arrays[3])";

if($conn->query($arraypass === true)){
    echo'Candidate has been added';
}
else{
    echo 'Error thrown while adding candidate to database';
}

mysqli_close($conn);

In my PHP preview I get this error thrown: https://gyazo.com/57f7faa9ee0869f56f031112b33d29b6

Full code is here:

PHP: https://codeshare.io/50NQjL HTML: https://codeshare.io/ax1POd

Thanks guys I've been stuck on this issue for about a week now just can't seem to see the problem.

(My PHP code is taking this array and the data from it and then placing it into the database)

EDIT: Code causing error while trying to add to database:

$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email'];



$arraypass = "INSERT INTO clientinfo(first_name, last_name, emails) 
VALUES($fname,$lname,$email)";

if($conn->query($arraypass === true)){
    echo'Candidate has been added';
}
else{
    echo 'Error thrown while adding candidate to database';
}

mysqli_close($conn);
cola465
  • 113
  • 8
  • You're sending `$_POST['array']`, not `$_POST['passed']`. – Barmar Jan 25 '20 at 11:04
  • Is `$arrays` supposed to be an array or string? You're comparing it to a string in `if ($arrays !== "")`, but then you're using `$arrays[1]`, `$arrays[2]`, etc. – Barmar Jan 25 '20 at 11:05
  • Why are you sending an array at all? Why not `data: {first_name: fname, last_name: lname, emails: email}`? Then you can use `$_POST['first_name']`, etc. – Barmar Jan 25 '20 at 11:06
  • Thanks guys data is being passed fine, however I have a new issue. For some reason my arraypass variable causes an error when I try to insert it to my database where the code claims 'empty query' I have added the code causing the error to my main question – cola465 Jan 25 '20 at 11:17
  • You need quotes around strings in the query. But you should use a prepared statement instead of substituting variables. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Barmar Jan 25 '20 at 11:20
  • Aside from the sql injection, as I've had alot of people telling me to protect against that I still cannot seem to add the variables to the database. I have added quotes around the strings but my database table is still not being manipulated. – cola465 Jan 25 '20 at 11:25
  • 1
    Get rid of `=== true`. The value of `$arraypass === true` is `false`. When that's converted to a string, you get an empty string, so you're doing `$conn->query("")` – Barmar Jan 25 '20 at 11:27
  • You probably meant to write `if($conn->query($arraypass) === true){`. But you don't need to compare with `true`, `if` will check if the value is truthy automatically. – Barmar Jan 25 '20 at 11:29
  • 1
    Can you try this one,`$arrays = $_POST(['passed']); if (empty($arrays)) { echo "empty array"; }` and this `if($conn->query($arraypass) === true)` to this `if($conn->query($arraypass))` –  Jan 25 '20 at 12:04
  • @Dilek OP is passing `$_POST['array']`, unless changed after Barmar's comment... – jibsteroos Jan 25 '20 at 12:22
  • @jibsteroos I can see that in image OP is passing values, Error refers to line 49 which is this `$arrays = $_POST(['passed'],true);` so, removing true can make it work, he dont need true in there. –  Jan 25 '20 at 12:33

0 Answers0