Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Mysqli_query - INSERT INTO issue

    Hi There,

    I have written a very basic Registration form that is validated prior to the user information being entered into the database. Everything works i.e. Connection, submit to process script, what isn't working is the INSERT INTO.

    I have been reading up on the mysqli_query() function and trawling the various PHP related sites for a solution.

    I have to also call the customer id on successful registration, but one step at a time.

    Thanks for taking a look and for any pointers.

    The registration form

    Code:
    <?php
    include 'header.html';
    ?>
    
    <!DOCTYPE html>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Bazaar Ceramics - Registration</title>
    </head>
    <body>
        <hr>
        <h1 align = "center"> Customer Registration Form</h1>
        <h3 align = "center"> 
            By adding yourself to our customer list it will make it easier for us to invoice you. 
            We will add you to our mailing list so we can keep you informed of any special offers.
        </h3>
        </br>    <hr>    </br>    
        <form action = "processCustomerDetails.php" method = "POST">
            <h2>Please complete all fields below and press submit when ready.</h2>
            <table>
                <tr>
                    <td>Given Name: </td>
                    <td><input type = "text" name = "gName" size = "30" value = ""></br></td>
                </tr>
                <tr>
                    <td>Last Name: </td>
                    <td><input type = "text" name = "lName" size = "30" value = ""></br></td>
                </tr>
                <tr>
                    <td>Email: </td>
                    <td><input type = "text" name = "email" size = "50" value = ""></td>
                </tr>
                <tr><td>Address: </td>
                    <td><input type = "text" name = "address" size = "50" value = ""></td>
                </tr>
                <tr>
                    <td><input id = "button" type = "submit" name = "submit" value = "Submit"></td>
                    <td><input id = "button" type = "reset" name = "reset" value ="Reset"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    The process script
    PHP Code:
    <?php
    include 'header.html';


    if(isset(
    $_POST['submit'])){
        require 
    'connect_db.php';
        
            
    $gName $_POST['gName'];
            
    $lName $_POST['lName'];
            
    $email $_POST['email'];
            
    $address $_POST['address'];
            
            
    //basic serverside validation
            
            
    if($gName == ''){
                
    header('location: ./getCustomerDetails.php?error=given name empty');
                exit();
            }
            if(
    $lName == ''){
                
    header('location: ./getCustomerDetails.php?error=last name empty');
                exit();
            }
            if(!
    filter_var($emailFILTER_VALIDATE_EMAIL)){
                
    header('location: ./getCustomerDetails.php?error=email not valid');
                exit();
            }
            if(
    $address == ''){
                
    header('location: ./getCustomerDetails.php?error=address empty');
                exit();
            }
            
            
    //insert customer into database table 
            
    $insert "INSERT INTO customer " .
                      
    "(gName,lName,email,address) " .
                      
    "VALUES('$gName','$lName','$email','$address')"
            
            
    //$conn is from the connection 
              
    mysqli_query($conn$insert) or die("Unable to insert into Database");
            
            
    header('location: regsuccess.php ' );
            
        }else{
            
    header('location: getCustomerDetails.php');
            exit();
        }

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    what isn't working is the INSERT INTO
    How do you know that? What error or symptom are you getting?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    How do you know that? What error or symptom are you getting?
    Hi CFMaBiSmAd,

    Yes I am self teaching myself PHP, hence the code is only very basic at this point in time.

    Prior to submitting my code I was getting a mysqli_query() error, however I didn't have the $link,$query, only the $query. After adding the $link ($conn) all it does is go straight to the die() function.

    I have checked php.ini from what I can understand all of the development environment errors are on.

    Checked the Server error log, the last lot of errors was to do with the mysqli_query() parameters.

    I have connection to the database as I am running a script that tells me I am connected on "date" and what tables are in my database, so I know I am reaching the tables.

    I can't see why it is skipping over the mysqli_query() and going straight to die().

    Cheers

    BP

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    So, the answer is, you are getting your "Unable to insert into Database" message?

    If you use mysqli_error($conn) as part of the die() output, it will tell you, or at least point in the right direction, why the query is failing.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    Blowpipe (02-18-2014)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yeah sorry, I get the "Unable to insert into database" message.

    Ok, um where do I write the mysqli_error()?

    Do you mean the following??

    mysqli_query($conn,$insert) or die ("Unable to insert into database" .mysqli_error($conn));

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well don't I feel stupid now.

    I have gotten it to work.

    CFMaBiSmAd thanks for your hint of mysqli_error($conn), it did point me in the right direction, I had a different field name in my form and insert codes lName (for last name) it should have been fName (family name) which is what I had in my DB.

    Ran it twice to confirm insertion worked..... like a charm!!

    I will thank you for your post.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Now on to the second (sub) question.

    How do I call the user id (customer id) from the DB so that when a customer is given the "registration is successful" they also get "your Customer ID is: x

    I have no idea on how to implement this at all.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Is it an autoincrement number? If so, you can simply do:
    PHP Code:
    $conn->insert_id;
    // or
    mysqli_insert_id($conn); 
    both of which should return the last surrogate key used.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Is it an autoincrement number? If so, you can simply do:
    PHP Code:
    $conn->insert_id;
    // or
    mysqli_insert_id($conn); 
    both of which should return the last surrogate key used.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Is it an autoincrement number? If so, you can simply do:
    PHP Code:
    $conn->insert_id;
    // or
    mysqli_insert_id($conn); 
    both of which should return the last surrogate key used.
    Hi Fou-Lu,

    I posted this yesterday, however I think the upgrade had started and my post wasn't saved.

    I am not following what you are putting down

    I have had a look at the PHP Manual and think that I need to do a WHERE query

    The customer table is an auto-increment for the customer id.

    How would this work if it wasn't an auto-increment?

    What is meant to be happening is the customer registers that form is sent to the processCustomerDetails.php (above) which in turn is redirected to (if successful) the php page below

    I am self teaching myself PHP and get lost at time (more oft than not)

    Thanks for any pointers

    BP
    regsuccess.php
    PHP Code:
    <?php
    include 'header.html';
    ?>

    <!DOCTYPE html>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Registration Success</title>
    </head>
    <body>
        <hr>
        <h1 align = "center">Registration Successful</h1>
        <h3> 
            Congratulations you have successfully registered as a Customer.
            </br></br> 
            <?php require 'connect_db.php'; echo 'Your Customer ID is: '$conn->insert_id?>.
        </h3>
    </body>
    </html>

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    A WHERE clause is used when providing a condition for a query to constrain the resultset. It won't be necessary for finding an autoincrement id as that's a part of the INSERT clause. The only time you use an INSERT with a WHERE is when you're using an INSERT. . .SELECT statement to draw the information from other tables.
    The mysqli_insert_id function is used to get the last insertion id generated on this connection. You would run that immediately after an INSERT into a database that has an autoincrement. If there is no autoincrement, than it will return either the last autoincrement generated (from any insert) on this connection or 0 if there has been no alteration that generates a last_insert_id.

    Without an autoincrement than all information is already known. Surrogates from the autoincrement are never *required* and are used for convenience. The primary key would simply be a unique value that has been provided and used, so you would never need to retrieve an id. An email address for example would be a candidate key and can be used for the primary. This information is already provided to you so you know the primary key of the record already.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #12
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the info.

    On running the $conn->$insert_id as written in my above code I keep getting "your customer id is: 0."
    even though the customer is added to the DB.

  • #13
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the info.

    On running the $conn->$insert_id as written in my above code I keep getting "your customer id is: 0."
    even though the customer is added to the DB.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $conn->$insert_id isn't valid. You need $conn->insert_id.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #15
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry finger fault local, in my code is $conn->insert_id not $insert_id.

    But I am still getting the number is: 0.

    Would last_insert_id() work, or am I reading the wrong stuff to get this to work??
    Last edited by Blowpipe; 02-22-2014 at 11:04 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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