kurt.santo Posted April 4, 2008 Share Posted April 4, 2008 Working on page to upload data to one table. Using the following code: // display/handling form to upload dataerror_reporting (E_ALL); // connect to the database require_once ('../mysql_connect2.php'); // connect to the database// check if the form has been submittedif (isset($_POST['submitted'])) { // handle the form //check for a name if(!empty($POST['name'])) { $n = escape_data($POST['name']); } else { $n = FALSE; echo '<p>Please enter a name</p>'; } var_dump($n); // add data to database $query = "INSERT INTO artist (name) VALUES ($n)"; $result = mysqli_query($dbc, $query); // report on the results if (mysqli_affected_rows($dbc) == 1) { echo '<p>Data added!</p>'; } else { echo '<p>Data could not be added!</p>'; }}// display the formecho '<form action="upload2.php" method="post"><fieldset><legend>Upload data</legend><p>Name: <input name="name" type="text" size="60" maxlength="100" /></p><input name="submitted" type="hidden" value="true" /><input name="submit" type="submit" value="Upload data" /></fieldset></form>'; the dump shows me a bool(false) after I entered a name in the input field and click sumit. It also says that I should enter a name and says that the data could not be added, short nothing worked. Does anyone know where the bug lies?Kurt Link to comment Share on other sites More sharing options...
Lulzim Posted April 4, 2008 Share Posted April 4, 2008 if(!empty($POST['name'])){$n = escape_data($POST['name']);} else {$n = FALSE;echo '<p>Please enter a name</p>';}if should be $_POST, you missed an underline Link to comment Share on other sites More sharing options...
kurt.santo Posted April 4, 2008 Author Share Posted April 4, 2008 if should be $_POST, you missed an underlineThanks. Unfortunately, still not working. Had a go on code that works:if (isset($_POST['submitted'])) { // Add the task to the database. $q = sprintf("INSERT INTO artist (task) VALUES ('%s')", mysqli_real_escape_string($dbc, $_POST['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="upload3.php" method="post"><fieldset><legend>Add a Task</legend><p>Task: <input name="task" type="text" size="60" maxlength="100" /></p><input name="submitted" type="hidden" value="true" /><input name="submit" type="submit" value="Add This Task" /></fieldset></form>'; Each time I change tasks to name and choose the right table it does not add the data. What is going wrong with that table/field? I really do not understand, with the different table's field in same database the exact code is working...Kurt Link to comment Share on other sites More sharing options...
justsomeguy Posted April 4, 2008 Share Posted April 4, 2008 Are you using reserved words for table or field names? Link to comment Share on other sites More sharing options...
kurt.santo Posted April 4, 2008 Author Share Posted April 4, 2008 Are you using reserved words for table or field names?I use name for relevant field. In MySQL reserved name list this one should be fine or am I wrong here?Kurt Link to comment Share on other sites More sharing options...
jlhaslip Posted April 5, 2008 Share Posted April 5, 2008 you have error_reporting set to all. are you getting any errors?add this at the end of the form to print out the post data: <?phpecho '<pre>';print_r($_POST);echo '</pre>';?> that will tell you what the values are in the POST array to see what your form is passing to the page.what happens if you use this query in phpmyadmin with the data set to a proper value?Is this file saved as upload2.php, which is the file specified in the action= for the Form?and I assume the escape_data function is defined in the connection included file?Which chapter of which Ullman book is this from? Looks like 2nd edition of Dynamic Web Sites???Hint: $query = "INSERT INTO artist (name) VALUES ('$n')"; single quotes around the variable in the query. Link to comment Share on other sites More sharing options...
kurt.santo Posted April 5, 2008 Author Share Posted April 5, 2008 you have error_reporting set to all. are you getting any errors?add this at the end of the form to print out the post data:<?phpecho '<pre>';print_r($_POST);echo '</pre>';?> that will tell you what the values are in the POST array to see what your form is passing to the page.what happens if you use this query in phpmyadmin with the data set to a proper value?Is this file saved as upload2.php, which is the file specified in the action= for the Form?and I assume the escape_data function is defined in the connection included file?Which chapter of which Ullman book is this from? Looks like 2nd edition of Dynamic Web Sites???Hint: $query = "INSERT INTO artist (name) VALUES ('$n')"; single quotes around the variable in the query. There is no error message, it just says that the task could not be added. Using the given code at the end it says: [task] => test [submitted] => true [submit] => Add This TaskSo, the problem really lies with the database somehow...I did not really get what you meant by the thing I should do in PHPMyAdmin and yes, I am refereing in form to same page (currently nr 3). Good guess, I work my way with Larry's books, is really excellent to learn php and mysql. The code is from Larry Ullman's PHP 5 Advanced in script add_task2.php on page 39. I just stripped it down to a bare minium, but really stuck now...Kurt Link to comment Share on other sites More sharing options...
jlhaslip Posted April 5, 2008 Share Posted April 5, 2008 I did not really get what you meant by the thing I should do in PHPMyAdminOne of the best techniques to debug database queries that are not working in a script is to have the script echo out the faulty or suspect query so that you can cut and paste the actual query into the phpmyadmin (or similar) SQL query manager.Doing this will tell you if the query works outside of the script. Then you know whether it is the Query or the scripting logic that needs to be checked. If the Query works in phpmyadmin, you need to look around or beyond the query. If the query fails in phpmyadmin, then you have a bad query. Link to comment Share on other sites More sharing options...
kurt.santo Posted April 5, 2008 Author Share Posted April 5, 2008 One of the best techniques to debug database queries that are not working in a script is to have the script echo out the faulty or suspect query so that you can cut and paste the actual query into the phpmyadmin (or similar) SQL query manager.Doing this will tell you if the query works outside of the script. Then you know whether it is the Query or the scripting logic that needs to be checked. If the Query works in phpmyadmin, you need to look around or beyond the query. If the query fails in phpmyadmin, then you have a bad query.It complains in phpmyadmin that several fields do not have a default value. Why should they? I have fields that need to filled out with null=no and fields that are not necessary with null=yes and a default of NULL. Is this not right?Kurt Link to comment Share on other sites More sharing options...
jlhaslip Posted April 5, 2008 Share Posted April 5, 2008 we would need to see the schema of the table you are using. Link to comment Share on other sites More sharing options...
kurt.santo Posted April 5, 2008 Author Share Posted April 5, 2008 we would need to see the schema of the table you are using.Did a dump of table, hope this is what you are after (let me know if not):Table structure for table `tasks`CREATE TABLE IF NOT EXISTS `tasks` ( `taks_id` int(3) NOT NULL, `task` varchar(20) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1; Kurt Link to comment Share on other sites More sharing options...
jlhaslip Posted April 6, 2008 Share Posted April 6, 2008 Your task_id is not auto incremented and is not null, so no value is being placed into it.also, you are inserting into a Table named artist. INSERT INTO artist (task) You need to return to the book's script and start fresh, I think. Link to comment Share on other sites More sharing options...
kurt.santo Posted April 6, 2008 Author Share Posted April 6, 2008 Your task_id is not auto incremented and is not null, so no value is being placed into it.also, you are inserting into a Table named artist.INSERT INTO artist (task) You need to return to the book's script and start fresh, I think. Guess you are right, I am really confused here... Start again... Thanks!Kurt Link to comment Share on other sites More sharing options...
kv79 Posted April 6, 2008 Share Posted April 6, 2008 I sow this comment as mistake , how can some with this connect to database .require_once ('../mysql_connect2.php'); // connect to the databaseTell me if I am wrong . Link to comment Share on other sites More sharing options...
kurt.santo Posted April 6, 2008 Author Share Posted April 6, 2008 I sow this comment as mistake , how can some with this connect to database .require_once ('../mysql_connect2.php'); // connect to the databaseTell me if I am wrong .The file with the connection data is required here. This is why I put connect to database in comment. Is this what you mean?Kurt Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.