3

Any ideas why the following code is not adding anything into the database once the user fills out the form? I'd really appreciate it.

Thank you!

    if($_SESSION['loginSuccess']==1) {

        // ============================================================
        // = Create the table of current tasks stored in the database =
        // ============================================================
        $userID = $_SESSION['userID'];
        $result = mysql_query("SELECT * FROM Tasks WHERE userID = '$userID'");
        echo "<div id=\"draggable\" class=\"ui-widget-content\"><table border='5'><tr class=\"ui-widget-header\"><td><u>Task Name:</u></td><td><u>Class:</u></td><td><u>Due Date:</u></td><td><u>Task Type:</u></td></tr>";
        echo $_SESSION['userID'];
        while($row = mysql_fetch_array($result)) {
            $taskName = $row[1];

            $class = $row[2];

            $taskDueDate = $row[3];

            $taskType = $row[4];


            echo "<tr><td>'$taskName'</td><td>'$class'</td><td>'$taskDueDate'</td><td>'$taskType'</td></tr>";
        }
        echo "</table>";

        function addNewTask ($name, $class, $dueDate, $type) {
            mysql_query("INSERT INTO Tasks VALUES ('$userID','$name', '$class', '$dueDate', '$type')");
        }

    if($_POST['taskName'] != NULL) {
            addNewTask($_POST['taskName'], $_POST['class'], $_POST['dueDate'], $_POST['dueDate']);
        }

?>


<!-- <img border="1" alt="New" src="/newTask.png" id="newTask" onmouseClick="showTaskField"/> -->
<p><form name="newTask" method="post" action="index.php" id="newTask"><br>
    Task Name: <input name="taskName" type="text"> (necessary)<br>
    Class: <input name="class" type="text"><Br>
    Due Date: <input name="dueDate" type="text" id="datepicker"><Br>
    Type: 
    <input type="submit"></p></div>
JAL
  • 21,295
  • 1
  • 48
  • 66
joshim5
  • 2,292
  • 4
  • 28
  • 40
  • 6
    Whoa, whoa, whoa! [SQL Injection](http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php) alert! Please read up on securing your queries (I think using [PDO](http://php.net/pdo) and [prepared statements](http://php.net/manual/en/pdo.prepared-statements.php) is the best solution, but there are a few), then check 1) is `addNewTask` being run? 2) is the query returning an error? (print `mysql_error()` to find out) – Matchu Dec 02 '10 at 03:03
  • Debug your `mysql_query` call by checking if it returns `true` or not and using `mysql_error` to figure out what went wrong if it returns `false`. – deceze Dec 02 '10 at 03:03
  • You really should [escape the strings you pass to SQL](http://stackoverflow.com/questions/2687866/escaping-single-quote-in-php-when-inserting-into-mysql). – cambraca Dec 02 '10 at 03:11
  • Read the comments below about the mysql error thing. And haha about forgetting to escape the quotes. Thanks for the reminder. – joshim5 Dec 02 '10 at 03:15

1 Answers1

4

Try getting rid of the ' around the variables in the insert statement. If that does nothing echoing mysql_error().

Galen
  • 29,976
  • 9
  • 71
  • 89
  • In previous INSERT INTO statements (that work), I had to put single-quotes around the variables. – joshim5 Dec 02 '10 at 03:04
  • The error mysql_error() is giving me is: Duplicate entry '0' for key 'PRIMARY'. I had primary keys in the beginning but removed them. Now I don't have a primary key. – joshim5 Dec 02 '10 at 03:05
  • 1
    @joshim Well, obviously you *do* have primary keys in your database. – deceze Dec 02 '10 at 03:07
  • Actually, I realize now I don't have a way to delete the primary key from it. Do you need a primary key in every table? – joshim5 Dec 02 '10 at 03:08
  • @joshim5: you don't *need* a primary key, but it's highly recommended if you ever plan to refer to a specific task. You probably will once it comes time to edit or delete. Regardless, it seems like you may have somehow removed the ID column but left the primary key index in place. – Matchu Dec 02 '10 at 03:09
  • Now, trying to add in a seperate row to make that primary key I'm getting: MySQL said: #1068 - Multiple primary key defined – joshim5 Dec 02 '10 at 03:10
  • I didn't delete the ID column. It's still there – joshim5 Dec 02 '10 at 03:11
  • Am I able to take away the attribute of primary key from a row once it's already happened? – joshim5 Dec 02 '10 at 03:11
  • @joshim5: the primary key isn't really a row attribute, even though you can kinda pretend like it is when creating a column for shorthand. The primary key is a special index named `PRIMARY`. – Matchu Dec 02 '10 at 03:17
  • Each row contains:userID - name - class - dueDate - type – joshim5 Dec 02 '10 at 22:03