MySQL Insert Script does not insert all data.

I have a code that is made to take city and state names form a text file, and input random content strings as well as the state and abbreviation into a database. The code runs fine when just entering the content of the text file, but once you try and add in the random content, it doesnt insert all 50 states. Sometimes its in the 40s, and some in the 30s, but never all 50. Here is the code below.

states.txt

Alabama = al: Alaska = ak: Arizona = az: Arkansas = ar: California = ca: Colorado = co: Connecticut = ct: Delaware = de: Florida = fl: Georgia = ga: Hawaii = hi: Idaho = id: Illinois = il: Indiana = in: Iowa = ia: Kansas = ks: Kentucky = ky: Louisiana = la: Maine = me: Maryland = md: Massachusetts = ma: Michigan = mi: Minnesota = mn: Mississippi = ms: Missouri = mo: Montana = mt: Nebraska = ne: Nevada = nv: New Hampshire = nh: New Jersey = nj: New Mexico = nm: New York = ny: North Carolina = nc: North Dakota = nd: Ohio = oh: Oklahoma = ok: Oregon = or: Pennsylvania = pa: Rhode Island = ri: South Carolina = sc: South Dakota = sd: Tennessee = tn: Texas = tx: Utah = ut: Vermont = vt: Virginia = va: Washington = wa: West Virginia = wv: Wisconsin = wi: Wyoming = wy:

content.php

[php]<?php
// Sentance 1 Part 1
$p1s1v1p1 = “If you are in need of”;
$p1s1v2p1 = “If you are considering”;
$p1s1v3p1 = “If you are thinking about getting”;
$p1s1v4p1 = “If you are contemplating getting”;
$p1s1v5p1 = “If you are going to get”;
$p1s1v6p1 = “If you are getting ready to get”;
$p1s1v7p1 = “If you are looking for”;
$p1s1v8p1 = “If you are seeking”;
$p1s1v9p1 = “If your business is in need of”;
$p1s1v10p1 = “If your business is considering getting”;
$p1s1v11p1 = “If your business is looking for”;
$p1s1v12p1 = “If the next step for your business is”;
$p1s1v13p1 = “If your business is getting ready to purchase”;
$p1s1v14p1 = “If your business is considering purchasing”;
$p1s1v15p1 = “If you want”;

// Sentance 1 Part 2
$p1s1v1p2 = “website design”;
$p1s1v2p2 = “website design”;
$p1s1v3p2 = “website design”;
$p1s1v4p2 = “website design”;
$p1s1v5p2 = “website design”;
$p1s1v6p2 = “website design”;
$p1s1v7p2 = “website design”;
$p1s1v8p2 = “website design”;
$p1s1v9p2 = “website design”;
$p1s1v10p2 = “website design”;
$p1s1v11p2 = “website design”;
$p1s1v12p2 = “website design”;
$p1s1v13p2 = “website design”;
$p1s1v14p2 = “website design”;
$p1s1v15p2 = “website design”;

// Sentance 1 Part 3
$p1s1v1p3 = “, then you have come to the right place.”;
$p1s1v2p3 = “, then you can consider this your last stop.”;
$p1s1v3p3 = “, then you can stop your shopping now.”;
$p1s1v4p3 = “, then you have met your match.”;
$p1s1v5p3 = “, then you have found the professional service for you.”;
$p1s1v6p3 = “, then you have found the place to get professional service and savings.”;
$p1s1v7p3 = “, then you have come to the right place if you are looking to save while getting quality design.”;
$p1s1v8p3 = “, then you have come to the right place if you are looking for the best service with the most competitive prices.”;
$p1s1v9p3 = “, then you can make this your last stop if you want to get a combination of quality and a great price.”;
$p1s1v10p3 = “, then you need to look no further.”;
$p1s1v11p3 = “, then you need to look no further for the combination of quality and savings.”;
$p1s1v12p3 = “, then we are the place for you.”;
$p1s1v13p3 = “, then we are the place for you to start.”;
$p1s1v14p3 = “, then we are here to help.”;
$p1s1v15p3 = “, then you have met your match if you are looking for a great price and superior quality.”;

// Sentance 2 Part 1
$p1s2v1p1 = “Prior to getting started,”;
$p1s2v2p1 = “Before you get started,”;
$p1s2v3p1 = “Dont just start getting quotes just yet,”;
$p1s2v4p1 = “Dont just go out and get started,”;
$p1s2v5p1 = “Dont just run out and start asking for quotes,”;
$p1s2v6p1 = “Before you start getting quotes,”;
$p1s2v7p1 = “Before your business starts asking for quotes,”;
$p1s2v8p1 = “Before you start getting quotes for your business,”;
$p1s2v9p1 = “Prior to getting your businesses’ quotes,”;
$p1s2v10p1 = “Prior to requesting quotes for your site,”;
$p1s2v11p1 = “Prior to requesting your businesses website quotes,”;
$p1s2v12p1 = “Prior to receiving any quotes,”;
$p1s2v13p1 = “Before you get any quotes for your business website,”;
$p1s2v14p1 = “Before you ask for any business website quotes,”;
$p1s2v15p1 = “Prior to asking for any quotes for your businesses website,”;

// Sentance 2 Part 2
$p1s2v1p2 = “there are a few things you should know,”;
$p1s2v2p2 = “there are a few things that you should consider,”;
$p1s2v3p2 = “there are many things that you should consider,”;
$p1s2v4p2 = “there are many things that you should know first,”;
$p1s2v5p2 = “there are things that you need to consider first,”;
$p1s2v6p2 = “there are several things that you need to know,”;
$p1s2v7p2 = “there are many things to take into consideration,”;
$p1s2v8p2 = “there are several things to take into consideration,”;
$p1s2v9p2 = “there are a few things to take into consideration,”;
$p1s2v10p2 = “there are quite a few that you should consider,”;
$p1s2v11p2 = “there are a handful of things that you need to know,”;
$p1s2v12p2 = “there are a great number of things that you should know,”;
$p1s2v13p2 = “there are a handful of things to take into consideration,”;
$p1s2v14p2 = “there are an abundance of things to take into consideration,”;
$p1s2v15p2 = “there are a great deal of things that you should know,”;

// Sentance 2 Part 3
$p1s2v1p3 = “about web design in $statename.”;
$p1s2v2p3 = “about web design in $statename.”;
$p1s2v3p3 = “about web design in $statename.”;
$p1s2v4p3 = “about web design in $statename.”;
$p1s2v5p3 = “about web design in $statename.”;
$p1s2v6p3 = “about web design in $statename.”;
$p1s2v7p3 = “about web design in $statename.”;
$p1s2v8p3 = “about web design in $statename.”;
$p1s2v9p3 = “about web design in $statename.”;
$p1s2v10p3 = “about web design in $statename.”;
$p1s2v11p3 = “about web design in $statename.”;
$p1s2v12p3 = “about web design in $statename.”;
$p1s2v13p3 = “about web design in $statename.”;
$p1s2v14p3 = “about web design in $statename.”;
$p1s2v15p3 = “about web design in $statename.”;

///////////////////// Randomize Parts \\\\\\\\\\\
include(“randomize.php”);
?>[/php]

randomize.php

[php]

<?php //Paragraph 1 Sentance 1 $p1s1p1_array = array("$p1s1v1p1", "$p1s1v2p1", "$p1s1v3p1", "$p1s1v4p1", "$p1s1v5p1", "$p1s1v6p1", "$p1s1v7p1", "$p1s1v8p1", "$p1s1v9p1", "$p1s1v10p1", "$p1s1v11p1", "$p1s1v12p1", "$p1s1v13p1", "$p1s1v14p1", "$p1s1v15p1"); $p1s1p1 = $p1s1p1_array[array_rand($p1s1p1_array)]; $p1s1p2_array = array("$p1s1v1p2", "$p1s1v2p2", "$p1s1v3p2", "$p1s1v4p2", "$p1s1v5p2", "$p1s1v6p2", "$p1s1v7p2", "$p1s1v8p2", "$p1s1v9p2", "$p1s1v10p2", "$p1s1v11p2", "$p1s1v12p2", "$p1s1v13p2", "$p1s1v14p2", "$p1s1v15p2"); $p1s1p2 = $p1s1p2_array[array_rand($p1s1p2_array)]; $p1s1p3_array = array("$p1s1v1p3", "$p1s1v2p3", "$p1s1v3p3", "$p1s1v4p3", "$p1s1v5p3", "$p1s1v6p3", "$p1s1v7p3", "$p1s1v8p3", "$p1s1v9p3", "$p1s1v10p3", "$p1s1v11p3", "$p1s1v12p3", "$p1s1v13p3", "$p1s1v14p3", "$p1s1v15p3"); $p1s1p3 = $p1s1p3_array[array_rand($p1s1p3_array)]; //Paragraph 1 Sentance 2 $p1s2p1_array = array("$p1s2v1p1", "$p1s2v2p1", "$p1s2v3p1", "$p1s2v4p1", "$p1s2v5p1", "$p1s2v6p1", "$p1s2v7p1", "$p1s2v8p1", "$p1s2v9p1", "$p1s2v10p1", "$p1s2v11p1", "$p1s2v12p1", "$p1s2v13p1", "$p1s2v14p1", "$p1s2v15p1"); $p1s2p1 = $p1s2p1_array[array_rand($p1s2p1_array)]; $p1s2p2_array = array("$p1s2v1p2", "$p1s2v2p2", "$p1s2v3p2", "$p1s2v4p2", "$p1s2v5p2", "$p1s2v6p2", "$p1s2v7p2", "$p1s2v8p2", "$p1s2v9p2", "$p1s2v10p2", "$p1s2v11p2", "$p1s2v12p2", "$p1s2v13p2", "$p1s2v14p2", "$p1s2v15p2"); $p1s2p2 = $p1s2p2_array[array_rand($p1s2p2_array)]; $p1s2p3_array = array("$p1s2v1p3", "$p1s2v2p3", "$p1s2v3p3", "$p1s2v4p3", "$p1s2v5p3", "$p1s2v6p3", "$p1s2v7p3", "$p1s2v8p3", "$p1s2v9p3", "$p1s2v10p3", "$p1s2v11p3", "$p1s2v12p3", "$p1s2v13p3", "$p1s2v14p3", "$p1s2v15p3"); $p1s2p3 = $p1s2p3_array[array_rand($p1s2p3_array)]; ?>[/php]

runstates.php
[php]

<?php include("database/connect.php"); $data = file_get_contents('states.txt'); // Get the file content $data = str_replace(array("\n", "\r"), '', $data); // Clear newline characters $data = explode(':', $data); // Get each record by : at end of line unset($data[count($data) - 1]); // Clear the last empty element $final_array = array(); foreach($data AS $row){ // Loop the exploded data $final_array[] = explode(' = ', $row); // Explode each row by Space=Space to each row of final_array } //////////////////////////////////////////////////// DONT FORGET TO CHANGE THE DILIMETER \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $row = 0; while ($row < 50) { $statename = $final_array[$row][0]; $abrev = $final_array[$row][1]; include("content.php"); $insert = mysql_query("INSERT INTO states (state, abrev, p1s1p1, p1s1p2, p1s1p3, p1s2p1, p1s2p2, p1s2p3) VALUES ('$statename', '$abrev', '$p1s1p1', '$p1s1p2', '$p1s1p3', '$p1s2p1', '$p1s2p2', '$p1s2p3')"); echo "$row entered
"; $row++; } ?>

[/php]

Any reason as to why it dosent input all 50 states when the runstates.php is ran?

Most likely a syntax issue. Firstly, you don’t need to put quotes around all your variables in randomize.php - they are strings, wrapping them in quotes is pointless and forces PHP to parse the non concatenated variables as part of another string - just use the variables on their own. Secondly, each field is encapsulation with single quotes in your INSERT statement like such:

INSERT INTO states (state, abrev, p1s1p1, p1s1p2, p1s1p3, p1s2p1, p1s2p2, p1s2p3) VALUES ('$statename', '$abrev', '$p1s1p1', '$p1s1p2', '$p1s1p3', '$p1s2p1', '$p1s2p2', '$p1s2p3')

It’s good practice to concatenate by the way, instead of:
[php]“My cousin said ‘$sentence’.”[/php]

You should do:
[php]“My cousin said '”.$sentence."’."[/php]

Anyway, I digress - some of your sentences have single quotes in them (apostrophes), when in your statement it makes the code something like:

VALUES ('my isn't it a lovely day','yes','i agree')

Note how the ’ in “isn’t” is the same character that opens the string - this leads to a parsing/syntax issue. In order to resolve this you should wrap the potentially ‘dangerous’ strings in mysql_escape_string (though soon to be deprecated in PHP 5.5.0)

Sponsor our Newsletter | Privacy Policy | Terms of Service