www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP Submit Button

  1. #1
    Join Date
    Feb 2014
    Posts
    6

    Question PHP Submit Button

    Hi,

    I am unable to make this code to work could anyone kindly to help please

    <?php
    $username = "root";
    $password = "";
    $hostname = "localhost";

    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";

    //select a database to work with
    $selected = mysql_select_db("sgptt",$dbhandle)
    or die("Could not select examples");

    if (isset($_POST['$submit'])) {
    $SQL = "INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES (\"".$Username."\", \"".$Password."\", \"".$Firstname."\")";
    $result = mysql_query($SQL);
    }
    //close the connection
    mysql_close($dbhandle);
    ?>
    ----------------------------------Html--------------------------------------------
    <form4 action="insert.php" method="post">
    <br><input id="Username" name="Username" type="text" placeholder="Username'abcd123'"><br>
    <input id="Password" name="Password" type="password" placeholder="Password"><br>
    <br><input id="Firstname" name="Firstname" type="text" placeholder="First Name"><br>
    <br><input id="Surname" name="Surname" type="text" placeholder="Surname"><br>
    <br><input id="Email" name="Email" type="email" placeholder="Email Address"><br>
    <br><select id="AccountType" name="AccountType">
    <option value="Student" selected="selected">Student</option>
    <option value="Staff">Staff</option>
    </select>
    <input id="regB" type="submit">
    <input id="reset" type="button" value="Reset">
    </form4>

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    752
    Numerous errors with var names and your insistence on using only one type of quote (double & single work!) make this full of problems. BUT - what exactly is NOT working for you?

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    752
    OK - took the time to do some work on your sample code. DESPITE THE FACT THAT MYSQL IS DEPRACATED AND SHOULD NOT BE USED (see mysqli or PDO) here is your cleaned up code.
    PHP Code:
     $username "root";
     
    $password "";
     
    $hostname "localhost"
     
    //connection to the database
     
    $dbhandle mysql_connect($hostname$username$password
            or die(
    "Unable to connect to MySQL");
     echo 
    "Connected to MySQL<br>";
     
    //select a database to work with
     
    $selected mysql_select_db("sgptt",$dbhandle
            or die(
    "Could not select examples");
     if (isset(
    $_POST['$submit'])) 
     {
    //    $SQL = "INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES (\"".$Username."\", \"".$Password."\", \"".$Firstname."\")";
    // Try this instead of above; note how single quotes make it much easier to type and to interpret later.
        
    $SQL "INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES ('$Username', '$Password', '$Firstname')";
    //  Your use of $Username is an error - var undefined.
    //  Your use of $Password and $Firstname are also errors.  Same reason.
    //  Not sure, but your query expects 6 input values but you only supply 3.  Another error.
        
    $result mysql_query($SQL);
    //  THEN add some error checking to ensure that your query ran!
        
    if (!$result)
        {
            echo 
    "Could not insert data - error msg is:<br> ".mysql_error();
            exit();
        }
        if (
    mysql_affected_rows() == 0)
        {
            echo 
    "No row inserted by query";
            exit();
        }
     }
     
    //close the connection
     
    mysql_close($dbhandle);
     
    ?>
     ----------------------------------Html--------------------------------------------
     <!--  WHAT IS A 'FORM4' TAG????? -->
     <form4 action="insert.php" method="post">
     <br>
     <!--  YOU DON'T NEED THE ID= ATTRIBUTE ON EVERY SINGLE INPUT TAG -->
     <input name="Username" type="text" placeholder="Username'abcd123'">
     <br>
     <input name="Password" type="password" placeholder="Password">
     <br>
     <br>
     <input name="Firstname" type="text" placeholder="First Name">
     <br>
     <br>
     <input name="Surname" type="text" placeholder="Surname">
     <br>
     <br>
     <input name="Email" type="email" placeholder="Email Address">
     <br>
     <br>
     <select name="AccountType">
     <option value="Student" selected="selected">Student</option>
     <option value="Staff">Staff</option>
     </select>
     <input type="submit" value='Submit'>
     <input type="button" value="Reset">
     </form4> 
    Your html is setup to provide you with several values but your php is not grabbing them. Perhaps that is why your don't have enough values in your query statement.

    I think that your 'reset' button will not do what you want it to do. I've never used it, but I think you want to say "type='reset'", not type='button'.

    This makes your code easier to read and debug I think. You still have some work to go to complete it.

    Most importantly though - when you finally figure out how to grab those inputs from your html with your php, you need to study up on protecting yourself from bad input. You need to edit the incoming values to ensure they are valid and are not containing things that will be harmful to your script/appl/server/database. I'm not the one to teach you that, but you do need to learn it!!!!

    Good luck with your learning process.

  4. #4
    Join Date
    Feb 2014
    Posts
    6
    Thank you for the code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles