Log in

View Full Version : Need help with adding data



custy913
02-06-2008, 04:10 PM
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;
}
?>

jackbenimble4
02-07-2008, 08:23 PM
For one thing, you need quotes around the variables $ziplat and $ziplong in your SQL.

So instead of
$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:
$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() (http://us3.php.net/manual/ro/function.PDOStatement-errorInfo.php) method to retrieve information on what the error was. So for example, if $db were the pdo object:

$errorData = $db->errorInfo();
echo "MySQL Error: " . $errorData[2];


Also, the mysql_error() (http://us2.php.net/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.