-1

I'm using php 5.5.3..In my scenario i have created a form to insert a task on which user have to fill 3 field namely description of task,due date and priority of task..immediately after that i have added a submit button..whenever i'm going to click on submit button the values should entered in database is my task. Now i wanna asked that how should i add due due date in database..i have tried with specific format but it doesn't work..my configuration id like :

Task.html :

<html>
<head>
<title>Creating a Personal To-Do List</title>
<h3>Add new Task</h3>
</head>
<body>



<form method="post" action="task.php">
    Description :
        <br/>
        <input type="text" name="name"/>
              <p>
    Due date :
        <br/> 
        <input type="text" name="date" size="20"/>
        <p><br/>
    Priority :
        <br/>
        <select name="priority">
        <option name="high">High</option>
        <option name="medium">Medium</option>
        <option name="low">low</option>
        </select>
    <p>
    <input type="submit" name="submit" value="Save Task">
</form>
</body>

</html>

And Task.php :

<?php
$db_hostname="localhost";
$db_database="music";
$db_username="root";
$db_password="p3";

if(isset($_POST['submit']))
{
    if(empty($_POST['name']))
      die("Error : Task name is required");
    $name=$_POST['name'];
    if(empty($_POST['date']))
     die("Error : Please enter a valid date");


$task_date = $_POST['date'];


    if(empty($_POST['priority']))
     die("Error : Please select a priority");
    $priority=$_POST['priority'];


$db_server=mysql_connect($db_hostname,$db_username,$db_password);
if(!$db_server)
  {
    die("Error:unable to connect to server");
  }
else 
    echo "Connected to server";

mysql_select_db($db_database)

    or die("Error:unable to connect to database");
    echo "</br>Connected to $db_database database";
  $task_date = date('Y-m-d');
echo $task_date;
$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority'))");

if(($insert_query)==TRUE)
{   
    echo "<br/>";
    echo "New Task inserted ";
}

}

?>

Please let me know where should i make changes?or is their any another way to do this?..

user3129056
  • 447
  • 3
  • 6
  • 19
  • is your mysql field a **date** or **datetime** ? – mariobgr Sep 12 '14 at 12:18
  • you should use [mysqli_* or PDO instead of mysql_*](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Hristo Valkanov Sep 12 '14 at 12:19
  • 4
    You have one bracket too many `,'$priority'))");` [**error reporting**](http://php.net/manual/en/function.error-reporting.php) and a good IDE would have spotted that, which you're not doing/using. – Funk Forty Niner Sep 12 '14 at 12:19
  • @mariobgr - thnx for reply ..it'd datetime – user3129056 Sep 12 '14 at 12:27
  • @Fred -ii - thnx for reply..i'll correct it – user3129056 Sep 12 '14 at 12:28
  • [`Seems to have fixed it.`](http://stackoverflow.com/questions/25808168/adding-timestamp-or-datetime-in-php#comment40369449_25808168) It's not "against the law" to ask the person who fixed it in the first place, to enter it as an answer (wink). @user3129056 – Funk Forty Niner Sep 12 '14 at 12:39

2 Answers2

0

Try this:

$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority')") 

Instead of this:

  $insert_query=mysql_query("Insert into 
task(name,due,priority)values('$name','$task_date','$priority'))")

Consider using PDO or Mysqli, Mysql is deprecated.

Matheno
  • 4,112
  • 6
  • 36
  • 53
0

Try this it works.

$insert_query=mysql_query("INSERT INTO task(name,due,priority) VALUES ('".$name."','".$task_date."','".$priority."')") or die(mysql_error());

mysql_error() function will tell the errors if data not inserted... and you are using this '$priority'))"); double braces...

user3833682
  • 263
  • 5
  • 20
  • 1
    I have down voted this answer for encouraging the used of the deprecated mysql_* functions. – vascowhite Sep 12 '14 at 12:30
  • 1
    First solve the problem it encourage to learner then tell them the functions you are using are deprecated. You didn't solve the problem and criticize the answer. Don't ever do this again... – user3833682 Sep 12 '14 at 12:33
  • You don't get to tell me how to use my votes, don't ever do that again. – vascowhite Sep 12 '14 at 12:35
  • `vascowhite` I have question can you help me? http://stackoverflow.com/questions/25646387/getting-three-records-in-descending-order-of-each-category-using-codeigniter – user3833682 Sep 12 '14 at 12:42