-1

Thank you for reply in advance.

Parse error: syntax error, unexpected T_ELSE in insert.php on line 45

Please check my insert.php code for error .. tried to connect but didn't success .. it went to insert.php in the browser but its a blank page.

Here is the insert.php:

<?php
$email = $_POST['email'];

if (!empty($email)){
    $host = "localhost";
    $dbUsername = "root";
    $dbPassword = "password";
    $dbname = "newsletters_soon_db";
} else {
    echo "Email Address is required";
    die();
}   
// Create Connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);

if (mysqli_connect_error()) {
    die('Connection Error('.mysqli_connect_error().')'.mysqli_connect_error());
}else{
    $SELECT = "SELECT email from email_newsletters Where email = ? Limit 1";
    $INSERT = "INSERT Into email_newsletters (email) values(?)";
    //Prepare statement
    $stmt = $conn->prepare($SELECT);
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->bind_result($email);
    $stmt->store_result();
    $rnum = $stmt->num_row;

    if ($rnum==0){
        $stmt->close();

        $stmt = $conn->prepare($INSERT);
        $stmt->bind_param("s",$email);
        $stmt->execute();
        echo "New record inserted successfully";
    } else {
        echo "You are already registered using this email, please type an other email address to receive updates";
    }
    $stmt->close();
    $conn->close();
}else {
    echo "Please insert insert";
    die();
}
?>

and the index.html is:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>local host</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->  
    <link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="css/util.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->
</head>
<body>
    <!--  -->
    <div class="simpleslide100">
        <div class="simpleslide100-item bg-img1" style="background-image: url('images/bg01.jpg');"></div>
        <div class="simpleslide100-item bg-img1" style="background-image: url('images/bg02.jpg');"></div>
        <div class="simpleslide100-item bg-img1" style="background-image: url('images/bg03.jpg');"></div>
    </div>
    <div class="size1 overlay1">
        <!--  -->
        <div class="size1 flex-col-c-m p-l-15 p-r-15 p-t-50 p-b-50">
            <h3 class="l1-txt1 txt-center p-b-25">
                Coming Soon
            </h3>
            <p class="m2-txt1 txt-center p-b-48">
                Our website is under construction, follow us for update now!
            </p>
            <div class="flex-w flex-c-m cd100 p-b-33">
                <div class="flex-col-c-m size2 bor1 m-l-15 m-r-15 m-b-20">
                    <span class="l2-txt1 p-b-9 days">35</span>
                    <span class="s2-txt1">Days</span>
                </div>
                <div class="flex-col-c-m size2 bor1 m-l-15 m-r-15 m-b-20">
                    <span class="l2-txt1 p-b-9 hours">17</span>
                    <span class="s2-txt1">Hours</span>
                </div>
                <div class="flex-col-c-m size2 bor1 m-l-15 m-r-15 m-b-20">
                    <span class="l2-txt1 p-b-9 minutes">50</span>
                    <span class="s2-txt1">Minutes</span>
                </div>
                <div class="flex-col-c-m size2 bor1 m-l-15 m-r-15 m-b-20">
                    <span class="l2-txt1 p-b-9 seconds">39</span>
                    <span class="s2-txt1">Seconds</span>
                </div>
            </div>
            <form action="insert.php" method="POST" class="w-full flex-w flex-c-m validate-form">
                <div class="wrap-input100 validate-input where1" data-validate = "Valid email is required: ex@abc.xyz">
                    <input class="input100 placeholder0 s2-txt2" type="text" name="email" placeholder="Enter Email Address">
                    <span class="focus-input100"></span>
                </div>
                <button type="submit" value="Submit" class="flex-c-m size3 s2-txt3 how-btn1 trans-04 where1">
                    Subscribe
                </button>
            </form>
        </div>
    </div>
<!--===============================================================================================-->  
    <script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/bootstrap/js/popper.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/countdowntime/moment.min.js"></script>
    <script src="vendor/countdowntime/moment-timezone.min.js"></script>
    <script src="vendor/countdowntime/moment-timezone-with-data.min.js"></script>
    <script src="vendor/countdowntime/countdowntime.js"></script>
    <script>
        $('.cd100').countdown100({
            /*Set Endtime here*/
            /*Endtime must be > current time*/
            endtimeYear: 2018,
            endtimeMonth: 06,
            endtimeDate: 30,
            endtimeHours: 23,
            endtimeMinutes: 59,
            endtimeSeconds: 59,
            timeZone: "Asia/Bahrain" 
            // ex:  timeZone: "America/New_York"
            //go to " http://momentjs.com/timezone/ " to get timezone
        });
    </script>
<!--===============================================================================================-->
    <script src="vendor/tilt/tilt.jquery.min.js"></script>
    <script >
        $('.js-tilt').tilt({
            scale: 1.1
        })
    </script>
<!--===============================================================================================-->
    <script src="js/main.js"></script>
</body>
</html>

I made a database called it newsletters_soon_db and made a table inside it called it email_newsletters and one column called it email with Varchar and length 40.

Thank you for help in advance

qlf
  • 9
  • 5
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Jun 06 '18 at 22:51
  • change `$stmt->num_row;` to `$stmt->num_rows;` – RiggsFolly Jun 06 '18 at 22:53
  • Please get into the habit of looking at your PHP Error log – RiggsFolly Jun 06 '18 at 22:53
  • I tried to use your method "RiggsFolly" to check error but it didn't print anything the page is blank don't know why. and I search the website here I came up with this method: in terminal .. type this: php -l phpfilename.php and error will come instantly. – qlf Jun 07 '18 at 10:07
  • Did you look in the PHP Error log for an clues? – RiggsFolly Jun 07 '18 at 10:08
  • "RiggsFolly" I would like to make a log file for all error in my php please .. – qlf Jun 07 '18 at 10:08
  • Please add the error message to your question – RiggsFolly Jun 07 '18 at 10:09
  • Yes .. I used php -l filename.php and solved the issue .. its syntax error, unexpected T_ELSE in insert.php on line 45 .. I made the correction and its work :) – qlf Jun 07 '18 at 10:53

1 Answers1

0

Your if (mysqli_connect_error()) { else statement is if ... else ... else

It needs to be if ... else if ... else or just delete the last else statement.

This is the error you should see in your error log

Parse error: syntax error, unexpected T_ELSE in insert.php on line 45

  • Thank you for reply .. yes I corrected and removed the last else and it works .. and I am wondering how to correct the code with if .. elseif .. else in my cases .. just to get some knowledge from you :) Thank you – qlf Jun 07 '18 at 10:51
  • I'm not clear as to what the last else statement is supposed to be triggered by. Here's a simple example if ($rnum==0) { //do something } else if ($rnum==1) { //do something else } else { echo "too many records returned"; } – Karen Vicknair Jun 08 '18 at 12:23
  • I think its not possible to add the last else in the code .. I add the last statement for verifying the text field not to be empty .. but I did deleted it and its work. – qlf Jun 11 '18 at 00:31