Jump to content

had error php made changes


thehubclick

Recommended Posts

hello i am learning php a newbie .
i am a perl javascript vb script visual basic. some php.
i am tring to put multiple $strings
into one then save it to file i need to be abe to split up the data later.
my split symbol is "|" or | the second code below is my changes
the second code below had errors


first code original
this code works i tested this it
grabbed the clients user input.

// this is the user input / textdata
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>


second code changes i made.
.

if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');

// i added this line
$string1="| "

// i changed this line below
// my error might be here
$data=$data.$string1;

fwrite($fp, $data);
fclose($fp);
}
?>


can i get help fixing this to work
 $data=$data.$string1

thank you in advance.

Edited by thehubclick
left out info
Link to comment
Share on other sites

I would recommend not to use this low level functions. At best you take a database with ACID compatibility, if you want to get a quick slim start, use SQLite, it's mostly build-in into PHP. But at least you can use json_encode(), file_put_contents(), file_get_contents() and json_decode() for your task, so you have clean and readable interface without thinking too much.

On the other hand, file handling and format manipulation is a valuable lesson.

But as already said, you have to tell us what problem you encountered in detail.

Edited by chhorn
Link to comment
Share on other sites

The working php file when i run in my webserver localhost
 

<!DOCTYPE html>
<html>
<head>

<meta http-equiv="pragma" content="nocache">
<meta name=viewport content='width=1100'>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/> <!--320-->

  <title>Store form data in .txt file</title>
</head>
<body>
  <form method="post">
    Enter Your Text Here:<br>
    <input type="text" name="textdata"><br>
    <input type="submit" name="submit">
    
  </form>
</body>
</html>
<?php
              
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}

 

?>

 

 

 

I changed it to now its not running when i run this php

On my webserver

if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
$string1="@@@ " added this

Added this

$data=$data.$string1;


fwrite($fp, $data);
fclose($fp);
}

 

 

Link to comment
Share on other sites

1 minute ago, thehubclick said:

The working php file when i run in my webserver localhost
 

<!DOCTYPE html>
<html>
<head>

<meta http-equiv="pragma" content="nocache">
<meta name=viewport content='width=1100'>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/> <!--320-->

  <title>Store form data in .txt file</title>
</head>
<body>
  <form method="post">
    Enter Your Text Here:<br>
    <input type="text" name="textdata"><br>
    <input type="submit" name="submit">
    
  </form>
</body>
</html>
<?php
              
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}

 

?>

 

 

 

I changed it now its not running when i run this php

On my webserver

if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
$string1="@@@ " added this

Added this

$data=$data.$string1;


fwrite($fp, $data);
fclose($fp);
}

 

 

Gives page wont display

Edited by thehubclick
Link to comment
Share on other sites

I stopped reading when I saw this:

Quote

 I am tring to put multiple $strings
into one then save it to file i need to be abe to split up the data later.

This suggests a very bad database design.  Joining multiple values and placing them all into one record is not the way to store data.  This should really be done by making multiple related records in a separate table, linked to the main record for the user/item/primary key so that you can then easily do queries for these data values.  Your current design/approach prevents this from being easily done.

Link to comment
Share on other sites

if I were you I'd use a newline (\n) to separate the data instead of "@@@".

if(isset($_POST['textdata']))
{
    file_put_contents('data.txt', $_POST['textdata']."\n", FILE_APPEND);
}

Then, if you data.txt file contains, say

TESTA
TESTB
TESTC
TESTD

... you can read it back and separate the items into an array using file();

$data = file('data.txt');

giving

$data = Array
(
    [0] => TESTA

    [1] => TESTB

    [2] => TESTC

    [3] => TESTD
)

 

  • Like 1
Link to comment
Share on other sites

23 minutes ago, Barand said:

Turn your error reporting ON

Ok im using the Xampp. Program to startup webserver ill see how to turn on error reporting on. I just started using it to test my website before i put it on my paid web hosting .www example:mydomain.com/home1.php thanks

 

This xampp is the best

It installs everything php perl so it works correctly

 

Sometimes i forget how to edit php.ini file

You know configuration

With xampp it makes it all work all you do is start up the webserver

5 stars xampp.

Edited by thehubclick
Link to comment
Share on other sites

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.