www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] MySQL data not inserting

  1. #1
    Join Date
    Oct 2013
    Posts
    12

    resolved [RESOLVED] MySQL data not inserting

    So, I am putting a drawing app on my website, but one thing is that no matter what I try, the save info is not inserting into my 'gallery' database. I have all the table data set up right, but when I try to get the insert working, it doesn't.
    PHP Code:
    <?
    include('../include/session.php');
    # GENERATE RANDOM STRING
    function generateRandomString($length 5){
    $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString '';
    for (
    $i 0$i $length$i++) {
    $randomString .= $characters[rand(0strlen($characters) - 1)];
    }
    return 
    $randomString;
    }
    $randomstring generateRandomString();
    $img mysql_real_escape_string(stripslashes($_POST['data']));
    $img str_replace('data:image/png;base64,'''$img);
    $img str_replace(' ''+'$img);
    $data base64_decode($img);
    $file "drawings/".strtolower($session->username)."-".$randomstring.".png";
    file_put_contents($file$data);
     
    # NOW INSERT STUFF INTO THE DATABASE
     
    $url=$file;
    $desc=$_POST['desc'];
    $name=$_POST['title'];
     
    mysql_query("INSERT INTO gallery(artist,title,desc,drawing)VALUES('$session->username','$name','$desc','$file')");
     
    ?>
    Note that the included "session.php" file contains all the database connections. My friend (who made the original script, but it doesn't fit with my database) couldn't seem to help either. Please tell me what I am doing wrong. Thanks!

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    776
    Add this to your script to see what errors this code is generating. That should help.

    [code]
    error_reporting(E_ALL | E_STRICT | E_NOTICE);
    ini_set('display_errors', '1');
    set_time_limit(2);
    [\code]

  3. #3
    Join Date
    Oct 2013
    Posts
    12
    It gives me no error whatsoever. That code is also in "session.php".

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    776
    Are you checking the results of your query execution? I don't see it.

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    The query seemed to be hitting reserved words (desc), and assuming the data being stored didn't contain any unescaped quotes it should have been ok afterwards.

    Try this query, for your own databases safety if nothing else:

    PHP Code:
    mysql_query('INSERT INTO gallery (`artist`,`title`,`desc`,`drawing`) VALUES ("' 
        
    mysql_real_escape_string($session->username) . '","' 
        
    mysql_real_escape_string($name)              . '","' 
        
    mysql_real_escape_string($desc)              . '","' 
        
    mysql_real_escape_string($file)              . '")'
    ); 
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    Oct 2013
    Posts
    12
    Quote Originally Posted by bionoid View Post
    The query seemed to be hitting reserved words (desc), and assuming the data being stored didn't contain any unescaped quotes it should have been ok afterwards.

    Try this query, for your own databases safety if nothing else:

    PHP Code:
    mysql_query('INSERT INTO gallery (`artist`,`title`,`desc`,`drawing`) VALUES ("' 
        
    mysql_real_escape_string($session->username) . '","' 
        
    mysql_real_escape_string($name)              . '","' 
        
    mysql_real_escape_string($desc)              . '","' 
        
    mysql_real_escape_string($file)              . '")'
    ); 
    No, desc is short for "Description"/

  7. #7
    Join Date
    Oct 2013
    Posts
    12
    Quote Originally Posted by bionoid View Post
    The query seemed to be hitting reserved words (desc), and assuming the data being stored didn't contain any unescaped quotes it should have been ok afterwards.

    Try this query, for your own databases safety if nothing else:

    PHP Code:
    mysql_query('INSERT INTO gallery (`artist`,`title`,`desc`,`drawing`) VALUES ("' 
        
    mysql_real_escape_string($session->username) . '","' 
        
    mysql_real_escape_string($name)              . '","' 
        
    mysql_real_escape_string($desc)              . '","' 
        
    mysql_real_escape_string($file)              . '")'
    ); 
    Excuse the previous irrelevant comment, but it appeared to work! Thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles