I am doing a database backup / restore system for my project. So far I have been able to back up the database of my systems without problems, but I can not get it restored from the "Restore" section that I have done, and all due to the way the database is supported. data.
The database is supporting me with the data in this way
CREATE TABLE 'citas' (
'idCitas' int(11) NOT NULL AUTO_INCREMENT,
'fCita' date DEFAULT NULL,
'horaAtencion' varchar(20) DEFAULT NULL,
'Examen_idExamen' int(11) NOT NULL,
'nombreExamen' varchar(55) DEFAULT NULL,
'Personas_cedula' varchar(13) NOT NULL,
'fechaSolicitud' datetime DEFAULT NULL,
'idEstatus' varchar(60) DEFAULT NULL,
PRIMARY KEY ('idCitas'),
KEY 'fk_Citas_Examen1_idx' ('Examen_idExamen'),
KEY 'fk_Citas_Personas1_idx' ('Personas_cedula'),
CONSTRAINT 'fk_Citas_Examen1' FOREIGN KEY ('Examen_idExamen') REFERENCES 'examen' ('idExamen') ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT 'fk_Citas_Personas1' FOREIGN KEY ('Personas_cedula') REFERENCES 'personas' ('cedula') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
The problem this is the part where it starts to show the main key and the foreign key simply because the dating table is the one that is being created first that the table people, and from so much trying, I went to phpmyadmin to put all the sql to see where the error occurred, and indeed it is there saying the following:
1215 - Can not add foreign key constraint
I would like to make the backup of the database save it as it does phpmyadmin when exporting the tables: first create all the tables, then insert them if they exist and finally the foreign key assignment and primary How could that be achieved? or how could I do to restore my database being that way?
To back up, I apply the following code
<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlUserName = "root";
$mysqlPassword = "1234";
$mysqlHostName = "localhost";
$DbName = "dycasys";
$backup_name = "mdycasys.sql";
$tables = array("sesion", "personas", "tipousuario", "informes", "familiar", "examen", "estatuscita", "citas");
//or add 5th parameter(array) of specific tables: array("mytable1","mytable2","mytable3") for multiple tables
Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName, $tables, $backup_name=false );
function Export_Database($host,$user,$pass,$name, $tables, $backup_name=false )
{
$mysqli = new mysqli($host,$user,$pass,$name);
$mysqli->select_db($name);
$mysqli->query("SET NAMES 'utf8'");
$queryTables = $mysqli->query('SHOW TABLES');
while($row = $queryTables->fetch_row())
{
$target_tables[] = $row[0];
}
if($tables !== false)
{
$target_tables = array_intersect( $target_tables, $tables);
}
foreach($target_tables as $table)
{
$result = $mysqli->query('SELECT * FROM '.$table);
$fields_amount = $result->field_count;
$rows_num=$mysqli->affected_rows;
$res = $mysqli->query('SHOW CREATE TABLE '.$table);
$TableMLine = $res->fetch_row();
$content = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n";
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0)
{
while($row = $result->fetch_row())
{ //when started (and every after 100 command cycle):
if ($st_counter%100 == 0 || $st_counter == 0 )
{
$content .= "\nINSERT INTO ".$table." VALUES";
}
$content .= "\n(";
for($j=0; $j<$fields_amount; $j++)
{
$row[$j] = str_replace("\n","\n", addslashes($row[$j]) );
if (isset($row[$j]))
{
$content .= '"'.$row[$j].'"' ;
}
else
{
$content .= '""';
}
if ($j<($fields_amount-1))
{
$content.= ',';
}
}
$content .=")";
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
{
$content .= ";";
}
else
{
$content .= ",";
}
$st_counter=$st_counter+1;
}
} $content .="\n\n\n";
}
//$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
$date = date("Y-m-d");
$backup_name = $backup_name ? $backup_name : $name.".$date.sql";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$backup_name."\"");
echo $content; exit;
}
?>