找不到任何解决方案

时间:2019-05-29 17:32:56

标签: php mysql

我正在尝试放入MYSQL和XML文件,但是当我按一下放入MYSQL时,它会这样做,而当我按一下放入XML时,它将放入MYSQL。我不知道该如何解决。

<?php
include_once 'includes/DuombazesInfo.php'
?>

<?php
if(isset($_REQUEST['Ikelimas'])) {
$xml = new DOMDocument ("1.0","UTF-8");
$xml -> load("pavyzdysxml.xml");

$rootTag = $xml -> getElementsByTagName ("document") -> item(0);

$dataTag = $xml -> createElement("data");

$nameTag = $xml -> createElement("name", $_REQUEST['name']);
$lastnameTag = $xml -> createElement("lastname", $_REQUEST['lastname']);
$emailTag = $xml -> createElement("email", $_REQUEST['email']);
$passwordTag = $xml -> createElement("password", $_REQUEST['password']);

$dataTag -> appendChild($nameTag);
$dataTag -> appendChild($lastnameTag);
$dataTag -> appendChild($emailTag);
$dataTag -> appendChild($passwordTag);

$rootTag -> appendChild($dataTag);

$xml -> save("PavyzdysXML.xml");
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$lastname = test_input($_POST["lastname"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["password"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Lentele su duomenu baze</title>
</head>
<body>
<h2>Savarankiškas darbas</h2>
<form action="includes/prisijungimas.php" method="POST">
<input type="text" name="name" placeholder="Vardas" required>
<br><br>
<input type="text" name="lastname" placeholder="Pavardė" required>
<br><br>
<input type="email" name="email" placeholder="El. Paštas">
<br><br>
<input type="text" name="password" placeholder="Slaptažodis" required>
<br><br>
<button type="submit" name="Įkelti"> Įkelti į duomenų bazę </button>
<input type="button" value="Peržiūrėti duomenų bazės informaciją" 
class="homebutton" id="btnHome" onClick="parent.location='index1.php'"/>
<input type="button" value="Peržiūrėti XML failą" class="homebutton" 
id="btnHome" onClick="parent.location='PavyzdysXML.xml'" />
<input type="submit" name="Ikelimas" value ="Įkelti į XML failą">
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应在文件include / prisijungimas.php中添加检查:

<?php
include_once 'DuombazesInfo.php';

//If button with name "Ikelimas" was clicked
if(isset($_REQUEST['Ikelimas'])) { 
   //Generete XML here (code from first message)
   $xml = new DOMDocument("1.0","UTF-8");
   $xml->load("pavyzdysxml.xml");

   $rootTag = $xml->getElementsByTagName ("document") -> item(0);

   $dataTag = $xml->createElement("data");

   $nameTag = $xml->createElement("name", $_REQUEST['name']);
   $lastnameTag = $xml->createElement("lastname", $_REQUEST['lastname']);
   $emailTag = $xml->createElement("email", $_REQUEST['email']);
   $passwordTag = $xml->createElement("password", $_REQUEST['password']);

   $dataTag->appendChild($nameTag);
   $dataTag->appendChild($lastnameTag);
   $dataTag->appendChild($emailTag);
   $dataTag->appendChild($passwordTag);

   $rootTag->appendChild($dataTag);

   $xml->save("PavyzdysXML.xml");
} else {
   //Insert into DB (code from includes/prisijungimas.php)

   // Kodo eilute skirta apsaugoti duomenis, kad ju negaletu istrinti pasinaudojant formos langais
   $vardas = mysqli_real_escape_string($conn, $_POST['name']);
   $pavarde = mysqli_real_escape_string($conn, $_POST['lastname']);
   $pastas = mysqli_real_escape_string($conn, $_POST['email']);
   $slaptazodis = mysqli_real_escape_string($conn, $_POST['password']);
   $md5pass = md5($slaptazodis);

   // Kodas skirtas įdėti į duomenų bazę duomenis kurie buvo suvesti formoje
   $sql = "INSERT INTO vartotojai1 (vardas, pavarde, pastas, slaptazodis)
   VALUES ('$vardas', '$pavarde', '$pastas ', '$slaptazodis');";
   mysqli_query($conn, $sql);
}

header("Location: ../index.php");