Solved

Issues inserting data into Mysql database using PHP PDO

Posted on 2013-12-06
9
564 Views
Last Modified: 2013-12-06
Below is the code. I am receiver no errors and no data is being inserted. Everything looks good to me.

<?php
require("pdo_connection.php");

pdo_connection("localhost","ats");

$id = 1887;

$stmt = $db->prepare("SELECT * FROM atsassets WHERE id = :id");
$stmt->bindParam('id', $id);
$stmt->execute();

$row = $stmt->fetch();
$date = date('Y-m-d');
$aname = $row['asset'];
$byuser = $_SERVER['REMOTE_USER'];


$sql = "INSERT INTO decom 
(asset, serial, model, os, lastupdate, datesetup, decomdate, prevuser, asigned, byuser) 
VALUES 
(:asset, :serial, :model, :os, :lastupdate, :datesetup, :decomdate, :prevuser, :asigned, :byuser)";
try
{
$qeury = $db->prepare($sql);
$qeury->execute(array(
':asset'=>$row['asset'],
':serial'=>$row['serial'],
':model'=>$row['model'],
':os'=>$row['os'],
':lastupdate'=>$row['lastupdate'],
':datesetup'=>$row['datesetup'],
':decomdate'=>$date,
':prevuser'=>$row['prevuser'],
':asigned'=>$row['asignedto'],
':byuser'=>$byuser
));
}
catch(PDOException $e)
{
echo 'Query failed'.$e->getMessage();
}

$stmt = null;






?>

Open in new window

0
Comment
Question by:ats2012
  • 5
  • 4
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39700991
I need to see pdo_connection.php code also, please.
0
 

Author Comment

by:ats2012
ID: 39700998
pdo_connection.php

<?php
function pdo_connection($host,$dbname)
{
global $db;

$db = new PDO("mysql:host=$host;dbname=$dbname", "root", "pass");

return $db;
}

?>

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39701019
Try to use var_dump to see if $row is correctly filled

$stmt = $db->prepare("SELECT * FROM atsassets WHERE id = :id");
$stmt->bindParam('id', $id);
$stmt->execute();

$row = $stmt->fetch();
echo "<pre>";
var_dump($row);
echo "</pre>";

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:ats2012
ID: 39701035
It is pulling data from the database.

array (size=44)
  'id' => string '1887' (length=4)
  0 => string '1887' (length=4)
  'asset' => string 'test' (length=4)
  1 => string 'test' (length=4)
  'serial' => string '' (length=0)
  2 => string '' (length=0)
  'model' => string '' (length=0)
  3 => string '' (length=0)
  'os' => string '' (length=0)
  4 => string '' (length=0)
  'lastupdate' => null
  5 => null
  'datesetup' => null
  6 => null
  'prevuser' => null
  7 => null
  'asignedto' => null
  8 => null
  'description' => null
  9 => null
  'ipone' => null
  10 => null
  'iptwo' => null
  11 => null
  'ipthree' => null
  12 => null
  'ipfour' => null
  13 => null
  'info' => string '' (length=0)
  14 => string '' (length=0)
  'type' => string 'Computer' (length=8)
  15 => string 'Computer' (length=8)
  'size' => null
  16 => null
  'charged' => string 'yes' (length=3)
  17 => string 'yes' (length=3)
  'status' => null
  18 => null
  'dualmon' => string 'no' (length=2)
  19 => string 'no' (length=2)
  'setupdate' => null
  20 => null
  'ssd' => string 'no' (length=2)
  21 => string 'no' (length=2)

Open in new window

0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39701055
Well, now we are sure your first query correctly runs and returns expected values.
Now I suggest two things:

1. add quotes to $date and $byuser value within the array given to execute statement
2. add error_reporting(E_ALL); at the very top of your scritp: the very strange thing is that you don't get any message and we need it to understand what's going wrong here.
0
 

Author Comment

by:ats2012
ID: 39701072
I made the changes to this and getting no errors. To me this should be working. Don't understand this.

<?php
error_reporting(E_ALL);

require("pdo_connection.php");

pdo_connection("localhost","ats");

$id = 1887;

$stmt = $db->prepare("SELECT * FROM atsassets WHERE id = :id");
$stmt->bindParam('id', $id);
$stmt->execute();

$row = $stmt->fetch();

echo "<pre>";
var_dump($row);
echo "</pre>";

$date = date('Y-m-d');
$aname = $row['asset'];
$byuser = $_SERVER['REMOTE_USER'];


$sql = "INSERT INTO decom 
(asset, serial, model, os, lastupdate, datesetup, decomdate, prevuser, asigned, byuser) 
VALUES 
(:asset, :serial, :model, :os, :lastupdate, :datesetup, :decomdate, :prevuser, :asigned, :byuser)";

$asset = $row['asset'];
$serial = $row['serial'];
$model = $row['model'];
$os = $row['os'];
$lastupdate = $row['lastupdate'];
$datesetup = $row['datesetup'];
$prevuser = $row['prevuser'];
$asigned = $row['asignedto'];

try
{
$qeury = $db->prepare($sql);
$qeury->execute(array(
':asset'=>$asset,
':serial'=>$serial,
':model'=>$model,
':os'=>$os,
':lastupdate'=>$lastupdate,
':datesetup'=>$datesetup,
':decomdate'=>$date,
':prevuser'=>$prevuser,
':asigned'=>$asigned,
':byuser'=>$byuser
));
}
catch(PDOException $e)
{
echo 'Query failed'.$e->getMessage();
}

$stmt = null;






?>

Open in new window

0
 

Author Comment

by:ats2012
ID: 39701107
Found the issue. The date fields were all NULL so when it tried to insert the query it freaked out. Added 0000-00-00 to all the date fields and now its working. They all went NULL because i added a manually field for testing. Wont make that mistake again. Thanks for your help.
0
 

Author Closing Comment

by:ats2012
ID: 39701110
Thanks.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39701117
Thanks for points but I should see it from your dumped data!
Cheers
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question