Jump to content

Special Characters and MySQLi Prepared Statements


Shadow Jolteon

Recommended Posts

Hello,

 

I've been working on a website for a client, and am having a bit of trouble with mysqli functions... Basically, everything works fine when the SQL I'm using is just selecting from the database, however, when I use update or insert, special characters do not go into the database properly. For example, the accented "é" character is inserted as "é".

 

After doing quite a bit of searching around, as well as reading many pages on php.net, I found one thing that helps for update and insert, adding set_charset( "utf8" ), but this causes the output of select to do the same thing that the other two were previously. Here is some of the code I'm working with right now that is affected by this:

$mysqli = new mysqli( "localhost", "user", "pass", "db" );
$mysqli -> set_charset( "utf8" );

if( $_GET['mode'] == "edit" && $_GET['drink'] && $_POST['e_name'] ) {
    $update = "update `menu` set `name` = ?, `description` = ?, `small` = ?, `medium` = ?, `large` = ?, `flavors` = ?, `section` = ?, `order` = ? where `id` = ?;";

    $id = $_GET['drink'];
    $nName = $_POST['e_name'];
    $nDesc = $_POST['e_desc'];
    $nSml = $_POST['e_prc1'];
    $nMed = $_POST['e_prc2'];
    $nLrg = $_POST['e_prc3'];
    if( $_POST['e_flav'] == "on" )
        $nFlv = 1;
    else
        $nFlv = 0;
    $nSect = $_POST['e_sect'];
    $nOrd = $_POST['e_ordr'];

    if( $changeIt = $mysqli -> prepare( $update ) ) {
            $changeIt -> bind_param( 'ssdddiiii', $nName, $nDesc, $nSml, $nMed, $nLrg, $nFlv, $nSect, $nOrd, $id );
        $changeIt -> execute();
    }

    header( "Location: http://phpfreaks.com/" );
    exit;
}

$mysqli -> close();

 

Additionally, the character set of the database is "utf8_unicode_ci", and I do have the HTTP Content-Type headers set as UTF-8 on both the server and HTML sides.

 

Thank you for reading.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.