Showing posts with label Mysqli INSERT. Show all posts
Showing posts with label Mysqli INSERT. Show all posts

Monday, 3 September 2018

Insert in MySQL does not work and there is no error

I'm trying to insert products information into the database, but I must be missing something. It does not insert anything into the database, it's not even showing any errors.

Whats wrong with my code?
<?php 

    if(!empty($product_name)){
        if(!empty($product_category)){

        ////////////////////////////////////////////////
        ///// connect to database
        ////////////////////////////////////////////////

        $dbc = new mysqli("localhost", "root", "password", "table");

        // Check connection
        if (mysqli_connect_errno($dbc)) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        if (isset($_POST['product_name'])) {

            $pid = ($_POST['thisID']);
            $product_name = ($_POST['product_name']);
            $product_category = ($_POST['product_category']);
            $product_detail = ($_POST['product_detail']);
            $product_price = ($_POST['product_price']);
            $product_company = ($_POST['product_company']);

            $query = "INSERT INTO product (product_name, product_category, product_price, product_detail,product_detail, product_company)
                VALUES ( '$product_name','$product_category','$product_price','$product_detail','$product_company')";

            $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Data cannot inserted");

            if($result){
                header('location:cp_product.php');
            }

            mysqli_close($dbc);
        }
    }}

?>


In your query you have add the product_detail column two times,there should be one ,as five columns refers to 5 values
product_detail,product_detail,

INSERT INTO product (product_name, product_category, product_price, product_detail, product_company)
VALUES ( '$product_name','$product_category','$product_price','$product_detail','$product_company');

Tuesday, 28 August 2018

Iinsert mysqli does not insert all text

I am trying to insert some names in mysql database by using mysqli commands. The problem is whenever the Turkish character ü comes in text, the text in the database becomes just previous part of that character.

For example I try to insert : Humana Still Tee Anne Sütünü Arttıran Bitki Çayı
The text in database : Humana Still Tee Anne S
The column in database is a text column has utf-8 general_ci standards.
And these are my codes for inserting
 function mysqli_connector()
{
  $link = mysqli_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE) or    die("hata");
  $link->set_charset("utf8");
  mysqli_set_charset($link,"utf8");
  return $link;
}

function oc_product_description($p_id,$name){
  $link = mysqli_connector();
  $link->query("insert into oc_product_description (product_id,name,language_id,meta_title)values('$p_id','$name','1','$name')")or die(mysqli_error($link));
  mysqli_close($link);
}

oc_product_description(106,"Humana Still Tee Anne Sütünü Arttıran Bitki Çayı");

The problem occurs for ü letter i tried with other Turkish characters, it works fine.
Thanks for your time.

Characters Truncated Using Utf8 is usually caused by
  • The client had accented characters encoded in latin1, and
  • SET NAMES utf8 was in effect during the INSERT
It does not matter what the CHARACTER SET is on the table and column; the problem occurs during the transfer to the server.
For example, here were the steps for INSERTing é:
  • The client had the é encoded as hex E9, the latin1 encoding.
  • When transferring to the server, SET NAMES utf8 said (incorrectly) "The bytes in the client are encoded as utf8".
  • But there is no utf8 character encoded E9, so the string is truncated before the E9
That is, the string is stored up to, but not including, the invalid encoding.
I have talked about utf8 and latin1, but many (not all) pairs of character sets suffer from this "truncation" problem.
The data cannot be recovered from the table.
Going forward, the fix is to have these agree:
  • the encoding of the characters in the client
  • the declared encoding in SET NAMES (or equivalent)
Note that it does not matter what the target column's CHARACTER SET is; the purpose of SET NAMES is to convert between what the client has and what the column has. However, it is generally good to use utf8mb4 (or at least utf8) for all text.