Lucuis Posted October 14, 2008 Share Posted October 14, 2008 Hi again everyone. Alright, so i have followed the "PHP Basic Database Handling" tutorial on this site. And i've run into a problem. I've got 4 Notices, and 1 Fatal Error. Which i think are all related. NOTE: I did not copy/paste, i sat and typed out every bit of it in an attempt to learn. For those that have seen my previous threads should be proud Notice: Undefined index: name in /var/www/html/FatalIndustries/database.php on line 9 Notice: Undefined index: cname in /var/www/html/FatalIndustries/database.php on line 17 Notice: Undefined index: name in /var/www/html/FatalIndustries/database.php on line 29 Notice: Undefined index: orderby in /var/www/html/FatalIndustries/database.php on line 37 Fatal error: SQL in /var/www/html/FatalIndustries/database.php on line 50 My question is how do i go about defining those indexes? Here's what i got. I'm guessing it's something the tutorial assumed i would add, or change to fit my database. <?php // error reporting ini_set('display_errors','On'); error_reporting(E_ALL); // connect to DB $conn = mysql_connect('localhost','FatalIndustries','*********') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('FatalIndustries',$conn) or trigger_error("SQL", E_USER_ERROR); // INSERT: if we have a name to add if($_POST['name']) { // little bit of cleaning $name = mysql_real_escape_string($_POST['name']); // insert new name into table $sql = "INSERT INTO info (id, name) VALUES ('','$name')"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end if // UPDATE: if we have name(s) to change if ($_POST['cname']) { // for each name to change foreach($_POST['cname'] as $cid => $cname) { // little bit of cleaning $id = mysql_real_escape_string($cid); $name = mysql_real_escape_string($cname); // update name in the table $sql = "UPDATE info SET name = '$name' WHERE id = '$id'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end of foreach } // end if // DELETE: if we have a name to delete if ($_GET['name']) { // little bit of cleaning $name = mysql_real_escape_string($_GET['name']); // delete name from table $sql = "DELETE FROM info WHERE name = '$name'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end if // ORDER BY: if one of the links was clicked if ($_GET['orderby']) { // make an array of allowed names $allowed = array('id','name'); // bit of cleaning $order = mysql_real_escape_string($_GET['orderby']); // is it a valid column name? yes: use it. no: default to 'id' $order = (in_array($order, $allowed))? $order : "id"; // if no link clicked, default to 'id' } else { $order = "id"; } // end else // SELECT: get the list of names from the database $sql = "SELECT id, name FROM info ORDER BY $order"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); /**** end deal with the database ****/ /**** list everything out ****/ // list columns echo <<<LISTCOLS <form action = '{$_SERVER['PHP_SELF']}' method = 'post'> <table border = '1'> <tr> <td><a href = '{$_SERVER['PHP_SELF']}?oderby=id'>id</td> <td><a href = '{$_SERVER['PHP_SELF']}?oderby=name'>name</td> <td>delete</td> </tr> LISTCOLS; // loop through the list of names while ($list = mysql_fetch_assoc($result)) { echo <<<LISTINFO <tr> <td>{$list['id']}</td> <td><input type = 'text' name = 'cname[{$list['id']}] <td><a href = '{$_SERVER['PHP_SELF']}?name={$list['name']}'>delete</a></td> </tr> LISTINFO; } // end while // list input box for adding new entry echo <<<NEWENTRY <tr> <td bgcolor = 'gray'></td> <td><input type = 'text' name = 'name'></td> <td bgcolor = 'gray'></td> </tr><tr> <td></td> <td align = 'center'> <input type = 'submit' value = 'submit'></td> <td></td> </tr> </table> </form> NEWENTRY; /**** end list everything out ****/ ?> Quote Link to comment Share on other sites More sharing options...
hvle Posted October 14, 2008 Share Posted October 14, 2008 forget about the indexes notice (they're just notice to help debugging, not error). What do you have in /var/www/html/FatalIndustries/database.php on line 50 ? Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 if you use <?php if(isset($_GET['name'])) ?> instead of <?php if($_GET['name']) ?> it will get rid of those warnings Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 You should consider echoing your query to the browser. I always set up a debug boolean at the top that turn on and off debug mode. If on, every query is echoed to the browser and I can see what the problem is. Quote Link to comment Share on other sites More sharing options...
Lucuis Posted October 14, 2008 Author Share Posted October 14, 2008 forget about the indexes notice (they're just notice to help debugging, not error). What do you have in /var/www/html/FatalIndustries/database.php on line 50 ? Line 50 $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); You should consider echoing your query to the browser. I always set up a debug boolean at the top that turn on and off debug mode. If on, every query is echoed to the browser and I can see what the problem is. How would i go about doing that? I don't remember coming across that in the tutorials i've read thus far. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted October 14, 2008 Share Posted October 14, 2008 just echo $sql; will print out the info sql has Quote Link to comment Share on other sites More sharing options...
Lucuis Posted October 14, 2008 Author Share Posted October 14, 2008 I'm not sure where to put echo $sql but i put it one line before the Fatal Error. the last notice changed to this. Notice: Undefined index: orderby in /var/www/html/FatalIndustries/database.php on line 37 SELECT id, name FROM info ORDER BY id Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 Now try putting that "SELECT id, name FROM info ORDER BY id" into your database manager query window and adjust it until it works. I'm guessing one of those columns is a key word which you will need to surround with ``. BTW: while you are learning, you should start most of your queries inside of your database manager, make sure they work first, then input them into your code. Once you learn php error handling, which you can find in the manual just search on google for php error handling, then it will be easier for you to write sql in your code on the fly. Quote Link to comment Share on other sites More sharing options...
Lucuis Posted October 14, 2008 Author Share Posted October 14, 2008 Now try putting that "SELECT id, name FROM info ORDER BY id" into your database manager query window and adjust it until it works. I'm guessing one of those columns is a key word which you will need to surround with ``. You lost me there, lol. I don't think i have a database manager query window. Unless you mean the Terminal where i created the database using mysql. I have a question. When i was following the old tutorial, everything worked up until i tried adding in sessions/logins etc. The database managing, editing tables etc worked fine. So far as i know. Would it be ok to use that code, since it's the old stuff, and then continue fresh from there? Or would the older could inevitably conflict somewhere further on? Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 Yes, the terminal window. This looks like a valid sql statement to me so I am assuming that some of the columns like "id" and "name" and possibly the tablename "info" may be reserved words in mysql so require must be quoted with ``. Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 14, 2008 Share Posted October 14, 2008 I have a question. When i was following the old tutorial, everything worked up until i tried adding in sessions/logins etc. The database managing, editing tables etc worked fine. So far as i know. Would it be ok to use that code, since it's the old stuff, and then continue fresh from there? Or would the older could inevitably conflict somewhere further on? It's certainly worth looking at if you want to. I would preserve this script you have here by renaming it to something like file.php.bak and then make the other the primary file for the time being and see if you can get that one working. But eventually you're going to want to stick with something. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted October 14, 2008 Share Posted October 14, 2008 if ($_GET['orderby']) { i think is the issue isnt orderby a php keyword? Quote Link to comment Share on other sites More sharing options...
Lucuis Posted October 15, 2008 Author Share Posted October 15, 2008 Yes, the terminal window. This looks like a valid sql statement to me so I am assuming that some of the columns like "id" and "name" and possibly the tablename "info" may be reserved words in mysql so require must be quoted with ``. Ok i did and got this. ERROR 1146 (42S02): Table 'FatalIndustries.info' doesn't exist I know the table FatalIndustries exists. Not sure what the suffix is though ift isn't .info. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.