Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

parser error-unexpected end of file on line 28

General discussions related to php

Moderators: macek, egami, gesf

parser error-unexpected end of file on line 28

Postby carlo2837 » Tue Apr 30, 2013 8:10 am

Hi, could someone please check this code and tell me how to fix it, I cannot figure out where the error is, I am just a beginner with php. Here is the code that I got from a tutorial:

<HTML>
<?php
if($submit)
{
$db = mysql_connect("localhost", "root","mypassword");
mysql_select_db("learndb",$db);
$sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary)
VALUES ('$first','$last','$nickname','$email','$salary')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
}
else
{
?>
<form method="post" action="input.php">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Nick Name:<input type="Text" name="nickname"><br>
E-mail:<input type="Text" name="email"><br>
Salary:<input type="Text" name="salary"><br>
<input type="Submit" name="submit" value="Enter information"></form>
<?
}
?>
</HTML>
carlo2837
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Jan 25, 2013 2:40 am

Re: parser error-unexpected end of file on line 28

Postby Strider64 » Wed May 01, 2013 9:43 am

You really should be using prepared statements by either using mysqli or PDO, further help study that at php.net manual (and for other help on php) :)


Not Promising this code works, for I didn't test it out and if it doesn't it shouldn't be to much to fix it. I usually don't write in procedural style, so that might have trip me up. However I think this should help you out ... well i think. :D

Top of File
Code: Select all
<?php
if (isset($_POST['submit'] && ($_POST['submit'] == "Enter information")  ) {
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_database);

/* check connection */
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = mysqli_prepare($link, "INSERT INTO personnel VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssss', $firstname, $lastname, $nick, $email, $salary);

$firstname  = htmlspecialchars($_POST['firstname']);
$lastname  = htmlspecialchars($_POST['lastname']);
$nick         = htmlspecialchars($_POST['nick']);
$email       = htmlspecialchars($_POST['email']);
$salary      = htmlspecialchars($_POST['salary']);

/* execute prepared statement */
mysqli_stmt_execute($stmt);
$message = "Thank you! Information entered....";
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
mysqli_stmt_close($stmt);

}


and middle/bottom of file....
Code: Select all
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<h1><?php echo (isset($message)) ? $message : "Enter Your Information Below"; ?></h1>
<form method="post" action="">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Nick Name:<input type="Text" name="nickname"><br>
E-mail:<input type="Text" name="email"><br>
Salary:<input type="Text" name="salary"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
</body>
</html>
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 44
Joined: Sat Mar 23, 2013 8:24 am


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron