PDA

View Full Version : problem updating database


gilgalbiblewhee
04-29-2008, 02:17 PM
I don't understand where I'm going wrong in this:
for($j=1; $j<=18; $j++){
$sql = "UPDATE boti_pages SET chapter = '".$chapTitles[$j][0]."' WHERE page_num >= '".$chapTitles[0][$j]."' AND page_num < '".$chapTitles[0][$j+1]."'";
echo $sql."<br />";
}

$chapTitles[1][0] = "Introduction";
$chapTitles[0][1] = "1";

$chapTitles[2][0] = "chapter 1";
$chapTitles[0][2] = "5";

$chapTitles[3][0] = "chapter 2";
$chapTitles[0][3] = "17";

$chapTitles[4][0] = "chapter 3";
$chapTitles[0][4] = "18";

$chapTitles[5][0] = "chapter 4";
$chapTitles[0][5] = "27";

$chapTitles[6][0] = "chapter 5";
$chapTitles[0][6] = "48";

Echo-ing the SQL I get:
UPDATE boti_pages SET chapter = 'Introduction' WHERE page_num >= '1' AND page_num < '5'
UPDATE boti_pages SET chapter = 'chapter 1' WHERE page_num >= '5' AND page_num < '17'
UPDATE boti_pages SET chapter = 'chapter 2' WHERE page_num >= '17' AND page_num < '18'
UPDATE boti_pages SET chapter = 'chapter 3' WHERE page_num >= '18' AND page_num < '27'
UPDATE boti_pages SET chapter = 'chapter 4' WHERE page_num >= '27' AND page_num < '48'
UPDATE boti_pages SET chapter = 'chapter 5' WHERE page_num >= '48' AND page_num < '62'

But there is no entry into the database in the chapter field.

JohnDubya
04-29-2008, 02:34 PM
I set up a database like yours and ran those queries on it, and it works fine. Are you getting any errors or anything?

Wait, are you actually running the query through the mysql_query() function? You must do that for PHP to actually send the query to your database. Like this:


for ($j=1; $j<=18; $j++){
$sql = "UPDATE boti_pages SET chapter = '".$chapTitles[$j][0]."' WHERE page_num >= '".$chapTitles[0][$j]."' AND page_num < '".$chapTitles[0][$j+1]."'";
echo $sql."<br />";
mysql_query($sql);
}

gilgalbiblewhee
04-29-2008, 03:50 PM
Thanks! That's what was missing.