Im trying to add some data to a database i just created.
Here is the database. It works fine!
<?php $installer = $this; $installer->startSetup(); $sql=<<<SQLTEXT create table emailorders( emailorders_id int not null auto_increment, order_date varchar(255) not null, product_name varchar(255) not null, location varchar(255) not null, date_for varchar(255) not null, name_title varchar(100) not null, email varchar(255) not null, company varchar(255) not null, adress varchar(255) not null, zipcode varchar(255) not null, city varchar(255) not null, phone varchar(255) not null, comment text not null, order_done int not null default 0, primary key(emailorders_id) ); SQLTEXT; $installer->run($sql); //demo //Mage::getModel('core/url_rewrite')->setId(null); //demo $installer->endSetup();
Here are the code when i try to add the data to det created database
<?php $details = $this->getDetails(); $conferance_name = $details['product_name']; $hotel = $details['hotelval']; $date = $details['dateval']; $ogtitel = $details['navn_title']; $virksomhed = $details['virksomhed']; $adresse = $details['adresse']; $postnr = $details['postnr']; $by = $details['by']; $telefon = $details['telefon']; $email = $details['email']; $kommentarer = $details['kommentarer']; $newdate = date('Y-m-d') ?> <?php $sql = "INSERT INTO emailorders (order_date, product_name, location, date_for, name_title, email, company, adress, zipcode, city, phone, comment, order_done) VALUES ('$newdate', $conferance_name', '$hotel', $date, '$ogtitel', '$email', '$virksomhed', '$adresse', '$postnr', '$by', '$telefon', '$kommentarer','0')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
All my variables works, i have echo them to check. But why is the data not going in the database?
Hope you can help
Hi,
Here is the solution, may be it can help you:
By default, Magento will automatically connect to it’s database and provide two separate resources which you can use to access data: core_read and core_write. As you can probably guess, core_read is for reading from the database while core_write is for writing to the database.
$conn = Mage::getSingleton('core/resource')->getConnection('core_read');
// Prints a list of all website names
$results = $conn->fetchAll("SELECT * FROM core_website;");
foreach($results as $row) {
echo $row['name'] . "\n";}
or to write to the table:
$conn->query('INSERT or UPDATE query here')
In order to get last insert id:
$conn->lastInsertId();
please accept my post if this help you
Thanks