Jump to content


Photo

sending form results to a file to use the file include latter how pls?

php

Best Answer Barand, 15 September 2013 - 07:35 AM

An auto_increment key isn't mandatory so long as you have another column that contains unique values and is a candidate for a primary key. For example, in an employee database you might use the company employee number or national insurance number. Names are not good candidates for a primary key; two people can legitimately have the same name plus it is easy to mistype a name giving two people when there should only be one.

Go to the full post


  • Please log in to reply
14 replies to this topic

#1 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 12 September 2013 - 01:11 PM

hi I have been trying to get a way of building a site for a online gaming community where they can post player points to a form so that I can make them be displayed in a list on a different page after editing

 

simply the group director gets the edited points from excel and pastes them into my html form then using file include in my page the results display in a page for viewing

this is what I got so far but now I am getting stupid error messages

 

<?php $player_name = $_POST['player_name'];$points = $_POST['points'];
$fp = fopen(”top100stats.txt”,);
fclose($fp);echo “<h1>You data has been saved in a text file!</h1>”;
?>

 

this is the error I get

Parse error: syntax error, unexpected ')' in /home/u159756103/public_html/top100stats.php on line 2

 

any help would be great as I was trying to do this using MySQL however after making my table I could not get php to write thew form data into it hahaha as you can see I am very new to php and mysql



#2 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 12 September 2013 - 02:47 PM

You would be best to pick up mysql and try to work that out as it's just better for your purposes in a myriad of ways.

However at a glance I would suggest the syntax error is the comma u have in Fopen line. Fopen usually requires two params so after that comma u should pass in parameter e.g. 'w' which opens the file ready for writing to it.
Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#3 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 12 September 2013 - 02:48 PM

Also passing in completely raw post data directly into a file makes me feel quite uneasy from a security perspective.
Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#4 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 12 September 2013 - 05:28 PM

haha yes just simply adding the 'w' after the comma fixed line 2 lol but moved to an new error on line 3

Parse error: syntax error, unexpected '>' in /home/u159756103/public_html/top100stats.php on line 3

 

i agree about your uneasyness with the raw data from a post but i figured that the site i am building is a free site for a low risk group of volenteers also i know nothing of mysql databases although i have managed to create a table using the system's already on my server which are phpmyadmin and mysql databases just when i try to connect to my table to add the records from my web form i get all kinds of issues connecting



#5 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 13 September 2013 - 03:24 AM

Can u post your code again? Based on the original I can't see why that error would be there.

also your script currently won't actually write anything to the file as all it does now is open it for writing and then closes it again .

Have you looked at php.net? U can pretty much copy their example herehttp://www.php.net/m...tion.fwrite.php

Also take a look at the parameters for fopen because some of the modes will overwrite the file each time and some will append to it http://www.php.net/m...ction.fopen.php
Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#6 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 13 September 2013 - 06:56 AM

I took your advice as above and scraped that stupid code because everytime I fixed a syntax a new one poped up so I re did it from scratch here is my new code:

 

<?php
$fp = fopen('top100stats.txt', 'w);
fwrite($fp, '$player_name');
fwrite($fp, '$points');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!
?>

 

haha now then spot my error as when the form is submitted it passes to this code and this code passes to the text file which is what I want however instead of passing the form variables $player_name $points that info is lost and I get the coding $player_name $points in my text file in the mean time im sitting a crash course in MySQL lolol



#7 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,782 posts

Posted 13 September 2013 - 07:41 AM

Variables don't work in single-quoted string. The quotes can be removed altogether.

<?php
fwrite($fp, $player_name);
?>

Edited by cyberRobot, 13 September 2013 - 07:41 AM.

Blogging about PHP and other web-related topics at ScorpBytes.com


#8 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 13 September 2013 - 11:23 AM

Yeah as cyber said drop the quotes around your variable names when passing then to the fwrite. And also on the fopen line u r missing your closing quote around the 'w'.
Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#9 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 13 September 2013 - 02:29 PM

Also, this would be much easier:

file_put_contents('top100stats.txt', $player_name);

You can use FILE_APPEND as the third param if you want to append.

 


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#10 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 14 September 2013 - 06:17 PM

ok I am back again ha ha so I took on board the advice you guys were all pointing at which was using MySQL database not fopen and sat reading a tutorial until a stupid hour in the morning after I finished work and I made a DB and created a table in there named " top100stats " here is the php I used;

 

<?php
 $con=mysqli_connect("mysql.neq3.com","u1*****103_mike","K*****id11","u1*****103_mike");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $sql="INSERT INTO top100stats (player_name, points)
 VALUES
 ('$_POST[player_name]','$_POST[points]')";

 if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error($con));
   }
 echo "1 record added";

 mysqli_close($con);
 ?>

 

ok now I am pretty impressed with myself at this point as you will see in a sec I actually managed a conection to MySQL,

and now for the BUT in this the part im not understanding is this error message I am reciving when I input to my form and submit it sends the data to a file named " dbconect.php " which then is supposed to add the record into my table ( code for this page above ) the error is;

 

Error: Table 'u1*****103_mike.top100stats' doesn't exist

 

now of course for my security I have used the astrix to hide parts of my user ID in all codes and error messages

I understand the error is telling me that it's looking for a table named with my user name and my table name but why is it doing this to me

oh did I say I don't really know my about MySQL I just read a tutorial over night and then produced this coding lol I love learning and I hope you guys love teaching here thanks in advance :happy-04:



#11 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,604 posts
  • LocationCheshire, UK

Posted 14 September 2013 - 06:58 PM

Check your spellings for the database and the table names, In my experience if an error message says something doesn't exist then it doesn't.


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#12 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 15 September 2013 - 04:21 AM

HAHA that's just too funny Guru only because that's exactly what it was but I worked it out for myself right after I posted on here LOL

just after I posted on here I went back to my domain looked at the same coding I had posted on here and logged into MySQL and started just looking at everything,

once I got into my tables and stuff I checked my structure and a few other options then realised that I was trying to insert into a tabled named " top100stats " the correct path I was needing to use was " top100standings ".............

I think that the reason I am struggling with  MySQL so much is that all tutorials refer to manually typing all the commands however on my domain with neq3.com this is not how its done I have a console everything is done using a wizard system for instance I had to change my table as I had 3 column's id, player_names & points

the id column was supposed to be an auto integer so that the user never had to add data to this column however when I tested this for some reason it was adding zero to the first record then was trying to add zero to the second which is not allowed so was throwing me a new error so I read about this and everyone was saying use AUTO_INCREMEANT but in my console I could not find any option to do this so I decided to do away with that column so all in all THANK YOU all for your help with setting this up even when your help has made me go away and research and learn my own help rather than giving me the answers



#13 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,604 posts
  • LocationCheshire, UK

Posted 15 September 2013 - 07:35 AM   Best Answer

An auto_increment key isn't mandatory so long as you have another column that contains unique values and is a candidate for a primary key. For example, in an employee database you might use the company employee number or national insurance number. Names are not good candidates for a primary key; two people can legitimately have the same name plus it is easy to mistype a name giving two people when there should only be one.


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#14 Baxt01

Baxt01

    Member

  • Members
  • PipPip
  • 13 posts

Posted 15 September 2013 - 07:54 AM

lol yeh that would be so much easyer for me if I was manually assigning the number value for each record however I am not even the one adding the data to the table I am making this for a voluntary group of iliterates (no offence ) I need it to be an auto generated numbered ID for the primary key



#15 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,604 posts
  • LocationCheshire, UK

Posted 15 September 2013 - 09:57 AM

In which case you need to make it as idiot-proof as possible

 

Create a table of players. This is the only place that will contain the player name so spelling mismatches can't happen. Use this table to create a dropdown selection of player names with the ids as the option values. Thisselected  id goes into the points table.

CREATE TABLE player (
player_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
player_name VARCHAR(100)
);

CREATE TABLE points (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
player_id INT,
points INT
);

+-------------+
| player      |
+-------------+         +-------------+
| player_id   |----+    |  points     |
| player_name |    |    +-------------+
+-------------+    |    | id          |
                   +---<| player_id   |
                        | points      |
                        +-------------+

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com