Results 1 to 2 of 2

Thread: Need help with adding data

  1. #1
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with adding data

    I have this form on my site. The information supposed to be stored in database (Mysql) but nothing happened.
    Can anyone please help me how to add new info in a database?


    <?php

    require_once('DB.php');
    $db = DB::connect("mysql://xxxxxx:xxxxx@localhost/xxxxx");
    if(!empty($_POST['store_name'])) {
    $store_name = addslashes($_POST['store_name']);
    $addr1 = addslashes($_POST['addr1']);
    $addr2 = addslashes($_POST['addr2']);
    $manager = addslashes($_POST['manager']);
    $phone = addslashes($_POST['phone']);
    $city = addslashes($_POST['city']);
    $state = addslashes($_POST['state']);
    $zip = addslashes($_POST['zip']);
    $notes = addslashes($_POST['notes']);

    $localq = $db->query("select ziplat, ziplong from zipcodes where zipcode = $zip");
    $localr = $localq->fetchRow(DB_FETCHMODE_ASSOC);
    $ziplat = $localr['ziplat'];
    $ziplong = $localr['ziplong'];

    // query the DB
    $sql = "INSERT INTO stores (store_name, addr1, addr2, city, state, zip, phone, manager, notes, ziplat, ziplong) values('$store_name', '$addr1', '$addr2', '$city', '$state', '$zip', '$phone', '$manager', '$notes', $ziplat, $ziplong);";
    $q = $db->query($sql);
    $db->commit();
    ?> <font color=red size=3><b>Store<u><?= $store_name ?></u> Added.</b></font>
    <?php
    $disabled = 'disabled=1';
    #exit;
    }
    ?>

  2. #2
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    For one thing, you need quotes around the variables $ziplat and $ziplong in your SQL.

    So instead of
    Code:
    $sql = "INSERT INTO stores (store_name, addr1, addr2, city, state, zip, phone, manager, notes, ziplat, ziplong) values('$store_name', '$addr1', '$addr2', '$city', '$state', '$zip', '$phone', '$manager', '$notes', $ziplat, $ziplong);";
    It would be:
    Code:
    $sql = "INSERT INTO stores (store_name, addr1, addr2, city, state, zip, phone, manager, notes, ziplat, ziplong) values('$store_name', '$addr1', '$addr2', '$city', '$state', '$zip', '$phone', '$manager', '$notes', '$ziplat', '$ziplong');";
    In case it's something besides that, what's the content of the DB class? If you're using PDO you can use the errorInfo() method to retrieve information on what the error was. So for example, if $db were the pdo object:
    Code:
    $errorData = $db->errorInfo();
    echo "MySQL Error: " . $errorData[2];
    Also, the mysql_error() function can retrieve the mysql errors.

    Whatever you're using in the DB class, you can use those functions to get the error from the most recently executed function. If you do that and still need help, we can help you decipher the error message.

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
  •