Jump to content

unexpected end of file


Go to solution Solved by benanamen,

Recommended Posts

I have this code that pulls data from a form and  is supposed to check if the neptune and username are inserted 2 times it replies back with an error if not it posts the data to 2 lines with neptune username and email on one line and just email and username on the other

 

The issue is the code i have gives me an error  saying unexpected end of file below is my code

any help will be appreciated

 <?php
    if(isset($_POST['choices']) && !empty($_POST['choices'])){
    if($_POST['choices'] == 'four'){
    //variables from form entered
    $username = $_POST['username'];
    $neptune = $_POST['neptune'];
    $email = $_POST['useremail'];
    //connect to the database
    $dbc = mysqli_connect('localhost', 'root', '', 'happygam_main') or die('Error connecting to MySQL server');
    $check=mysqli_query($dbc,"select * from ballot where username='$username' and neptune='$neptune'");
    $checkrows=mysqli_num_rows($check);

   if($checkrows>0) {
      echo "This combination of neptune and username has already been processed";
   } else {  
    //insert results from the form input in 2 rows one with neptune one without
      $query = "INSERT IGNORE INTO ballot(username, useremail, neptune) VALUES('$username', '$email', '$neptune')";
$query1 = "INSERT IGNORE INTO ballot(username, neptune) VALUES('$username', '$neptune')";
      $result = mysqli_query($dbc, $query, $query1) or die('Error querying database.');

      mysqli_close($dbc);
    }
    }
	
  ?>
Link to comment
https://forums.phpfreaks.com/topic/302940-unexpected-end-of-file/
Share on other sites

  • Solution

You are missing the closing }

 

There are other problems. You need to use prepared statements. You never insert user supplied data directly to the DB. Dont SELECT *. Specify the columns you want. You also do not need to manually close the connection. It closes automatically. It would appear your logic is flawed.

 

You can't throw two query parameters into mysql like that. And don't create variables for no reason. I formatted your code so it is more readable but it still needs fixing aside from the missing bracket I put in.

 

I would recommend you use PDO. https://phpdelusions.net/pdo

 <?php
if (isset($_POST['choices']) && !empty($_POST['choices']))
    {
    if ($_POST['choices'] == 'four')
        {
        //variables from form entered
        $username = $_POST['username'];
        $neptune  = $_POST['neptune'];
        $email    = $_POST['useremail'];

        //connect to the database
        $dbc = mysqli_connect('localhost', 'root', '', 'happygam_main') or die('Error connecting to MySQL server');
        $check     = mysqli_query($dbc, "select * from ballot where username='$username' and neptune='$neptune'");
        $checkrows = mysqli_num_rows($check);
        
        if ($checkrows > 0)
            {
            echo "This combination of neptune and username has already been processed";
            }
        else
            {
            //insert results from the form input in 2 rows one with neptune one without
            $query  = "INSERT IGNORE INTO ballot(username, useremail, neptune) VALUES('$username', '$email', '$neptune')";
            $query1 = "INSERT IGNORE INTO ballot(username, neptune) VALUES('$username', '$neptune')";
            $result = mysqli_query($dbc, $query, $query1) or die('Error querying database.');
            
            mysqli_close($dbc);
            }
        }
    
    }
?>
Edited by benanamen
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.