Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Issues inserting data into Mysql database using PHP PDO

Posted on 2013-12-06
9
Medium Priority
?
585 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

604 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