SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Location
    San Antonio, Texas
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I'm confused and could use some help...

    I need some help, kind of need some guidance. I have a form with a simple text box with a button next to it, when I enter a customer code such as 6 digits and click retrieve. When I click the button, the database retrieves the record based on the customer ID and populates the rest of the form, I just need a starter, just a text box to enter the code, another empty text box that makes a name appear once the correct client ID is entered.

    The DB Schema
    Code:
    CREATE TABLE `client` (
      `id` mediumint(8) unsigned NOT NULL auto_increment,
      `clientID` mediumint default NULL,
      `FirstName` varchar(255) default NULL,
      `LastName` varchar(255) default NULL,
      `Address` varchar(255) default NULL,
      `City` varchar(255),
      `State` varchar(50) default NULL,
      `ZipCode` varchar(10) default NULL,
      PRIMARY KEY (`id`)
    ) AUTO_INCREMENT=1;
    
    INSERT INTO `client` (`clientID`,`FirstName`,`LastName`,`Address`,`City`,`State`,`ZipCode`) VALUES (730776,"Maxwell","Watson","Ap #440-3947 Elit. Street","Pike Creek","DE","47194");
    INSERT INTO `client` (`clientID`,`FirstName`,`LastName`,`Address`,`City`,`State`,`ZipCode`) VALUES (736667,"Nicholas","Jacobs","P.O. Box 394, 597 Elit, Rd.","Augusta","ME","21439");
    INSERT INTO `client` (`clientID`,`FirstName`,`LastName`,`Address`,`City`,`State`,`ZipCode`) VALUES (918489,"Levi","Weeks","7474 Lorem, Road","Clarksville","TN","26122");
    INSERT INTO `client` (`clientID`,`FirstName`,`LastName`,`Address`,`City`,`State`,`ZipCode`) VALUES (572492,"Gary","Gates","P.O. Box 161, 6348 Sed Street","Reading","PA","47926");
    INSERT INTO `client` (`clientID`,`FirstName`,`LastName`,`Address`,`City`,`State`,`ZipCode`) VALUES (287782,"Adam","Tyler","Ap #592-9862 Iaculis, Av.","Laramie","WY","53564");
    index.php
    Code:
    <script>    
        $(function () {
            $('.submit').bind('click', function () {
                $.post('customer.php', {id:$('.code').val()}, function (data) {
                    $('.customers').html(data);
                });
            });
        });
    </script>
    <div class="customers"></div>
    <input type="text" class="code" placeholder="Customer Code" /><input type="button" class="submit" value="search" />
    
    <br><br>
    Client ID: <input type="text" name="clientID"><br>
    First name:<input type="text" name="fname"><br>
    Last name:<input type="text" name="fname"><br>
    Address:<input type="text" name="fname"><br>
    City:<input type="text" name="fname"><br>
    State:<input type="text" name="fname"><br>
    Zip Code:<input type="text" name="fname"><br>

    customers.php
    Code:
    <?php
        $db = new PDO('mysql:host=localhost;dbname=golf;charset=utf8', 'root', '');
        if (isset($_POST['clientID'])) {
            $customer = $db->prepare("SELECT * FROM client WHERE customer_number LIKE '%:clientID' OR customer_number LIKE ':clientID%' OR customer_number LIKE '%:clientID%' OR");
            $customer->execute(array(':clientID'=>$_POST['clientID']));
            while ($row = $customer->fetchObject()) {
                echo $row->customer_number.'<br />';
            }
        }
    ?>
    Thank you for your time!! You're awesome!

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi, welcome to the forums!

    You've got a couple of small bugs in your code which will stop it from working.

    In index.php, the name of the page you're posting to is customer.php and in your post you say it should be customers.php. Also, you're passing the client ID as id, but your PHP script is expecting the variable clientID:
    Code JavaScript:
    // Before
    $.post('customer.php', {id:$('.code').val()}, function (data) {
     
    //After
    $.post('customers.php', {clientID:$('.code').val()}, function (data) {

    In customers.php, your query references the field customer_number which doesn't exist in your DB schema. It should be clientID:
    PHP Code:
    // Before
    $customer $db->prepare("SELECT * FROM client WHERE customer_number LIKE '%:clientID' OR customer_number LIKE ':clientID%' OR customer_number LIKE '%:clientID%' OR");
    $customer->execute(array(':clientID'=>$_POST['clientID']));

    while (
    $row $customer->fetchObject()) {
        echo 
    $row->customer_number.'<br />';
    }

    // After
    $customer $db->prepare("SELECT * FROM client WHERE clientID = :clientID");
    $customer->execute(array(':clientID'=>$_POST['clientID']));

    while (
    $row $customer->fetchObject()) {
        echo 
    $row->clientID.'<br />';



Bookmarks

Posting Permissions

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