0

This is my server side code:

$pic=$_FILES['file-0']['tmp'];
$type    = $_POST['type'];
$address = $_POST['address'];
$nums    = $_POST['nums'];
$lng     = $_POST['lng'];
$lat     = $_POST['lat'];
$signal  = $_POST['sigs'];
$time    = $_POST['time'];
$date    = $_POST['date'];
$image_name=uploadImage($_FILES);
$db = new mysqli( '', '', '', '' );//deleted the connection string parameters

if ( $db->connect_errno > 0 ) {
    die( 'Unable to connect to database [' . $db->connect_error . ']' );
} else {
    echo 'connected to db';
}

if(!($stmt=$db->prepare("INSERT INTO mytable(number, address, signals, time, date, lat, lng, type,image) VALUES (?,?,?,?,?,?,?,?,?)"))){
    echo "Prepare failed: (" . $db->errno . ") " . $db->error;
}

if(!($stmt->bind_param("isissiiss",$nums, $address, $signal, $time, $date, $lat, $lng, $type,'user-img/'.$image_name))){
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if(!($stmt->execute())){
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

I'm using jquery ajax and try to get the error like this but it alerts just the whole html file markup without any errors:

  $.ajax({
        type:'POST',
        url:'../index.php',
        contentType:false,
        processData : false,
        data:data,
        success: function(data) {
            $('#load-page').fadeOut();
        },
        error:function(xhr, status, error) {
            alert(xhr.responseText);
        }
    })

I'm sure that this works too because when I use this code without prepare and just write to the database with mysqli->query() then everything works fine. I have no access to the servers log files and can't read it so I tried to alert out the error like in the code above but it doesn't work. for the $stmt->bind_param() type parameters I tried to put everything as string (s) but didn't work too. I don't know what is wrong with my code. Any ideas?

DevMan
  • 538
  • 1
  • 5
  • 25
  • 2
    Have you checked your error logs on the server? It gives more details than just "error 500". – aynber Jan 10 '17 at 15:49
  • i have no access to the servers error logs – DevMan Jan 10 '17 at 15:49
  • @DevMan: Have you turned on error reporting? Somewhere something is reporting an error. You're going to have to find out what that error is before you can correct it. If you don't have access to the server logs, find someone who does. – David Jan 10 '17 at 15:50
  • is there a way to print out error logs without having access to the server log errors? – DevMan Jan 10 '17 at 15:52
  • @David this is the error I get: Fatal error: Cannot pass parameter 10 by reference in /home/mysite/www/index.php on line75. I don't know what parameter 10 is because I have just 9 parameters. Line 75 is the line where I bind my parameters – DevMan Jan 10 '17 at 16:16
  • The problem is that string concatenation for last value. As a workaround, populate a new variable as a separate statement, right before the bind_param... `$foo = 'user-img/'.$image_name;` and then reference `$foo` in the `bind_param`. `mysqli_stmt_bind_param` passes parameters *by reference*, so you need to pass a reference. (If you were using PDO, you could workaround the issue using `bindValue` instead of `bindParam`.) – spencer7593 Jan 10 '17 at 16:31
  • @spencer7593 yes I did it and like $img='user-img/'.$image_name and the problem solved. Thanks – DevMan Jan 10 '17 at 16:32

0 Answers0