thehubclick Posted November 21, 2019 Share Posted November 21, 2019 (edited) 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 November 21, 2019 by thehubclick left out info Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2019 Share Posted November 21, 2019 In what way is it "not working"? What do you want to happen so you can say it is working? When posting, bear in mind we are not psychic and cannot see your screen and the data values you are seeing. Quote Link to comment Share on other sites More sharing options...
chhorn Posted November 21, 2019 Share Posted November 21, 2019 (edited) 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 November 21, 2019 by chhorn Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2019 Share Posted November 21, 2019 23 minutes ago, thehubclick said: // i added this line $string1="| " BTW, that line you added is missing a ";" from the end; Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 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); } Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 (edited) 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 November 21, 2019 by thehubclick Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 11 minutes ago, Barand said: BTW, that line you added is missing a ";" from the end; Thanks php is picky ";" Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2019 Share Posted November 21, 2019 10 minutes ago, Barand said: BTW, that line you added is missing a ";" from the end Turn your error reporting ON 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 21, 2019 Share Posted November 21, 2019 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. Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 Thank you Barand What you said adding ";" fixed it i left that out Thanks for your help My code works with your all helping Good day. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2019 Share Posted November 21, 2019 1 minute ago, ginerjm said: I stopped reading when I saw this: Pity. If you had read on you'd have seen he is writing to a text file. 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2019 Share Posted November 21, 2019 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 ) 1 Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 (edited) 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 November 21, 2019 by thehubclick Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 21, 2019 Share Posted November 21, 2019 You can turn on error reporting in your script by putting this at the top of the page error_reporting(E_ALL); https://www.php.net/manual/en/function.error-reporting.php Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 21, 2019 Share Posted November 21, 2019 4 hours ago, thehubclick said: This xampp is the best You would find Laragon to be even better. (Coming from a former Xampp user) https://laragon.org/ Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 4 hours ago, Psycho said: You can turn on error reporting in your script by putting this at the top of the page error_reporting(E_ALL); https://www.php.net/manual/en/function.error-reporting.php Ok i try that ty Quote Link to comment Share on other sites More sharing options...
thehubclick Posted November 21, 2019 Author Share Posted November 21, 2019 1 hour ago, benanamen said: You would find Laragon to be even better. (Coming from a former Xampp user) https://laragon.org/ Ill check it out Quote Link to comment Share on other sites More sharing options...
ajoo Posted November 23, 2019 Share Posted November 23, 2019 Hi Benanamen, Quote You would find Laragon to be even better. (Coming from a former Xampp user) Can it be installed along with xampp on the same machine or would it conflict. Thanks. Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 23, 2019 Share Posted November 23, 2019 Yes but you don't want to run both at the same time. If you really wanted to, you would need to change the Apache port on one of them as they both use port 80 1 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.