Saving Array Parts to different fields in database

I have a database with a few thousands keyword fields that have 10 space delimited keywords. I would like to explode them into an array and save the parts into individual fields within the database. I know I need to normalize the database and upgrade to mysqli, but that’s not important yet. I’m new at this, and I can’t figure out what is wrong with my code. For instance, after assigning the portions of the array to strings, I can echo the strings, but I can’t save those strings to my database.

Any help would be much appreciated!



// MySQL Select-Comment Query

$result = mysql_query("SELECT keyword FROM keyword WHERE keyword_id = 3");

$row = mysql_fetch_array($result);

// Explode String

$keywordString = ($row['keyword']);
$keywordArray = explode(" " , $keywordString);

// Assign Array Strings

$keyWord1 = $keywordArray [0];
$keyWord2 = $keywordArray [1]; 
$keyWord3 = $keywordArray [2]; 
$keyWord4 = $keywordArray [3]; 
$keyWord5 = $keywordArray [4]; 
$keyWord6 = $keywordArray [5]; 
$keyWord7 = $keywordArray [6]; 
$keyWord8 = $keywordArray [7]; 
$keyWord9 = $keywordArray [8]; 
$keyWord10 = $keywordArray [9]; 

// Save Query to Database

mysql_query(

"INSERT INTO `keyword`

(
`keyword1`,
`keyword2`,
`keyword3`,
`keyword4`,
`keyword5`,
`keyword6`,
`keyword7`,
`keyword8`,
`keyword9`,
`keyword10`
) 

VALUES

(
'$keyword1',
'$keyword2',
'$keyword3',
'$keyword4',
'$keyword5',
'$keyword6',
'$keyword7',
'$keyword8',
'$keyword9',
'$keyword10'

)"

)

or die(mysql_error()); 


// Close Session

mysql_close(); 


You are probably getting array key doesn’t exist or undefined index error if less that ten words are searched for or defined. Check for the KEY when setting variable.


$keyWord1 = (array_key_exists(0,$keywordArray) ? $keywordArray[0] :'');
$keyWord2 = (array_key_exists(1,$keywordArray) ? $keywordArray[1] :''); 
$keyWord3 = (array_key_exists(2,$keywordArray) ? $keywordArray[2] :''); 
$keyWord4 = (array_key_exists(3,$keywordArray) ? $keywordArray[3] :''); 
$keyWord5 = (array_key_exists(4,$keywordArray) ? $keywordArray[4] :''); 
$keyWord6 = (array_key_exists(5,$keywordArray) ? $keywordArray[5] :''); 
$keyWord7 = (array_key_exists(6,$keywordArray) ? $keywordArray[6] :''); 
$keyWord8 = (array_key_exists(7,$keywordArray) ? $keywordArray[7] :''); 
$keyWord9 = (array_key_exists(8,$keywordArray) ? $keywordArray[8] :''); 
$keyWord10 = (array_key_exists(9,$keywordArray) ? $keywordArray[9] :'');

Fixed!

I tested sending less than 10 keywords as well as 10 keywords, and both behaved the same … a new empty row in the table. So I deleted the table and made it again using PHPMyAdmin. Same behavior.

So I again deleted the table and instead used a PHP create table function, and now the saving array bit works. I don’t know what setting I had wrong when I created the table in PHPMyAdmin, but it wouldn’t allow anything to be saved. Now it saves to the keyword slots, and anything fewer than ten remains empty.

Also note that variable names are case-sensitive in PHP, so this:


$keyWord1 = $keywordArray [0];

in your assignment code is not the same as this


'$keyword1',

in your query.

Fixed.

Not sure what the problem was. I created the original table via PHPMyAdmin. I deleted and recreated the table, and when it still didn’t work, I deleted the table but recreated it using a PHP / MySQL script. The code is now saving to the database as desired. I’m unsure why the PHPMyAdmin table wouldn’t allow entries, but this newly created table does. Thanks!