简体   繁体   中英

PHP/MYSQL - Insert multiple rows into one column, works via phpmyadmin but only partially via myqli prepared statement?

Using a mysqli prepared statement I would like to insert an array into a mysql database table.

Being aware that bind-param and arrays do not go together, we would like to write the query in php first, then process this as a prepared statement:

$tagQuery = "INSERT INTO word_tags(speaks) VALUES ";
// Count total array values
$icoderayInsideCount = count($icoderayInside);
foreach ($icoderayInside as $icoderayInsideKey=>$icoderayInsideValue)
{
    // Last value
    //  Currrent Array Key    Total / Last Array Value
    if ($icoderayInsideKey == $icoderayInsideCount)
    {
        $tagQuery .= "('$icoderayInsideValue')";
    }
    // All other values
    else
    {
        $tagQuery .= "('$icoderayInsideValue'), ";
    }           
}

// Send array (keywords) to database
if ($stmt2 = $link2->prepare($tagQuery))
{
    if (!$stmt2->execute())
    {
        // #2 If it can prepare but can't execute, why?
        echo "Error {$link2->errno}  :  {$link2->error} (Cant execute?) <br/>";
        // Dump query to check the end result
        var_dump($tagQuery);
        exit();
    }
    $stmt2->close();
}
else
{
    // #1 If it cant prepare, why?
    echo "Error {$link2->errno}  :  {$link2->error} (Cant prepare?)";
    exit();
}

When i run this via PHP / Server i get:

Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('DONGLES'),' at line 1 (Cant prepare?)

So, the statement prepares but is not being executed.

The value of the generated query, $tagQuery is:

INSERT INTO word_tags(speaks) VALUES ('PMP3670B_BK'), ('PRESTIGIO'), ('MULTIPAD'), ('7"'), ('800X480'), ('1GHZ'), ('ARM'), ('CORTEX'), ('A8'), ('CPU'), ('512MB'), ('DDR3'), ('DRAM'), ('ANDROID'), ('4.1'), ('JELLY'), ('BEAN'), ('MALI'), ('400'), ('MP'), ('GPU'), ('VIDEO'), ('4GB'), ('INTERNAL'), ('FLASH'), ('MEMORY'), ('SUPPORT'), ('32GB'), ('SDHC/SD'), ('USB/WI-FI/HEADSET'), ('PORT'), ('LITHIUM'), ('POLYMER'), ('BLACK'), ('HDMI'), ('OUTPUT'), ('UPTO'), ('1080'), ('HD'), ('USB2.0'), ('MINI'), ('HIGH'), ('SPEED'), ('FOR'), ('3G'), ('DONGLE'), ('OTG'), ('CABLE'), ('INCLUDED')

The fourth value from the end is ('DONGLE') which is what the error message is complaining about.

When i run this exact same query through phpmyadmin there is no error involved.

What i assume is happening, is that there is some kind of length limit involved within creating a prepared statement... Or something to this effect.

Have scratched my brains for hours now to try to solve this and have not found any relating information.

If anyone could offer some assistance / advice / indication / input or otherwise as to what the conflict of problem may be within this, it would be GREATLY appreciated.

Thanks so much for the time and effort in readying through this!

EDIT:

@Mihai - Thanks for the thought.

It seems that the word dongle does have something string to it. In the original string, before being parsed to an array, it looks like this: DONGLE,

I run preg_replace to remove this comma from the string before parsing it to an array:

$icode = preg_replace('#,#', '', $icode);

Then into an array:

$icoderayInside = explode(" ", $icode);

Still cannot think of any reason this would cause a conflict as the output string, the query is as i have previously stated and includes no comma, or anything... Any would be greatly appreciated!

EDIT 2:

@ShadyAtef

Original input is stored in mysql as varchar, latin_general_ci:

PRESTIGIO MULTIPAD, 7" 800X480, 1GHZ ARM CORTEX A8 CPU, 512MB DDR3 DRAM, ANDROID 4.1 JELLY BEAN, MALI 400 MP GPU, VIDEO, 4GB INTERNAL FLASH MEMORY, SUPPORT 32GB SDHC/SD, USB/WI-FI/HEADSET PORT, LITHIUM POLYMER, BLACK, HDMI OUTPUT UPTO 1080 HD, USB2.0 MINI HIGH SPEED PORT FOR 3G DONGLE, OTG CABLE INCLUDED

Brought into php then processed to an array with additional requirements:

// Convert Var a + b to String      
$icode = $itemCode . ' ' . $description;  
// Clean of Unwanteds
$icode = preg_replace('#,#', '', $icode);
$icode = preg_replace('#\(#', '', $icode);
$icode = preg_replace('#\)#', '', $icode);
// Creates array from sting
$icoderayInside = explode(" ", $icode);
// Remove array duplicates
$icoderayInside = array_unique($icoderayInside);

Before being built into the query. Any assistance would be GREATLY appreciated!

EDIT 3:

@ShadyAtef

//  Currrent Array Key    Total / Last Array Value
if ($icoderayInsideKey == $icoderayInsideCount)
{
    // dump here shows:
    $icoderayInsideKey == 49
    $icoderayInsideCount == 49
}

This was really tricky,but I got it : The tricky wrong part is that

    if ($icoderayInsideKey == $icoderayInsideCount)

It should be

        if ($icoderayInsideKey == ($icoderayInsideCount-1))

Because the last key in the array equals to the array (length -1) So you should change your if condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM