bundyxc Posted November 22, 2008 Share Posted November 22, 2008 Yeah, I'm a total MySQL newbie, but am trying to run a little PHP/MySQL program on my site. Here's what I have so far. <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $grade = $_POST['grade']; $age = $_POST['age']; $11 = $_POST['11']; $12 = $_POST['12']; $13 = $_POST['13']; $14 = $_POST['14']; $21 = $_POST['21']; $22 = $_POST['22']; $23 = $_POST['23']; $24 = $_POST['24']; $con = mysql_connect("mysql","bundyxc","*****"); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } mysql_select_db("ccf2", $con); $sql = "INSERT INTO 'ccf2' ('firstname' ,'lastname' ,'grade' ,'age' ,'11' ,'12' ,'13' ,'14' ,'21' ,'22' ,'23' ,'24' ) VALUES ('$firstname','$lastname','$grade','$age','$11','$12','$13','$14','$21','$22','$23','$24');"; mysql_query($sql,$con); mysql_close($con); ?> I know that telling it to run off of 'mysql' is strange (and that the norm is localhost), but I know that I'm supposed to use 'mysql' as opposed to localhost. I'm running a forum system off of it, and I know for a fact that it works. My database name is 'ccf2', and my table name is 'ccf2' as well. I'm sure that I'm just making some stupid syntax mistake, but I'm not running into any problems. I also can not get my script to post any errors (even when I do something like change the password to something incorrect). I have this set up at bundyxc.com/cff2 if anybody wants to take a look at it and point me in the right direction. I've even tried to change the MySQL command so that it doesn't use any of the posted variables. I'm not even getting a null entry in my database.. it just isn't entering anything. Can somebody please help me? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/ Share on other sites More sharing options...
revraz Posted November 22, 2008 Share Posted November 22, 2008 Remove the single quotes from 'ccf2' in your insert statement. Remove the single quotes from your field names in your insert statement. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-696462 Share on other sites More sharing options...
Mchl Posted November 22, 2008 Share Posted November 22, 2008 Enable error messages put this error_reporting(E_ALL); ini_set('display_errors','1'); at the beginning of your script. Also change mysql_query($sql,$con); to mysql_query($sql,$con) or die(mysql_error().": ".$sql); to show any MySQL errors encoutered when executing query [added] And remove those quotes revraz posted about. You should use `` not '' around database objects' names (or you can refrain from any quotes all together around them) Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-696465 Share on other sites More sharing options...
bundyxc Posted November 23, 2008 Author Share Posted November 23, 2008 Thanks guys, I can't get it to work though. Should it look like this? <?php error_reporting(E_ALL); ini_set('display_errors','1'); $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $grade = $_POST['grade']; $age = $_POST['age']; $11 = $_POST['11']; $12 = $_POST['12']; $13 = $_POST['13']; $14 = $_POST['14']; $21 = $_POST['21']; $22 = $_POST['22']; $23 = $_POST['23']; $24 = $_POST['24']; $con = mysql_connect("mysql","bundyxc","*****"); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } mysql_select_db("ccf2", $con); $sql = "INSERT INTO ccf2 (firstname, lastname, grade, age, 11, 12, 13, 14, 21, 22, 23, 24) VALUES ('$firstname','$lastname','$grade','$age','$11','$12','$13','$14','$21','$22','$23','$24');"; mysql_query($sql,$con) or die(mysql_error().": ".$sql); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-696597 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 Yes. Are there any errors displayed? Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-696895 Share on other sites More sharing options...
revraz Posted November 23, 2008 Share Posted November 23, 2008 echo $sql and see if all the variables are populated Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697020 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 It's already there, next to mysql_error() If query encountered any problems during execution it should be displayed along with error message. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697023 Share on other sites More sharing options...
revraz Posted November 23, 2008 Share Posted November 23, 2008 Which means it will only echo if there is an error. He should echo it to check his variables for troubleshooting. It's already there, next to mysql_error() If query encountered any problems during execution it should be displayed along with error message. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697167 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 Well I suppose that INSERT query can either insert a row, or report an error. Since row is not inserted, then error should be triggered. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697173 Share on other sites More sharing options...
revraz Posted November 23, 2008 Share Posted November 23, 2008 He said "he can't get it to work", do you have a mind reading device that explains exactly what that means? It can insert a row with blank data if the fields allow that. It doesn't have to throw a mysql_error in order to "not work". You should learn to not assume. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697175 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 I'm not even getting a null entry in my database.. it just isn't entering anything. Or should I? Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697180 Share on other sites More sharing options...
revraz Posted November 23, 2008 Share Posted November 23, 2008 A NULL entry isnt the same thing as no data, which is what he is getting. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697197 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 As I understood this, no rows are inserted. So the INSERT query fails. How do you understand this? Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697204 Share on other sites More sharing options...
bundyxc Posted November 23, 2008 Author Share Posted November 23, 2008 Even if I'm not correctly posting my variables, it should be inserting a null row, or at least that's my understanding of it. But I have no blank rows. Absolutely nothing. I went into my phpmyadmin, and manually inserted a row (to make sure that my database wasn't doing anything funky) and it worked perfectly. Right now my code says this: $con = mysql_connect("mysql","bundyxc","*****"); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } mysql_select_db("ccf2", $con); Shouldn't it look like this? $con = mysql_connect("mysql","bundyxc","*****"); mysql_select_db("ccf2", $con); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } EDIT: I tried changing it.. still no luck. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697251 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 It shouldn't make a difference, and it is more logical as it is now. You have mysql_connect Which tries to estabilish a connection to MySQL server, and if successful, returns a connection handle to $con Then you check if this handle exists, and if not, display an error message. Then you select a database to work with. You could change mysql_select_db("ccf2", $con); $db_selected = mysql_select_db("ccf2", $con); if (!$db_selected) { die ('Can\'t use ccf2 : ' . mysql_error()); } to display error message, if database cannot be selected for any reasons. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697255 Share on other sites More sharing options...
xtopolis Posted November 23, 2008 Share Posted November 23, 2008 Your field names and mysql column names are improper. The first error I received is that $11 is an unexpected T_LNUMBER. Make your <input /> field names more meaningful or at least start with a letter. Meaning, make $11 -> $14 & $21 -> $24 variables prefixed like $p11 , $p12, $p13 .. etc. That solves the first part. The second part is that your MySQL column names are improperly named: regarding the 11-14, 21-24 thing. You had the right idea using quotes before... but you should rename your column names, or at least have them start with a letter. If you wish to keep the column names, enclose the fields in ' or backticks `. But once again, better to make them more meaningfully named for better portability. Fix those syntax and proper naming convention errors and it should work just fine. I don't know how you missed the errors.. it popped up right away on mine. My debug code for reference: <?php error_reporting(E_ALL); ini_set('display_errors','1'); if(isset($_POST['student'])){ $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $grade = $_POST['grade']; $age = $_POST['age']; $p11 = $_POST['11']; $p12 = $_POST['12']; $p13 = $_POST['13']; $p14 = $_POST['14']; $p21 = $_POST['21']; $p22 = $_POST['22']; $p23 = $_POST['23']; $p24 = $_POST['24']; $con = mysql_connect("*****","****","*****"); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } mysql_select_db("******", $con); $sql = "INSERT INTO ccf2 (firstname, lastname, grade, age, `11`, `12`, `13`, `14`, `21`, `22`, `23`, `24`) VALUES ('$firstname','$lastname','$grade','$age','$p11','$p12','$p13','$p14','$p21','$p22','$p23','$p24');"; mysql_query($sql,$con) or die(mysql_error().": ".$sql); mysql_close($con); }else{ ?> <form method="post"> Fn<input type="text" name="firstname" /><br > Ln<input type="text" name="lastname" /><br > Grade<input type="text" name="grade" /><br > Age<input type="text" name="age" /><br > 11<input type="text" name="11" /><br > 12<input type="text" name="12" /><br > 13<input type="text" name="13" /><br > 14<input type="text" name="14" /><br > 21<input type="text" name="21" /><br > 22<input type="text" name="22" /><br > 23<input type="text" name="23" /><br > 24<input type="text" name="24" /><br > <input type="submit" name="student" value="Go" /> </form> <?php } ?> edit: almost left my database credentials lol Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697279 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 Should've seen it sooner... Variables can't start with numbers... Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-697290 Share on other sites More sharing options...
fenway Posted November 25, 2008 Share Posted November 25, 2008 Should've seen it sooner... Variables can't start with numbers... Hope this will be marked solved, or it's in danger of being moved to the more appropriate PHP thread. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-698851 Share on other sites More sharing options...
bundyxc Posted November 26, 2008 Author Share Posted November 26, 2008 Wow. I feel like a total idiot. haha, thank you guys for your help.. but I'm still having problems. It isn't posting any errors, but the script will echo when I add a little: echo "Test test test."; Before it wasn't doing that. Nor would it show any HTML that I put into script.php. So now it looks like the script should be running.. but it isn't inserting anything into the table. ??? <?php error_reporting(E_ALL); ini_set('display_errors','1'); $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $grade = $_POST['grade']; $age = $_POST['age']; $p11 = $_POST['p11']; $p12 = $_POST['p12']; $p13 = $_POST['p13']; $p14 = $_POST['p14']; $p21 = $_POST['p21']; $p22 = $_POST['p22']; $p23 = $_POST['p23']; $p24 = $_POST['p24']; $con = mysql_connect("mysql","********","********"); if (!$con) { die('Error connecting to MySQL database: ' . mysql_error()); } mysql_select_db("ccf2") or die(mysql_error()); $db_selected = mysql_select_db("ccf2", $con); if (!$db_selected) { die ('Can\'t use ccf2 : ' . mysql_error()); } $query = "INSERT INTO ccf2 (firstname, lastname, grade, age, '11', '12', '13', '14', '21', '22', '23', '24') VALUES ('$firstname','$lastname','$grade','$age','$p11','$p12','$p13','$p14','$p21','$p22','$p23','$p24');"; mysql_close($con); ?> This is HTML. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-699177 Share on other sites More sharing options...
bundyxc Posted November 26, 2008 Author Share Posted November 26, 2008 It won't let me double-edit my post. So I guess I'll have to resort to double-posting. SORRY! After looking at my code, I realized that the reason my last code didn't work was becuase of me forgetting to put this in.. mysql_query($sql,$con) or die(mysql_error().": ".$sql); Thank you guys so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-699265 Share on other sites More sharing options...
fenway Posted November 27, 2008 Share Posted November 27, 2008 So is this "solved"? Quote Link to comment https://forums.phpfreaks.com/topic/133821-beginner-phpmysql-help/#findComment-700479 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.