I try to learn more php and I have a script for a to-do list. You just add tasks to the database. When I tried the script on localhost I got this error: The task could not be added! Can, please, someone tell me where I'm wrong? This is the code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Add task</title> </head> <body> <?php //This page adds tasks to the tasks table //Connect to the database $dbc = @mysql_connect('localhost', 'root', '', 'test') or die ('<p> Could not connec to the database !</p></body></html>'); //Check if the form has been submitted if(isset($_POST['submitted']) && !empty($_POST['task'])){ //The parent_id must be an integer if(isset($_POST['parent_id'])) { $parent_id = (int) $_POST['parent_id']; } else{ $parent_id = 0; } //Escape the task $task = mysql_real_escape_string($_POST['task'], $dbc); //Add the task to the database $q = "INSERT INTO tasks(parent_id, task) VALUES ($parent_id, '$task')"; $r = mysql_query($q, $dbc); //Report the results if(mysql_affected_rows($dbc) == 1){ echo '<p>The task has been added!</p>' ; }else{ echo '<p>The task could not be added!</p>' ; } }// END of submission IF //Display the form echo '<form action="script1.2-add_task.php" method="POST"> <fieldset> <legend>Add a task</legend> <p>Task <input name="task" type="text" size="60" maxlength="100" /></p> <p>Parent task <select name="parent_id"><option value="0">None</option>'; //Retrive all the incomplete tasks $q = 'SELECT task_id, parent_id, task FROM tasks WHERE date_completed = "0000-00-00 00:00:00" ORDER BY date_added ASC'; $r = mysql_query($dbc, $q); //Also store the tasks in an array for use later $tasks = array(); while(list($task_id, $parent_id, $task) = mysql_fetch_array($r, MYSQL_NUM)){ //Add to the selected menu echo "<option value =\"$task_id\">$task</option>'\n"; //Add to the array $tasks[] = array('task_id' => $task_id, 'parent_id' => $parent_id, 'task' => $task); } echo '</select></p> <input name="submitted" type="hidden" value="true" /> <input name="submit" type="submit" value="Add this task" /> </fieldset> </form>'; //Sort the task by parent_id function parent_sort($x, $y) { return ($x[$parent_id] > $y[$parent_id]); } usort ($tasks, 'parent_sort'); //Display the tasks echo '<h3> Curent to do list </h3><ul>'; foreach ($tasks as $task){ echo "<li>{$task['task']}</li>\n "; } echo '</ul>'; ?> </body> </html> Code (markup): and the database is: -- phpMyAdmin SQL Dump -- version 4.0.4.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Aug 26, 2013 at 03:59 PM -- Server version: 5.5.32 -- PHP Version: 5.4.16 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: `test` -- CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE `test`; DELIMITER $$ -- -- Procedures -- CREATE DEFINER=`root`@`localhost` PROCEDURE `test_multi_sets`() DETERMINISTIC begin select user() as first_col; select user() as first_col, now() as second_col; select user() as first_col, now() as second_col, now() as third_col; end$$ DELIMITER ; -- -------------------------------------------------------- -- -- Table structure for table `tasks` -- CREATE TABLE IF NOT EXISTS `tasks` ( `task_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `parent_id` int(10) unsigned NOT NULL, `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `date_completed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `task` varchar(100) NOT NULL, PRIMARY KEY (`task_id`), KEY `parent_id` (`parent_id`,`date_added`,`date_completed`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; /*!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 */; Code (markup):
After the statement: $r = mysql_query($q, $dbc); PHP: check to see the value of $r. if($r == true) echo "row added"; else echo "Row adding failed"; PHP: and see what you get. Also check to see if you get rows added on the database or not even if the script says "Task not added".
I tried what you said big 'G' but the result is the same. I put your code samayk and I got Row adding failed. So why I can't add anything in the database? I don't have any error that I can't connect to the database.
I'd like to see the sql line looking like this $q = "INSERT INTO tasks(`parent_id`, `task`) VALUES ('{$parent_id}', '{$task}')"; Code (markup): and the line when you actually run it - temporarily - looking like $r=mysql_query($q,$dbc) or die(mysql_error().'<br/>'.$q; Code (markup): So if it fails the error message might be enough to let you know what the issue is, if not, copy the query and try it through phpMyAdmin and if you are still stuck paste it into the thread.
I put your code sarahk and, also, I printed the insert and I got this: INSERT INTO tasks('parent_id', 'task') VALUES ('0', 'Must do this!') No database selected The mysql_error is that the database is not selected, but I put it in the $dbc in the top of the script. I don't understant what is happening.
Look at the Parameters you pass these functions, what you have wrote is wrong (mysq_connect doesn't accept db name): http://php.net/manual/en/function.mysql-connect.php http://php.net/manual/en/function.mysql-select-db.php
That was perfect. Now you know what you have missed. Follow the links @ThePHPMaster has given you and you'll be sorted. I hadn't actually checked that part, but that's what is so good about error messages... they point you in the right direction.
Thank you ThePHPMaster, but now I get this error: INSERT INTO tasks('parent_id', 'task') VALUES ('0', 'Must do this!')You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''parent_id', 'task') VALUES ('0', 'Must do this!')' at line 1 How can I check this error in MySql?
Thank you to all of you for help. I put by mistake single quotes for parent_id and task in: INSERT INTO tasks('parent_id', 'task'). I took the quotes and now works.