Solved

Issues inserting data into Mysql database using PHP PDO

Posted on 2013-12-06
9
574 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

734 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