pcw Posted May 20, 2009 Share Posted May 20, 2009 Hi, I have the following script to select and restore a database, but I keep getting errors. if(isset($_COOKIE['sbadmin'])) { # INCLUDE THE FILE CONTAINING THE MYSQL CONNECTION VARIABLES include_once("data/required.php"); # CONNECT TO MYSQL DATABASE if ($error=true) { $link = @mysql_connect($dbhost,$dbuser,$dbpass); if ($link) $db = mysql_select_db($db); if (!$link || !$db) { print ("<p class=\"error\">The following error occured: ".mysql_error()."</p>\n"); $error=true; } } else { $link = false; } // REQUIRED SETTINGS $linesThisSession = 3000; define ('DATA_CHUNK_LENGTH',16384); foreach ($_REQUEST as $key => $val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val); $_REQUEST[$key] = $val; } $error = false; $file = false; # CREATES THE DATABASE SELECTION FORM $backupFileDir = "backup/"; $handle = opendir($backupFileDir); while ($filename = readdir($handle)) { $filenames[] = $filename; } closedir($handle); sort($filenames); print "</br>"; print "<center>"; print "<h2 class=style2 align=center>Restore Database</h2>"; print "<p class=style8>This function allows you to restore MySQL databases that have previously been backed up using the MySQL database backup function. Select the database you wish to restore from the menu below and submit the request.</p>"; print "</center>"; print "<form action=\"".$_SERVER["PHP_SELF"]."?cmd=sqlimport&import=1&filename=$filename&\" method='POST'>"; print "<table bgcolor=#0066FF align=center>"; print "<tr><td>"; print "<select name='filename'>"; print "<option>Choose Database Backup</option>"; foreach ($filenames as $filename) { if (($filename != ".") And ($filename != "..")) { print sprintf("<option value='$backupFile%s'>%s</option>", $filename, $filename); } } print "</select></td>"; print "<td><input type='submit' name='import' value='Start Import'></td></tr>"; print "</table></form>"; print "</center>"; print "</td>"; print "</tr>"; print "</table>"; # DEFINES THE FILENAME POSTED FROM THE DATABASE SELECTION FORM $filename = 'backup/'.$_POST['filename']; # CHECK THAT SELECTED FILE IS A .GZ FILE if (eregi("\.gz$",$filename)) $gzFile=true; else $gzFile=false; ((!$gzFile && !$file=@fopen($filename,"rt")) || ($gzFile && !$file=@gzopen($filename,"rt"))); # START IMPORTING BACKUP TO DATABASE if (!$error && isset($_REQUEST["import"]) && eregi("(\.(gz))$",$filename)) $lineNum = $_REQUEST['import']; $querylines=0; # PROCESS IF MAX LINES ARE NOT REACHED while ($lineNum<$_REQUEST['import']+$linesThisSession || $query!="") { $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n") { if (!$gzFile) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; # REMOVE \\ FROM DUMPLINE $noslash = str_replace ("\\\\","",$dumpline); $query .= $dumpline; # EXECUTE THE QUERY IF END OF QUERY IS REACHED if (ereg(";$",trim($dumpline))) { if (!mysql_query(trim($query), $link)) { print ("<p class=\"error\">Error at the line $lineNum: ". trim($dumpline)."</p>\n"); print ("<p>Query: ".trim(nl2br(htmlentities($query)))."</p>\n"); print ("<p>MySQL: ".mysql_error()."</p>\n"); $error=true; break; } $query=""; } $lineNum++; } # CONFIRM DATABASE HAS BEEN RESTORED AND THEN RESTART THE SCRIPT if(isset($_POST['import'])) { print "<center></br><p class=style8>MySQL Database: <b>$filename</b> has been restored successfully.</p></center>\n"; } if ($link) mysql_close(); if ($file && !$gzFile) fclose($file); else if ($file && $gzFile) gzclose($file); } else { print "<p class=style2>You are not authorised to access this page</p>"; } And these are the errors I get Notice: Undefined index: filename in /home/moveitho/public_html/cgi-bin/sitebuilder/admin.php on line 1210 Notice: Undefined index: import in /home/moveitho/public_html/cgi-bin/sitebuilder/admin.php on line 1230 Notice: Undefined variable: lineNum in /home/moveitho/public_html/cgi-bin/sitebuilder/admin.php on line 1230 How do I fix this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/ Share on other sites More sharing options...
radi8 Posted May 20, 2009 Share Posted May 20, 2009 Be sure to initialize these variables at the top of the page or section where they are being used. These variables exist in the code but when the form processed they were not referenced or used. Since they are in the form cache state table, it generates a WARNING. If you initialize the variables somewhere that they will always be touched by the process, then these warnings will go away. Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838315 Share on other sites More sharing options...
pcw Posted May 20, 2009 Author Share Posted May 20, 2009 Hi thanks for your reply. I moved things about and got this case "sqlimport": if(isset($_COOKIE['sbadmin'])) { # INCLUDE THE FILE CONTAINING THE MYSQL CONNECTION VARIABLES include_once("data/required.php"); # CONNECT TO MYSQL DATABASE if ($error=true) { $link = @mysql_connect($dbhost,$dbuser,$dbpass); if ($link) $db = mysql_select_db($db); if (!$link || !$db) { print ("<p class=\"error\">The following error occured: ".mysql_error()."</p>\n"); $error=true; } } else { $link = false; } // REQUIRED SETTINGS $linesThisSession = 3000; define ('DATA_CHUNK_LENGTH',16384); foreach ($_REQUEST as $key => $val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val); $_REQUEST[$key] = $val; } $error = false; $file = false; # CREATES THE DATABASE SELECTION FORM $backupFileDir = "backup/"; $handle = opendir($backupFileDir); while ($filename = readdir($handle)) { $filenames[] = $filename; } closedir($handle); sort($filenames); print "</br>"; print "<center>"; print "<h2 class=style2 align=center>Restore Database</h2>"; print "<p class=style8>This function allows you to restore MySQL databases that have previously been backed up using the MySQL database backup function. Select the database you wish to restore from the menu below and submit the request.</p>"; print "</center>"; print "<form action=\"".$_SERVER["PHP_SELF"]."?cmd=sqlimport&import=1&filename=$filename&\" method='POST'>"; print "<table bgcolor=#0066FF align=center>"; print "<tr><td>"; print "<select name='filename'>"; print "<option>Choose Database Backup</option>"; foreach ($filenames as $filename) { if (($filename != ".") And ($filename != "..")) { print sprintf("<option value='$backupFile%s'>%s</option>", $filename, $filename); } } print "</select></td>"; print "<td><input type='submit' name='import' value='Start Import'></td></tr>"; print "</table></form>"; print "</center>"; print "</td>"; print "</tr>"; print "</table>"; # CONFIRM DATABASE HAS BEEN RESTORED AND THEN RESTART THE SCRIPT if(isset($_POST['import'])) { # DEFINES THE FILENAME POSTED FROM THE DATABASE SELECTION FORM $filename = 'backup/'.$_POST['filename']; # CHECK THAT SELECTED FILE IS A .GZ FILE if (eregi("\.gz$",$filename)) $gzFile=true; else $gzFile=false; ((!$gzFile && !$file=@fopen($filename,"rt")) || ($gzFile && !$file=@gzopen($filename,"rt"))); # START IMPORTING BACKUP TO DATABASE if (!$error && isset($_REQUEST["import"]) && eregi("(\.(gz))$",$filename)) $lineNum = $_REQUEST['import']; $querylines=0; # PROCESS IF MAX LINES ARE NOT REACHED while ($lineNum<$_REQUEST['import']+$linesThisSession || $query != "") { $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n") { if (!$gzFile) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; # REMOVE \\ FROM DUMPLINE $noslash = str_replace ("\\\\","",$dumpline); $query .= $dumpline; # EXECUTE THE QUERY IF END OF QUERY IS REACHED if (ereg(";$",trim($dumpline))) { if (!mysql_query(trim($query), $link)) { print ("<p class=\"error\">Error at the line $lineNum: ". trim($dumpline)."</p>\n"); print ("<p>Query: ".trim(nl2br(htmlentities($query)))."</p>\n"); print ("<p>MySQL: ".mysql_error()."</p>\n"); $error=true; break; } $query=""; } $lineNum++; } print "<center></br><p class=style8>MySQL Database: <b>$filename</b> has been restored successfully.</p></center>\n"; } if ($link) mysql_close(); if ($file && !$gzFile) fclose($file); else if ($file && $gzFile) gzclose($file); } else { print "<p class=style2>You are not authorised to access this page</p>"; } break; But now get this error Notice: Undefined variable: query in /home/moveitho/public_html/cgi-bin/sitebuilder/admin.php on line 1251 I have echoed query and it shows the database contents that are to be restored. Why isnt this working? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838329 Share on other sites More sharing options...
pcw Posted May 21, 2009 Author Share Posted May 21, 2009 Does anyone know how to fix this? I cant seem to define $query If I echo query it prints all the information in the mysql backup file, so I dont understand what is going wrong Any help is much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838841 Share on other sites More sharing options...
trq Posted May 21, 2009 Share Posted May 21, 2009 Where do you define $query ? Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838853 Share on other sites More sharing options...
pcw Posted May 21, 2009 Author Share Posted May 21, 2009 Problem is, I dont quite understand what that means. Do you mean where do I give $query a value? Im not sure how I would do this as it appears to have a value when I echo it. If I remove query from this line, I get endless amounts of errors, I really am confused as to what to do Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838865 Share on other sites More sharing options...
trq Posted May 21, 2009 Share Posted May 21, 2009 Problem is, I dont quite understand what that means. Do you mean where do I give $query a value? Yes. A variable is defined when you first assign it a value. So, your still not sure where you defined $query? That makes two of you, PHP isn't either. Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838875 Share on other sites More sharing options...
pcw Posted May 21, 2009 Author Share Posted May 21, 2009 I wouldnt have of thought echoing $query would display anything if it wasnt already defined. This is driving me mad lol Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838894 Share on other sites More sharing options...
KevinM1 Posted May 21, 2009 Share Posted May 21, 2009 I wouldnt have of thought echoing $query would display anything if it wasnt already defined. This is driving me mad lol You should show us all code from where $query is defined/assigned to the line where the error is being reported. Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838895 Share on other sites More sharing options...
pcw Posted May 21, 2009 Author Share Posted May 21, 2009 That is everything - there is no more to the script. It restores the database as planned but shows that error Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838897 Share on other sites More sharing options...
trq Posted May 21, 2009 Share Posted May 21, 2009 That isn't 1251 lines of code. Are you sure your looking at the relevent code? Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838903 Share on other sites More sharing options...
pcw Posted May 21, 2009 Author Share Posted May 21, 2009 As you can see, the sqlimport is within case and break; tags, it is part of a huge script, and the rest of it has not relevance to importing a database. Quote Link to comment https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/#findComment-838924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.