0

Hi I am a beginner programmer trying to insert values (comments) into a table. I have completed insert functions before but this time the values just will not be inserted. I suspect one of the values inserted, now() is causing the problem. After the fail insertion, var_dump(ini_get('error_log')); produced string(0) "" when displayed on browser. newcomment.php

<?php

header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=UTF-8");

error_reporting(E_ERROR);

try{
    $conn = new mysqli("localhost", "XXXXXXXX_XXX", "XXXXXXX", "XXXXXXX");
    $vendorid = $_GET["vendorid"];
    $userid = $_GET["userid"];
    $comment = $_GET["comment"];

    $query = "insert into vendorcomments (comment, commentdate, vendorid, userid) 
    values ('" . addslashes($comment) . "',  now() ,'" . addslashes($vendorid) . "','". addslashes($userid)."')";

        $result = $conn->query($query);

        if (!$result){
 // unsuccessful insert
//$json_out = "[" . json_encode(array("result"=>0)) . "]";
    var_dump(ini_get('error_log'));

}

        else {
$json_out = "[" . json_encode(array("result"=>1)) . "]";
}

        echo $json_out;

        $conn->close();
}

catch(Exception $e) {
    $json_out = "[".json_encode(array("result"=>0))."]";
    echo $json_out;
}
?>

I have also edited the column value for now() to be DATE_TIME as shown here AND here.

Additional notes:
1) The parameters sent were to request correct, userid, vendorid and comment all had values sent over.
2) xmlhttp.status == 200 !
3) My vendordetails schema:

-- phpMyAdmin SQL Dump
-- version 4.3.7
-- http://www.phpmyadmin.net
--
-- Host: XXXXXXXXXXXXX
-- Generation Time: Dec 07, 2016 at 08:33 PM
-- Server version: 5.7.15
-- PHP Version: 5.4.45-0+deb7u4

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `XXXXXXXXX`
--

-- --------------------------------------------------------

--
-- Table structure for table `vendorcomments`
--

CREATE TABLE IF NOT EXISTS `vendorcomments` (
  `comment` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
  `commentdate` datetime(3) NOT NULL,
  `vendorid` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `userid` varchar(50) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `vendorcomments`
--

INSERT INTO `vendorcomments` (`comment`, `commentdate`, `vendorid`, `userid`) VALUES
('eBay is working and its amazing, to be able to gain cashback.', '0000-00-00 00:00:00.000', '2', 'jimmy');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `vendorcomments`
--
ALTER TABLE `vendorcomments`
  ADD UNIQUE KEY `vendorid` (`vendorid`), ADD UNIQUE KEY `userid` (`userid`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Detective merry
  • 384
  • 1
  • 21
  • Your code is vulnerable to [SQL-Injections](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Please start using Prepared, Parameterized Queries. – Charlotte Dunois Dec 07 '16 at 19:24

2 Answers2

0

Try creating a trigger for the table which will work on insert queries.

CREATE TRIGGER `triggername` BEFORE INSERT ON `vendorcomments` FOR EACH ROW SET NEW.commentdate = NOW();

Then you can simply run your queries without having to insert the timestamp. :)

SmurfTheSmurf
  • 105
  • 2
  • 12
0

Read my schema and found out that userid and vendorid are actually set as primary keys by another member so I could not insert because I was trying to duplicate a primary key

Detective merry
  • 384
  • 1
  • 21