Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/158947-undefined-variable-and-index/
Share on other sites

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.