Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a simple form and I run my script without any errors. I click my btn and I would like to see my echo errors but no errors show up when I have no value in textboxes. I have tried if(isset($_Post["submit"])) and if($_SERVER["REQUEST_METHOD"] == "post") What I think is nether of there ifs are becoming valid on my btn click.
PHP
<?php
$nameerror = $twoerror = $errormsg = "";
// PHP Procedural MYSQLi
// connect to mysql database with phpmyadmin
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

$connection = new mysqli($servername, $username, $password, $dbname);
//if(isset($_Post["submit"]))
if($_SERVER["REQUEST_METHOD"] == "post")
{
    if(empty(trim($_Post["name"]))) {
        $nameerror = "Name is required";
    }
    else {
        $namesafe = mysqli_real_escape_string($connection, $_Post["name"]);
    }
    if(empty(trim($_Post["two"]))) {
        $twoerror = "Two is required";
    }
    else {
        $twosafe = mysqli_real_escape_string($connection, $_Post["two"]);
    }

    if($namesafe != "" && $twosafe != "") {
        $sqlInsert = "INSERT INTO tester(name, two) " .
          "VALUES('". $namesafe ."','". $twosafe ."')";

          if(mysqli_query($connection, $sqlInsert)) {
              echo "Successfully entered.";
          } else {
              echo "NOT successful error: " . $sqlInsert . "<br>" . mysqli_error($connection);
          }
    } 
}
mysqli_close($connection);
?>
<?php

      if(!$connection) {
        die("Connection Failed! " . mysqli_connect_error());
      }
      echo "Connected Successfully@!";



   ?>
    <section class="text-align" id="section-content">
        <div id="alertMessages" class="container rounded"></div>
        <div id="contentdiv" class="container rounded">
            <form id="formtest" class="rounded" method="post" >
              <!-- action="" -->
                <h3>PHP Create</h3>
                <?php
                    if(isset($errormsg)) {
                    //  echo "<div><span>";
                      echo $errormsg;
                    //  echo "</span></div>";
                    }
                 ?>
                <div>
                    <div class="form-group">
                        <input type="text" class="form-control" id="txtName" name="name" />
                        <label for="txtName">Name </label>
                        <?php if(isset($nameerror)) { echo '<span class="error">' . $nameerror . '</span>'; } ?>
                        <!-- ? php//if(isset($_Post["name"])) echo htmlspecialchars($_Post["name"]); ? >" /> --->
                        <!-- <span class="error"><?php //echo $nameerror; ?></span> -->
                    </div>
                    <div>
                        <input type="text" class="form-control" id="txttwo" name="two" />
                        <label for="txttwo">Text Two </label>
                        <?php if(isset($twoerror)) { echo '<span class="error">' . $twoerror . '</span>'; } ?>
                    </div>
                </div>
                <button type="submit" class="btn btn-lg btn-primary btn-block" name="submit">Click</button>
            </form>
        </div>
    </section>


What I have tried:

I have been trying with internal php files and external php I just want to ...
This is basically my whole file without the bootstrap CDNs
I can run my page and I get no errors I tried to setup php-debug and xdebug on atom and chrome I cant count how many tutorials I have used and tried but for some reason my breakpoints will not turn green on atom. and stop to the IDE when i run the page.

I would refresh the page and in the network tab I would see my .php file and if i had somekind of error like at first it says no function mysql_connect so i realized i needed to use mysqli_connect

I use xampp with myphpadmin to connect to mysql.

I just want to do these simple check if row exists in db and create in db then ill do a login and crud and shopping cart etc.. but i been having troubles just setting up the debugger if i had that i know everything would be easier so i can see exactly whats going on
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900