简体   繁体   中英

PHP code not working getting server error 500

UPDATE: Thank you all for the late night help. I Have updated the code below with the changes that I have made. If a line is commented it is something I tried and it didn't work, what isn't commented out right now was my last attempt. I am still not able to get my page to load. Again Thank You!.

Website location: www.encapdio.com/pyscraper.php I am a systems guy taking a shot at programming and decided to put a website together to learn php. My code if causing the page to error 500 and even with my php.ini file showing errors it seems its just breaks. I can't find any errors in my code, but I most likely need a fresh pair of eyes. This is what it should do.

The purpose of this page is to grab line per line stock ticker symbols. Once you hit submit my code should check the textarea if its empty. If it isn't empty then take the $content in to an array, add $linebreak to the end of each array item and account for \\r type line breaks, Next write each entry line by line in to the file. If it is empty to echo out that no tickers were entered.

Then there if all was successful and the status = results you get to see the ticker symbols next to their stock price. I have a fully tested python script checking the ticker_inputs.txt file that will execute my webcrawler/scraper/ticker_inputs.txt updater.

It is the php that I cannot get to update the ticker_input.txt file or even show the php and html on the same page without that server error 500.

Example Code:

<?php
include("inc/header.php");
include("inc/nav.php");

#if (isset ($_POST[$text_name])) && $_POST['text_name'] != "") {
if (!empty($_POST['textfile'])) {
    $content = isset($_POST['textfile'])?$_POST['textfile']:"";
    $linebreak = explode("\n", str_replace("\r", "", $content));
  $tick_wfile = fopen("ticker_input.txt", "w") or die("Unable to open file!");
    fwrite($tick_wfile,$linebreak);
    fclose($tick_wfile);
   if (empty($_POST['textfile'])) {
   echo "No Tickers Were Entered!";
  }
header("Location: pyscraper.php?status=results");
exit;
}
include("inc/header.php");
include("inc/nav.php"); ?>

<div class="container" align="center">
    <div class="page-header">
          <br><h1>Web Scraper<small> built with python</small></h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "results") {
         $tick_rfile = fopen("ticker_input.txt","r") or die("Unable to open file after writting to it!");
        echo fread($tick_rfile,filesize("ticker_input.txt"));
        fclose($tick_rfile);
} else { ?>
    <form name="savefile" method="post" action="">
        <fieldset class="form-group">
        <label for="tickerlist">Enter Tickers <small>(one per line)</small></label>
        <textarea name="textfile" class="form-control" rows="5"></textarea>
        </fieldset>
        <button type="submit" name="submit_file" value="Save File" class="btn btn-primary">Submit</button>
    </form>
    <?php } ?>
    </div>
</div>
<?php include("inc/footer.php"); ?>

Try changing:

header("location:pyscraper.php?status=results");

To

header("Location: pyscraper.php?status=results");
exit;

Basically, without the exit; the rest of the PHP is still executed. Also HTTP headers are case sensitive .

If this does not work, I'll remove the answer. Posting as a comment would have been harder to follow.

One closing parenthesis is missing in this line

$linebreak = explode("\\n", str_replace("\\r", "", $content);

Check your code

An open paranthesis is missing on fourth line after isset.

Also you can't check using isset on a function call.Try using empty() instead. $_POST['textfile'] is always set but can be empty

check this link for more

You are missing the isset brackets here if (isset $_POST[$text_name]) . Try this instead to check if its empty and if it's set if(isset($_POST[$text_name]) && $_POST['text_name'] != "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM