Search code examples
mysqlwhitespacemysql-insert-id

Blank spaces in mysql_insert_id(); return


im getting blank spaces before my id?

when it comes back i get this?

&id=%20%20%20%20%20%20%20%20%20%20%20%201005

its supposed to just do this

&id=1005

Could it be my jquery data code?

    success: function(data) { 
    $('.success').slideDown('slow');  
    setTimeout(function(){window.location="user.php?v=single&id="+data;}, 3000);            
    }   

or could it be the php code?

<?php
include 'includes/db.php';
protect();
if(isset(clean($_POST['uid']))) { 
$usrid=$_SESSION['uid'];
$insertNewUser=mysql_query("INSERT INTO users (uid,status,datetime) VALUES  ('$usrid','0',NOW())") or die(mysql_error());
$id=mysql_insert_id();
echo $id;
$insertUserOptions=mysql_query("INSERT INTO users_options (uid,lid) VALUES  ('$usrid','$id')") or die(mysql_error());
mkdir('media/listings/'.$id,0755);
mkdir('media/listings/'.$id.'/photos',0755);
mkdir('media/listings/'.$id.'/documents',0755);
mkdir('media/listings/'.$id.'/video',0755);
mkdir('media/listings/'.$id.'/audio',0755);
chmod('media/listings/'.$id,0775);
chmod('media/listings/'.$id.'/photos',0775);
chmod('media/listings/'.$id.'/documents',0775);
chmod('media/listings/'.$id.'/video',0775);
chmod('media/listings/'.$id.'/audio',0775);
} 
?> 

UPDATE:

all the characters infront of the id were because i ad several lines of file includes in the protect() function. Once i moved those includes into a new function and called that accordingly it fixed the issue. Thanks martin for your clear solution as well :).


Solution

  • Try this, its not a solution but a workaround!

    $('.success').slideDown('slow');  
        setTimeout(function(){window.location="user.php?v=single&id="+$.trim(data);}, 3000);            
    }   
    

    It's uses the jQuery trim function to remove the spaces before and after. It should do the trick.

    To actually fix it you can do a few other things:

    Option 1

    Return the ID as an json string, so the spaces are not visible.

    Option 2

    Check your files for echo spaces or maybe sometimes closing the php tag where it's not necessary and echo'ing spaces to your result.