VinceGledhill Posted April 20, 2011 Share Posted April 20, 2011 Hi People. I am trying to insert data from a form into my database. Now I have the following code to connect to the DB to update a table so I know that I can connect to the DB ok <?php // this code I got from the new boston, PHP tutorial 25 in selecting a mysql db // opens connection to mysql server $dbc = mysql_connect('localhost', 'VinnyG', 'thepassword'); if (!$dbc) { die("Not Connected:" . mysql_error ()); } // select database $db_selected = mysql_select_db ("sitename",$dbc); if(!$db_selected) { die("can not connect:" . mysql_error ()); } // testing code $query="UPDATE users SET username = 'testing testing' WHERE user_id = '2'"; $result=mysql_query($query); ?> Now here is the code from my form. </head> <body> <?php //include "connection_file.php" //include "config01.php" $username = "username"; $height_above = "height_above"; $mb_diff = "mb_diff"; $alternative = "alternative"; ?> <form name = 'form1' method = 'post' action='config01.php'> <table width="700" border="1" cellspacing="5" cellpadding="5"> <caption> Submit Your Airfield Details </caption> <tr> <td width="100"> </td> <td width="200">Your Name</td> <td width="200"><input type='text' name='username' maxlength='30'></td> <td width="100"> </td> </tr> <tr> <td> </td> <td>Height Above MSL</td> <td><input type='text' name='height_above'maxlength= '30'></td> <td> </td> </tr> <tr> <td> </td> <td>Mb Difference</td> <td><input type='text' name='mb_diff'maxlength='40'></td> <td> </td> </tr> <tr> <td> </td> <td>Alternative Airfield</td> <td><input type='text' name='alternative' maxlength='30'></td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><input type='submit' name='submit' value='post' /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> <?php $sql01 = "INSERT INTO users SET user_id = '', username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); ?> </body> </html> here is the config01.php that the form refers to in the 'action' above. <?php $host = 'localhost'; $username = 'VinnyG'; $password = 'thepassword'; $db_name = 'sitename'; //connect to database mysql_connect ("$host","$username","password")or die ("cannot connect to server"); mysql_select_db ("db_name") or die ("cannot select DB"); ?> Please could someone look at the above code and tell me where I'm going wrong. I can connect to the DB and update using the top script but I can't submit the form for some reason. I get a "cannot connect to server" message. Please someone help. It's been driving me crazy for the past two days. Regards VinceG http://www.microlightforum.com Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/ Share on other sites More sharing options...
mikosiko Posted April 20, 2011 Share Posted April 20, 2011 in //connect to database mysql_connect ("$host","$username","password")or die ("cannot connect to server"); mysql_select_db ("db_name") or die ("cannot select DB"); you $username password is "password" or the content of the variable $password?... same question for "db_name" Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204120 Share on other sites More sharing options...
perky416 Posted April 20, 2011 Share Posted April 20, 2011 Hi Mate, Here... mysql_connect ("$host","$username","password")or die ("cannot connect to server"); You have missed out the $ from the password, that will be why you cant connect. Also where you have your variables, You havent used the posted information. Instead of.... $username = "username"; use... $username = $_POST['username']; Do that for all your form data. Also I think you will have to move the query that you use to write to the database to the config01.php page. Hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204123 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 Thanks for your input guys. I have corrected the errors but still it doesn't move stuff into the database. I thought that it could be the fact that the user_id which is the primary and is auto-incrementing was the problem so I took the info out. Now all I get is everything passed to config01.php and a blank screen. Please someone help before I loose the plot altogether. <body> <?php //include "connection_file.php" //include "config01.php" //$user_id = $_POST[""]; $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; ?> <form name = 'form1' method = 'post' action='config01.php'> <table width="700" border="1" cellspacing="5" cellpadding="5"> <caption> Submit Your Airfield Details </caption> <tr> <td width="100"> </td> <td width="200">Your Name</td> <td width="200"><input type='text' name='username' maxlength='30'></td> <td width="100"> </td> </tr> <tr> <td> </td> <td>Height Above MSL</td> <td><input type='text' name='height_above'maxlength= '30'></td> <td> </td> </tr> <tr> <td> </td> <td>Mb Difference</td> <td><input type='text' name='mb_diff'maxlength='40'></td> <td> </td> </tr> <tr> <td> </td> <td>Alternative Airfield</td> <td><input type='text' name='alternative' maxlength='30'></td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><input type='submit' name='submit' value='post' /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html> Here is the config01.php I move the "post it" stuff out of the form and into this file but still nothing, no errors now though but still nothing in the DB. <?php $host = 'localhost'; $username = 'VinnyG'; $password = 'thepassword'; $db_name = 'sitename'; //connect to database mysql_connect ("$host","$username","$password")or die ("cannot connect to server"); mysql_select_db ("$db_name") or die ("cannot select DB"); $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); ?> If I could see what was going on it would help. But a blank screen is no help whatsoever. Regards VinceG Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204127 Share on other sites More sharing options...
perky416 Posted April 20, 2011 Share Posted April 20, 2011 Instead of... $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); Use... mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); The user_id you mention wouldnt be the problem. With the code i have given you above, you need to make sure you have the total number of form fields inserted. For example if you have 5 fields in your table, the insert should contain 5 values. Try it and let us know. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204132 Share on other sites More sharing options...
perky416 Posted April 20, 2011 Share Posted April 20, 2011 Another thing... Move your variables.... $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; to your config01.php Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204133 Share on other sites More sharing options...
kenrbnsn Posted April 20, 2011 Share Posted April 20, 2011 Why are you commenting out the includes? Ken Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204134 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 Hi Ken. I'm commenting out the includes because with them in the form won't load. Perky416. I actually have 5 fields in the db. How do I handle the auto incrementing user_id (primary field)? (see picture attached) Code now as below... Still get the can't connect message <body> <?php //include "connection_file.php" //include "config01.php" //$user_id = $_POST[""]; //$username = $_POST["username"]; //$height_above = $_POST["height_above"]; //$mb_diff = $_POST["mb_diff"]; //$alternative = $_POST["alternative"]; ?> <form name = 'form1' method = 'post' action='config01.php'> <table width="700" border="1" cellspacing="5" cellpadding="5"> <caption> Submit Your Airfield Details </caption> <tr> <td width="100"> </td> <td width="200">Your Name</td> <td width="200"><input type='text' name='username' maxlength='30'></td> <td width="100"> </td> </tr> <tr> <td> </td> <td>Height Above MSL</td> <td><input type='text' name='height_above'maxlength= '30'></td> <td> </td> </tr> <tr> <td> </td> <td>Mb Difference</td> <td><input type='text' name='mb_diff'maxlength='40'></td> <td> </td> </tr> <tr> <td> </td> <td>Alternative Airfield</td> <td><input type='text' name='alternative' maxlength='30'></td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><input type='submit' name='submit' value='post' /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html> Here is the code for the config01.php <?php $host = 'localhost'; $username = 'VinnyG'; $password = 'thepassword'; $db_name = 'sitename'; $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; //connect to database mysql_connect ("$host","$username","$password")or die ("cannot connect to server for fuck sake this is driving me potty"); mysql_select_db ("$db_name") or die ("cannot select DB"); /* $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); */ mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); ?> Thanks again for your valued input guys. I've been to the local shop for "beer" Regards Vince [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204156 Share on other sites More sharing options...
kenrbnsn Posted April 20, 2011 Share Posted April 20, 2011 If you're commenting out the includes, that's why you get a mysql error. The includes hold your connection statements. Ken Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204162 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 Just uncommented the include "config01.php" and I don't even get the form up. All I get is "can't connect to the server message" if you see my actual code you will see the "actual message" to say it's driving me potty is an understatement. Regards VinceG Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204167 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 ok, the problem with the connection is that you are over writing the contents of $username before you run the connection string. so you now have the form input user name rather than the database connection user name. rather than just having or die("cannot connect to server for fuck sake this is driving me potty"); use or die ('Error During Connect:<br>'.mysql_error()); This will let you see what error the database is returning. Now, change $username = "VinnyG"; to $usr = "VinnyG"; and then update your mysql_connect to be mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); that should get your connection sorted out. you should change all your or die() statements that are attached to the use of mysql functions to report the mysql_error() as well as hold some statement relevent to what the php was trying to do when the error occured. To handle the INSERT INTO you just name the fields that you want to the values inserted into in the order that you are presenting the values to the table. mysql_query("INSERT INTO users (username, height, difference, alternative) VALUES ('bob', 10, 5, 'something different')" Notice that any values that are being insert that are not integers are not wraped in single quotes and that there are spaces after every comma. Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204177 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 Thanks for your input Muddy Funster. I have updated as you said and I now have got rid of the errors and show the form. However, when I fill it in and click the button I get the config01.php page (which is now blank) and no error messages and nothing into the DB. Here's my two files <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php include "config01.php"?> <form name = 'form1' method = 'post' action='config01.php'> <table width="700" border="1" cellspacing="5" cellpadding="5"> <caption> Submit Your Airfield Details </caption> <tr> <td width="100"> </td> <td width="200">Your Name</td> <td width="200"><input type='text' name='username' maxlength='30'></td> <td width="100"> </td> </tr> <tr> <td> </td> <td>Height Above MSL</td> <td><input type='text' name='height_above'maxlength= '30'></td> <td> </td> </tr> <tr> <td> </td> <td>Mb Difference</td> <td><input type='text' name='mb_diff'maxlength='40'></td> <td> </td> </tr> <tr> <td> </td> <td>Alternative Airfield</td> <td><input type='text' name='alternative' maxlength='30'></td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><input type='submit' name='submit' value='post' /></td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html> <?php $host = 'localhost'; $usr = "VinnyG"; $password = 'thepassword'; $db_name = 'sitename'; $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; //connect to database mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); mysql_select_db ("$db_name") or die ("cannot select DB"); /* $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); */ mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204182 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 you still havn't changed the line that runs the insert to what I showed you (or put an or die() statement on there either...) Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204193 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 I've changed the die statements. But sorry, I can't get my head around your stating that I should put "values" in there. Surely the values come from the variables set by the user as he is filling in the form? Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204236 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 I've changed the die statements. and I've changed the insert and the die. I'm still getting blank screen and nothing inserted. Sorry for being THICK. I really am a newbie and it's driving me potty. <?php $host = 'localhost'; $usr = "VinnyG"; $password = 'thepassword'; $db_name = 'sitename'; $username = $_POST["username"]; $height_above = $_POST["height_above"]; $mb_diff = $_POST["mb_diff"]; $alternative = $_POST["alternative"]; //connect to database mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error()); mysql_select_db ("$db_name") or die ('Error Selecting DB:<br>'.mysql_error()); /* $sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative"; $result=mysql_query($sql01); */ //mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); mysql_query("INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"); ?> Regards VinceG Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204253 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 ok - try this: $insert_query = "INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative')"; $insert_action = mysql_query($insert_query) or die ('Error Dring Insert :<br>'.mysql_error().'<br><br>Error occured running the following code :<br>'.$insert_query); that is replace your current mysql_query("INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"); which is missing the closing bracket from the VALUES statement in the SQL... Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204261 Share on other sites More sharing options...
Pikachu2000 Posted April 20, 2011 Share Posted April 20, 2011 Change the last part of that code to the below and see what it gives you. //mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); CHANGES FOLLOW THIS LINE . . $query = "INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"; if( !$result = mysql_query($query) ) { echo "<br>Query: $query<br>Produced error: " . mysql_error() .'<br>'; } else { echo "Query ran and inserted " . mysql_affected_rows() . "row(s)."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204268 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 Muddy Funster. Thanks for your patience with this Thicko. I've done as you said and now it is "sort of" working. I am getting TWO rows inserted into the DB. One is a new user ID and everything else is empty. Then another row with what I put into the form??? Pikachu2000, Brilliant, thanks for your input, it's now Working as it should be. Result people thanks a bunch. I'll be back with more I'm sure. Hope one day to put something back. Like I am doing to my users at www.microlightforum.com and www.microlight.co On behalf of all my forum members, thanks guys. You've been brilliant with this THICKO. Regards VinceG Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204273 Share on other sites More sharing options...
VinceGledhill Posted April 20, 2011 Author Share Posted April 20, 2011 One thing I haven't got though is any feedback. I see from your code there is supposed to be some feedback echo "Query ran and inserted " . mysql_affected_rows() . "row(s)."; I just got a blank page?????? Any ideas why? Because when I've finished this I really would like some feedback to the users. Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204274 Share on other sites More sharing options...
Pikachu2000 Posted April 20, 2011 Share Posted April 20, 2011 If you aren't getting either of the messages, something else is failing. Do you have error_reporting = -1 and display_errors = On in your php.ini file? Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204276 Share on other sites More sharing options...
VinceGledhill Posted April 21, 2011 Author Share Posted April 21, 2011 Thanks for that Pikachu2000. I've run a php_info on my server and it says that configuration file path can be located at /etc/php5/apache2 but when I look into this with filezilla I am told that it doesn't exist? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1204368 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 You'll need to contact the hosting company and see how you need to proceed to alter the php configuration values for your site's directory. SOmetimes it's via .htaccess file, sometimes you can create a local php.ini file. Quote Link to comment https://forums.phpfreaks.com/topic/234284-please-help-trying-to-input-data-into-db-driving-me-crazy/#findComment-1205111 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.