首页 > 解决方案 > 在 XAMPP 上使用 PHP 创建 XML 数据文件在 Mac 上不起作用(显示空白页)

问题描述

我正在尝试在 XML 文件中添加一些数据。但是,这些代码在 Windows 上运行良好,但在 mac XAMPP 上却无法运行。我已经发布了下面的代码。点击“添加产品按钮”后,显示

test.php:1 POST http://localhost/xampp/test/test.php 500(内部服务器错误)

我不知道是因为 Mac 上的 XAMPP,还是因为代码。

 <!-- // php for adding to xml file, it will create a new file called pro.xml
 // in case you dont already have this file,
 //  or it will add new product to it if you already have this file. -->
 <?php

function debug_to_console($data) {
  $output = $data;
  if (is_array($output))
      $output = implode(',', $output);

  echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
$errors = array();
if(isset($_POST['add'])){ 
    $name = preg_replace('/[^A-Za-z0-9]/', '', $_POST['name']);

    $id =  $_POST['id'];
    $price = $_POST['price'];

     if($name == ''){
        $errors[] = 'product Name is blank';}
        if($id == ''){
           $errors[] = 'product id is blank';}
        if($price == ''){
           $errors[] = 'price is blank';
     }

     if(count($errors) == 0){
   if(file_exists('pro.xml')){ //new

     //open an already exsisted xml file
     $xml = new DOMDocument();
     $xml->load('pro.xml');
     //getting the root.
     $root = $xml->getElementsByTagName('root')->item(0);
     //adding product to the same file.
     $product = $xml-> createElement('product');
     $root->appendChild($product);
     $name = $xml-> createElement('name');
     $product->appendChild($name);
     $name_txt = $xml -> createTextNode($_POST['name']);
     $name-> appendChild($name_txt);
     $Atr = $xml-> createAttribute('id');
     $name->appendChild($Atr);
     //append it to the name tag.
     $Atr_txt=$xml->createTextNode($_POST['id']);
     $Atr->appendChild($Atr_txt);
     $price = $xml -> createElement('price');
     $product->appendChild($price);
     $price_txt = $xml-> createTextNode( $_POST['price']);
     $price->appendChild($price_txt);

     // adding this new product to the xml file now.
     $xml->formatOutput= true;
     $xml-> save('pro.xml');

         echo" product ". $_POST['name'] . " has been added sucsessfully!!";
           header('Refresh:2;url=test.php');
        die;
      }// for if.
      else{//new
        $xml = new DOMDocument('1.0');
        // create a root tag.
        $root= $xml->createElement('root');
        $xml->appendChild($root);
        $xml->formatOutput= true;
        $xml->save('pro.xml');

        //open an already exsisted xml file
        $xml = new DOMDocument();
        $xml->load('pro.xml');
        //getting the root.
        $root = $xml->getElementsByTagName('root')->item(0);
        //adding product to the same file.
        $product = $xml-> createElement('product');
        $root->appendChild($product);
        $name = $xml-> createElement('name');
        $product->appendChild($name);
        $name_txt = $xml -> createTextNode($_POST['name']);
        $name-> appendChild($name_txt);
        $Atr = $xml-> createAttribute('id');
        $name->appendChild($Atr);
        //append it to the name tag.
        $Atr_txt=$xml->createTextNode($_POST['id']);
        $Atr->appendChild($Atr_txt);
        $price = $xml -> createElement('price');
        $product->appendChild($price);
        $price_txt = $xml-> createTextNode( $_POST['price']);
        $price->appendChild($price_txt);

        // adding this new product to the xml file now.
        $xml->formatOutput= true;
        $xml-> save('pro.xml');


          echo" new products file has been added, and";//new
           echo" product ". $_POST['name'] . " has been added sucsessfully!!";
            header('Refresh:4;url=test.php');//new
              die;//new
      }//new for else.
    }//new if(count($errors)
  }//if(isset($_POST['add']))
?>

<!-- =============    end of addind to xml file    ============= -->




<!-- this will delete a specific product from the xml file if the file exsists,
and it will echo a msg if the file doesnt exsist.-->

<?php


$errors = array();
if(isset($_POST['remove'])){
    $name = preg_replace('/[^A-Za-z0-9]/', '', $_POST['name']);

    $id =  $_POST['id'];
    // $price = $_POST['price'];

     if($name == ''){
        $errors[] = 'product Name is blank';}
        if($id == ''){
           $errors[] = 'product id is blank';}
     //    if($price == ''){
     //       $errors[] = 'price is blank';
     // }

     if(count($errors) == 0){
   if(file_exists('pro.xml')){

$xml1 = simplexml_load_file('pro.xml');
function delete($id,$filename='pro.xml'){

  $data = simplexml_load_file($filename);
  for ($i=0,$length = count($data->product);$i<$length; $i++){

    if($data->product[$i]->name==$id){
  unset($data->product[$i]);
  echo" product ". $_POST['name'] . " has been deleted sucsessfully!!";
    header('Refresh:3;url=test.php');//new
    break;
  }
else{
    echo" the product that you are trying to delete doesnt exsist in this folder!!";
    header('Refresh:3;url=test.php');//new
      break;
}


}//end of for loop.
file_put_contents($filename,$data->saveXML());

}// end of function delete.
delete($_POST['name']);



    die;//new
}// for this  if(file_exists('pro.xml').
else{
    echo" the file you are trying to delete from doesnt exsist!!";
}
}// for this    if(count($errors) == 0).
}// if(isset($_POST['remove']).
 ?>

<!-- =============    end of deletion from xml file    ============= -->

<!DOCTYPE html>
<html >
<head>

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>User Sign Up</title>
</head>
<body>

  <!-- //============      adding  form   ============================== -->
    <form class="main-container" method="post" action="">
      <?php
      if(count($errors) > 0){
          echo '<ul>';
          foreach($errors as $e){
              echo '<li>' . $e . '</li>';
          }
          echo '</ul>';
      }
      ?>
        <div style="display: flex; flex-flow: column;">

            <div class="input-container">
                <h2>add a product</h2>
            </div>
            <div class="input-container">
                <input type="text" name="name" placeholder="product Name" size="10">
            </div>

            <div class="input-container">
                <input type="text" name="id" placeholder="product id" size="10">
            </div>

            <div class="input-container">
                <input type="number" name="price" placeholder="price" size="20">

            </div>

        <div class="input-container">
          <button type="submit" name="add" value="add" >add product</button>
        </div>

    </form>

<!-- //==============       deletion form     ======================= -->

    <form class="main-container" method="post" action="">
      <?php
      if(count($errors) > 0){
          echo '<ul>';
          foreach($errors as $e){
              echo '<li>' . $e . '</li>';
          }
          echo '</ul>';
      }
      ?>

        <div style="display: flex; flex-flow: column;">

            <div class="input-container">
                <h2>delete a product</h2>
            </div>
            <div class="input-container">
                <input type="text" name="name" placeholder="product Name" size="10">
            </div>

            <div class="input-container">
                <input type="text" name="id" placeholder="product id" size="10">
            </div>
<!--
            <div class="input-container">
                <input type="number" name="price" placeholder="price" size="20">

            </div> -->

        <div class="input-container">
          <button type="submit" name="remove" value="remove" >remove product</button>
        </div>

    </form>
<style>



html body {
    margin: 0;
    padding: 0;
    display: flex;
    flex-flow: column;
}

.main-container {
    max-width: 900px;
    width: 900px;
    display: flex;
    margin: auto;
    flex-flow: column;
}

.column-container {
    display: flex;
    flex-flow: row;
    justify-content: space-around;
}

.input-container {

    display: flex;
    justify-content: center;
       border-radius: 25px 25px  25px  25px;
}


input {

    width: 175px;
    height: 30px;
    margin-top: 1rem;
    border-radius: 15px 15px  15px  15px;
}

button {

    width: 150px;
    height: 30px;
    margin-top: 1rem;
    background-color: rgb(32, 92, 202);
    color: whitesmoke;
    border-radius: 20px 20px  20px  20px;
}



</style>

</body>

</html>

标签: phpxmlmacosxampp

解决方案


推荐阅读