Solved

Issues inserting data into Mysql database using PHP PDO

Posted on 2013-12-06
9
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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