Avatar of ats2012
ats2012
 asked on

Issues inserting data into Mysql database using PHP PDO

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

PHPMySQL Server

Avatar of undefined
Last Comment
Marco Gasi

8/22/2022 - Mon
Marco Gasi

I need to see pdo_connection.php code also, please.
ats2012

ASKER
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

Marco Gasi

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
ats2012

ASKER
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

ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ats2012

ASKER
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

ats2012

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ats2012

ASKER
Thanks.
Marco Gasi

Thanks for points but I should see it from your dumped data!
Cheers