0

Im new to programming and i cant understand why this code is not working.

<?php
      $host="localhost";
      $username="ryan"; 
      $password="s@ch!911";
      $db_name="webservice"; 

      $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
      mysql_select_db("$db_name")or die("cannot select DB");

      $ngno = '112';
      $myArray = array("date"=> "Mon Apr 11 00:00:00 GMT+05:30 2016", "Thu Mar 31 00:00:00 GMT+05:30 2016");

      foreach($myArray as $dateSelected => $dateValue){

             $sql = "INSERT INTO datepicker(ngno, date) VALUES($ngno, $dateValue)"; 
             $result = mysql_query($sql);
      }
?>

datepicker table has 3 columns. which are entry_id, ngno, date. entry_id gets auto incremented. I have tried removing the entry_id column as well. But no luck. I have other php files using the same Database and they are all working fine. Inserting, Selecting etc works fine. But when i run this php nothing happens.what am i doing wrong here?

Ryan Aluvihare
  • 225
  • 1
  • 2
  • 9

2 Answers2

0

Try to change your Insert into into this:

$sql = "INSERT INTO datepicker(ngno, date) VALUES('$ngno', '$dateValue')";

let me know if works.

Ryan Riel
  • 34
  • 6
0

You can use this. I hope it will work fine for you.

<?php 

  $host="localhost";
  $username="root"; 
  $password="";
  $db_name="test"; 

  $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db($db_name, $con)or die("cannot select DB");

  $ngno = '112';
  $myArray = array("date"=> "Mon Apr 11 00:00:00 GMT+05:30 2016", "Thu Mar 31 00:00:00 GMT+05:30 2016");

  foreach($myArray as $dateSelected => $dateValue){
         $sql = "INSERT INTO datepicker (`ngno`, `date`) VALUES('$ngno', '$dateValue')"; 
         $result = mysql_query($sql);
  }
?>
Md Mahfuzur Rahman
  • 2,319
  • 2
  • 18
  • 28
  • I tried this as well, But it doesn't work when i put single quotes for the table column names. anyway thanks for the help. – Ryan Aluvihare Apr 05 '16 at 12:25
  • Those are not single quotes what I've put. Press the button which is in left side of "1" (2nd row of the keyboard) to get the quotes. By the way, I'd like to suggest you to avoid the `mysql_` functions in PHP. It has been removed in PHP 7. Try to use `PDO`. Best of luck. – Md Mahfuzur Rahman Apr 05 '16 at 14:24