Jump to content

Add task to database script


kurt.santo

Recommended Posts

Work on page to add tasks to MySql database. It displays an empty page under http://www.jalp.co.uk/testing/phpVarious/add_task.php. Cannot see error myself, but realise username and password need to replaced. If that was the problem I would have expected "cannot connect to database" error message. Does anyone know what is\wrong?Then I know the password, but not any more the username when i set up mysql. is thera a way to find out?Kurt

Link to comment
Share on other sites

So the only thing we can see is a blank page, and you want us to guess what the problem is? Turn on error reporting and find out yourself.
Sorry, guys. Realise now that I am just showing you an empty page with no coding to look at. Still not used to use server-side scripting as opposed to "normal" htm pages...Here come the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />	<title>Add a Task</title></head><body><?php # Script 1.2 - add_task.php/*	This page adds tasks to the tasks table. *	The page both displays and handles the form. */error_reporting (E_ALL); // Connect to the database:$dbc = @mysqli_connect ('localhost', 'username', 'password', 'test') OR die ('<p>Could not connect to the database!</p></body></html>');// Check if the form has been submitted:if (isset($_POST['submitted']) && !empty($_POST['task'])) {	// Sanctify the input...		// 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:	// Assumes Magic Quotes are off!	$task = mysqli_real_escape_string($dbc, $_POST['task']);		// Add the task to the database.	$q = "INSERT INTO tasks (parent_id, task) VALUES ($parent_id, '$task')"; 	$r = mysqli_query($dbc, $q);		// Report on the results:	if (mysqli_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="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>';// Retrieve all the uncompleted 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 = mysqli_query($dbc, $q);// Also store the tasks in an array for use later:$tasks = array();while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) {	// Add to the select 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 tasks by parent_id:function parent_sort ($x, $y) {	return ($x['parent_id'] > $y['parent_id']);}usort ($tasks, 'parent_sort');// Display all the tasks:echo '<h3>Current To-Do List</h3><ul>';foreach ($tasks as $task) {	echo "<li>{$task['task']}</li>\n";}echo '</ul>';?></body></html>

As you can see error reporting is turned on, still empty page. Obviously I use a username (root) and a password, which constists of 5 letters and 2 digits. Would be great if you could have a look...Kurt

Link to comment
Share on other sites

Error reporting might still be disabled, add this along with the call to error_reporting:ini_set("display_errors", 1);If there were errors happening then you should see some sort of message, if you're not then either there aren't any errors or error reporting is off. You can also FTP to the server and look for a file called error_log, that's typically where the errors go if they aren't being displayed. You also have errors being suppressed on the call to mysql_connect so even if errors were happening there and being reported, you still wouldn't know it. It's generally a good idea to enable all errors while you're developing and only disable things and suppress warnings when it goes live.

Link to comment
Share on other sites

Error reporting might still be disabled, add this along with the call to error_reporting:ini_set("display_errors", 1);If there were errors happening then you should see some sort of message, if you're not then either there aren't any errors or error reporting is off. You can also FTP to the server and look for a file called error_log, that's typically where the errors go if they aren't being displayed. You also have errors being suppressed on the call to mysql_connect so even if errors were happening there and being reported, you still wouldn't know it. It's generally a good idea to enable all errors while you're developing and only disable things and suppress warnings when it goes live.
After commenting the database connection bit out of the script found two fatal errors, this explains that there was no code after the <body> tag. Once those were also commented out the rest of the html is displayed (obviously with bits missing that should have come from database). The problem seems to lie in the two lines reading (lines 60 and 65):$r = mysqli_query($dbc, $q);while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM))For the first one I am very confused, in line 37 I had already the same ($r = mysqli_query($dbc, $q):)In this case it did not throw an fatal error. This does not make sense, does it?Kurt
Link to comment
Share on other sites

What do the errors say? The previous query is inside an if block and might not have even been executed.
justsomeguy, Thank you for your help. Found my problem on mysql.com. Although the mysqli-extension were installed, the system variables were missing in Windows. So, I included ;c:php in path. Then, I also had to change the folder of the mysqli-extension in the php ini file. All seems to work now;-) This is the problem: when you are so new to the whole thing you just do not know where to look. You guys probably would have found the same solution in few minutes (or lets say it would not have happened in the first place;-)).Cheers,Kurt
Link to comment
Share on other sites

Yeah, like I've said, knowing how to find answers is the most important thing. Always start with the error messages, if you get an error pay attention to what the error message is saying is wrong and think about what it's trying to tell you and what it might mean in the context of your application. Glad you got it worked out.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...