genesysmedia Posted July 14, 2009 Share Posted July 14, 2009 Can someone please show me the correct syntax for inserting data into a mysql database using superglobals? Thank you in advance. <?php $dbh = mysql_connect("localhost", "{db_user}", "{db_password}") or die ('I cannot connect to the database.'); mysql_select_db("{db_name}"); $insert_data = "INSERT INTO jobs set id='', business_name='$_POST['business_name']'"; mysql_query($insert_data, $dbh); ?> Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/ Share on other sites More sharing options...
Riparian Posted July 14, 2009 Share Posted July 14, 2009 This is how I do it ; // in a config file $HOST = 'YOURHOST'; $USER = "YOURUSERNAME"; $PASS = "YOURPASSWORD"; $DATA = "DATABASENAME"; function connect_data() { global $HOST, $USER, $PASS, $DATA; mysql_connect($HOST, $USER, $PASS) or die('Connection Failed'); mysql_select_db($DATA); } // call the connect function require_once('Location of your config.php file ');connect_data(); $insert_data= "INSERT INTO jobs ( id,business_name )VALUES( '','$_POST[business_name]' ); Try that NOTE SET= is used in Update not insert Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/#findComment-874956 Share on other sites More sharing options...
genesysmedia Posted July 14, 2009 Author Share Posted July 14, 2009 Thank you very much.... Does it require the following statement: mysql_query($insert_data, $dbh); Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/#findComment-874965 Share on other sites More sharing options...
haku Posted July 14, 2009 Share Posted July 14, 2009 Always. Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/#findComment-874966 Share on other sites More sharing options...
Riparian Posted July 14, 2009 Share Posted July 14, 2009 Sorry my mistake I dont use this part (but it is good for complex statements so you can display them on the screen if there is a problem) $insert_data = "INSERT INTO jobs (id,business_name)VALUES('','$_POST[business_name]'"; I put it straight into the mysql_query() mysql_query("INSERT INTO jobs (id,business_name)VALUES('','$_POST[business_name]'") or die ('you can put your own notation comments here'.mysql_error()); Sorry... i forgot to put the mysql_query() but it in earlier Considering that haku (guru) says that you should use mysql_query("$YOURQUERY","$YOURDBH"); perhaps this is better. Cheers Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/#findComment-874973 Share on other sites More sharing options...
genesysmedia Posted July 14, 2009 Author Share Posted July 14, 2009 Thank you guys....it works great... Link to comment https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/#findComment-874976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.