0

So, from what I have been learning for these past few weeks I believe I have sufficient knowledge on how to perform PHP, and SQL related queries to create a good and dynamic website that could support something like a forum. I've not been able to do that yet, and am having quite a bit of trouble with it as well. So far, I've made a PHP file, that was simply to see if I could use PHP well. It did not work out, and I've been getting plenty of errors, and I've been unable to fix them, whatsoever. And so, I'd like to come here to ask, if anyone out there could possibly analyze my code that I've written, and see what is wrong with it, if possible. Along with that, I'd like to know what would be the "Proper" way of A. Connecting to SQL B. Selecting Data C. Displaying/Utilizing Data And thank you, for reading and/or possibly replying to this. Here, is the code I've written but have been unable to work.

<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form>
Input First name:<br>
<input type="text" name="FN">
<br>
Input Last name:<br>
<input type="text" name="LN">
<br>
Input Email:<br>
<input type="text" name="Email">
<br>
<input type="submit" method="post">
<?php
if (isset($_POST['FN'], $_POST['LN'], $_POST['Email']))
$sql = 'INSERT INTO `info` ("USERID", "FN", "LN", "Email") VALUES (\'$_POST[FN]\', '$_POST["LN"]', '$_POST["Email"]')';

?>
</form>

<?php
$sql = "SELECT FN, LN, Email
        FROM
            info"
            $result = "mysql_query($sql)"
            while($row_list = mysql_fetch_assoc( $result )) {
ECHO <div>The Names are:</div><br>
ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
Turtles
  • 39
  • 3
  • When you say it doesnt work, are you getting errors? Also it helps to include only the most relevant code to the problem you are experiencing and try and be more direct with your question. Oh, welcome to SO. – Lima Oct 24 '15 at 00:47
  • http://www.w3schools.com/php/php_mysql_select.asp or http://www.w3schools.com/php/php_mysql_insert.asp for good reference – 夢のの夢 Oct 24 '15 at 01:08
  • To be exact, I am Experiencing this error: Parse error: syntax error, unexpected '$_POST' (T_VARIABLE) in C:\xampp\htdocs\index.php on line 19 Along with that, I posted the full code as I believe there to be more errors. And thanks for the link, I've attempted research of my own using w3schools and alike to no avail, either misunderstanding it, or having some other error. – Turtles Oct 24 '15 at 01:26

3 Answers3

0

Your PHP code is wrong in so many ways even in your query. What I did is clean your codes.

<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form action="" method="POST">
    Input First name:<br>
    <input type="text" name="FN">
    <br>
    Input Last name:<br>
    <input type="text" name="LN">
    <br>
    Input Email:<br>
    <input type="text" name="Email">
    <br>
    <input type="submit" name="submit-btn" value="submit">

</form>

<?php

if (isset($_POST['submit-btn'])){
    $sql = 'INSERT INTO info ( "FN", "LN", "Email") VALUES ('$_POST[FN]', '$_POST["LN"]', '$_POST["Email"]')';

    if (mysql_query($sql)) {
        echo "New record created successfully";
    } 
}



$sql = "SELECT FN, LN, Email FROM info";
 $result = mysql_query($sql)


while($row_list = mysql_fetch_assoc( $result )) {
    ECHO '<div>The Names are:</div><br>';
    ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
user3814670
  • 61
  • 1
  • 3
  • 10
  • Thank you for the help, however I would like to say that with using the code that you've cleaned up above, I receive this error: Parse error: syntax error, unexpected '$_POST' (T_VARIABLE) in C:\xampp\htdocs\index.php on line 23 Also, thank you all for helping me with this, as I'm sure you can see, I'm terrible at this insofar. – Turtles Oct 24 '15 at 05:21
  • try to change $sql = 'INSERT INTO info ( "FN", "LN", "Email") VALUES ('$_POST[FN]', '$_POST["LN"]', '$_POST["Email"]''; to $sql = "INSERT INTO info ( 'FN', 'LN', 'Email') VALUES ('".$_POST['FN']."', '".$_POST['LN']."', '".$_POST['Email']."'"; – user3814670 Oct 24 '15 at 05:28
  • It appears that worked, now I am receiving: Parse error: syntax error, unexpected 'while' (T_WHILE) in C:\xampp\htdocs\index.php on line 36 Also, quick question, if you don't mind, why the . in front of each $_POST ? Just part of the syntax? I never quite saw that. EDIT: Just got it, forgot a ; on line 33. – Turtles Oct 24 '15 at 05:34
  • Apparently, the way that I have learned about going about it, does not work, so could you tell me how exactly I would fetch Data from the SQL server and ECHO it onto the page as a variable, assuming I need all of the rows? – Turtles Oct 24 '15 at 05:42
  • It's the concatenation operator, concatenating both strings together (making one string out of two separate strings). Read this http://stackoverflow.com/questions/6104449/what-does-the-period-character-mean-if-used-in-the-middle-of-a-php-string – user3814670 Oct 24 '15 at 05:42
0
  1. try to indent your code to make it more readable for yourself.
  2. as already answered by user3814670, your insert query was wrong, with 4 elements (id,fn,ln,email) and only 3 data (fn,ln,email)
  3. your query was't being executed also cleaned by user3814670 by adding the lines

    if (mysql_query($sql)) { echo "New record created successfully"; }

  4. try to print your query to the screen and executing it in you database to see if your query fails or print the error to screen

    mysql_error()

  5. add this on top of your file after

    ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

davejal
  • 6,009
  • 10
  • 39
  • 82
0

Here's how you display data from the database using while loop

while($row=mysql_fetch_array($result)) {   
 echo $row['FN'] . " " . $row['LN'] . " " . $row['Email'];
}
user3814670
  • 61
  • 1
  • 3
  • 10