简体   繁体   中英

MySQL Insert does not work in jQuery Mobile

I am trying to make a register form using an insert query that has worked in a booking form that I previously made, but it doesn't seem to be inserting and it gives me this error message in the console log

Uncaught Error: cannot call methods on page prior to initialization; attempted to call method 'bindRemove' coming from an external JS file.

Any Help is greatly appreciated.

This is from a PHP include

<?php

//This adds the connection file which has the details to connect to the database and what database to connect to
include_once('connection.php');

//Checks if the submit button is not set
if(!isset($_POST['submit']))
{
exit;
}
    //Declared Variables
    $first = $_POST['fname'];
    $surname = $_POST['sname'];
    $password= $_POST['password'];
    $email= $_POST['email'];
    $postcode= $_POST['postcode'];
    $GPID= $_POST['Practice'];


    //Database Connection, this connects to the database using the connection.php
    $conn=ConnectionFactory::connect();

    //Insert Query
    $query="INSERT INTO `User`(`UserID`, `First_name`, `Surname`, `Username`, `Password`, `email`, `Image`, `Postcode`, `StoreID`, `DeptNo`, `AccessID`, `GPID`) VALUES (NULL, :fname, :sname, NULL, :password, :email, NULL, :postcode, 1, 1, 1, :GPID)";

    $stmt=$conn->prepare($query);

    //Binding values
    //$stmt->bindValue(':post', $Post);
    $stmt->bindValue(':fname', $first);
    $stmt->bindValue(':sname', $surname);

    $stmt->bindValue(':password', $password);
    $stmt->bindValue(':email', $email);
    $stmt->bindValue(':postcode', $postcode);
    $stmt->bindValue(':GPID', $GPID);

    $affected_rows = $stmt->execute();

    //Message if insert is Successful
    if($affected_rows==1){
        print "<script type=\"text/javascript\">"; 
        print "alert('Register Successful')"; 
        print "</script>";
        exit;
        }else{
        print "<script type=\"text/javascript\">"; 
        print "alert('Unable to register at this moment')"; 
        print "</script>";
        exit;   
        }




    //Terminates the connection
            $conn=NULL;
?>

This is the form

 <section data-role="content">
    <h1>Register to be able to Book Appointments on the go</h1>
    <form id ="register" accept-charset="UTF-8" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method="POST">

        <fieldset data-role="fieldcontain">
        <label for="fname">First name:</label>
        <input type="text" name="fname" id="fname" required>
        </fieldset>    

        <fieldset data-role="fieldcontain">
        <label for="sname">Surname:</label>
        <input type="text" name="sname" id="sname" required>
        </fieldset>

        <fieldset data-role="fieldcontain">
        <label for="password">Password:</label>
        <input type="Password" name="password" id="password" required>
        </fieldset>

        <fieldset data-role="fieldcontain">
        <label for="Cpassword">Confirm Password:</label>
        <input type="Password" name="Cpassword" id="Cpassword" required>
        </fieldset>

        <fieldset data-role="fieldcontain">
        <label for="email">Email:</label>
        <input type="email" name="email" id="email" required>
        </fieldset>

        <fieldset data-role="fieldcontain">
        <label for="postcode">Postcode:</label>
        <input type="text" name="postcode" id="postcode" required>
        </fieldset>

        <fieldset data-role="fieldcontain">
        <select id="Practice" name="Practice" class="required">
            <option value="null">Select a Practice</option>
            <option value="1">Guide Bridge Medical Centre</option>
            <option value="2">The University Health Centre</option>
            <option value="3">King Street Medical Centre</option>
            <option value="4">Mossley Medical Centre</option>
        </select>
        </fieldset>

        <input type="submit" id="submit" name="submit" data-icon="check" data-iconpos="top" value="Register"/>
        <?php include 'registerInsert.php';?>
     </form>

Instead of the line

 $affected_rows = $stmt->execute();

replace it with

 mysqli_query($conn, "INSERT INTO `User`(`UserID`, `First_name`, `Surname`, `Username`, `Password`, `email`, `Image`, `Postcode`, `StoreID`, `DeptNo`, `AccessID`, `GPID`) VALUES (NULL, :fname, :sname, NULL, :password, :email, NULL, :postcode, 1, 1, 1, :GPID)");

I found the problem, it was the bind value but not quite sure what...

//Binding values

    $stmt->bindValue(':fname', $first);
    $stmt->bindValue(':sname', $surname);
    $stmt->bindValue(':password', $password);
    $stmt->bindValue(':email', $email);
    $stmt->bindValue(':postcode', $postcode);
    $stmt->bindValue(':GPID', $GPID);

To get around it I used variables in the query

//Declared Variables
    $first = $_POST['fname'];
            $surname = $_POST['sname'];
    $username= "lemon";
    $password= $_POST['password'];
    $email= $_POST['email'];
    $Image= "1";
    $postcode= $_POST['postcode'];
    $StoreID= "1";
    $DeptNo= "1";
    $AccessID= "1";
    $GPID= $_POST['GPID'];

//Insert Query
    $query ="INSERT INTO `User`(`UserID`, `First_name`, `Surname`, `Username`, `Password`, `email`, `Image`, `Postcode`, `StoreID`, `DeptNo`, `AccessID`, `GPID`) VALUES (NULL, :fname, :sname, 'NULL',:password,:email, NULL,:postcode,'1','1','1',:GPID)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM