JamesThePanda Posted January 23, 2010 Share Posted January 23, 2010 Hi Im having trouble with the following bit of code basically I want to ecute the MYSQL query so it inserts the entries in to the database. I assume since Im not doing anything with the $result then the query isnt being excuted. <?php // form information $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $phone = $_POST['phone']; $email = $_POST['email']; // End of Form information // SQL connect $host = "localhost"; $username = "root"; $password = ""; $db_name= "DB"; $tbl_name= "TABLE"; mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database"); mysql_select_db ("$db_name") or die (mysql_error()); //end sql connect // sql query to insert contacts $sql = "INSERT INTO `jvcom`.`contacts` (`id`, `firstname`, `surname`, `email`, `phone`) VALUES (NULL, $firstname, $lastname, $email, $phone)"; $result = mysql_query($sql) or trigger_error(mysql_error()); ?> Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/ Share on other sites More sharing options...
Garethp Posted January 23, 2010 Share Posted January 23, 2010 Wrong. Some queries will give you a feedback, like SELECT, so you can capture that feedback with $result. But the moment you use mysql_query() then the query gets executed Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000299 Share on other sites More sharing options...
JamesThePanda Posted January 23, 2010 Author Share Posted January 23, 2010 so why arent the fields appearing the database? Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000300 Share on other sites More sharing options...
spfoonnewb Posted January 23, 2010 Share Posted January 23, 2010 Does the script output anything at all? $sql = "INSERT INTO `jvcom`.`contacts` (`id`, `firstname`, `surname`, `email`, `phone`) VALUES (NULL, $firstname, $lastname, $email, $phone)"; try putting echo $sql; at the bottom, and make sure your variables are set. Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000301 Share on other sites More sharing options...
JamesThePanda Posted January 23, 2010 Author Share Posted January 23, 2010 Hi I tried echoing $result then Tried echoing $sql still no joy so I took out the back tick on the code. That did nothing Im not geting any error messages Im not sure whats up Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000302 Share on other sites More sharing options...
schilly Posted January 23, 2010 Share Posted January 23, 2010 because you're inserting strings you need to put them in quotes $sql = "INSERT INTO `jvcom`.`contacts` (`id`, `firstname`, `surname`, `email`, `phone`) VALUES (NULL, '$firstname', '$lastname', '$email', '$phone')"; If that is an auto increment id you don't need to insert it either. ie. remove it from the query. hope that works. Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000305 Share on other sites More sharing options...
JamesThePanda Posted January 23, 2010 Author Share Posted January 23, 2010 Still nothing I am using this on local host could that be an issue? Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000383 Share on other sites More sharing options...
bl00dshooter Posted January 23, 2010 Share Posted January 23, 2010 Try doing: mysql_query($sql) or die(mysql_error()); Without the "$result =" and I also THINK that result is a reserved word. Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000424 Share on other sites More sharing options...
JamesThePanda Posted January 23, 2010 Author Share Posted January 23, 2010 Here is what I have at the moment , I had a few people look at it and it still does work <?php // form information $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $phone = $_POST['phone']; $email = $_POST['email']; // End of Form information // SQL connect $host = "localhost"; $username = "root"; $password = ""; $db_name= "db"; $tbl_name= "table"; mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database"); mysql_select_db ("$db_name") or die (mysql_error()); //end sql connect // sql query to insert contacts $sql = "INSERT INTO 'jvcom'.'contacts' ( 'firstname', 'surname', 'email', 'phone') VALUES ( '$firstname', '$lastname', '$email', '$phone')"; mysql_query($sql) or trigger_error(mysql_error()); echo $sql; // End Of MySQL insert into Database ?> Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000452 Share on other sites More sharing options...
TEENFRONT Posted January 23, 2010 Share Posted January 23, 2010 try this. // sql query to insert contacts $sql = "INSERT INTO contacts ('firstname', 'surname', 'email', 'phone') VALUES ('$firstname', '$lastname', '$email', '$phone')"; Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000455 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2010 Share Posted January 23, 2010 Why on earth did you change the back-ticks you originally had around the table and column names into single-quotes? No one suggested to do that. You are probably not getting any error output from the or trigger_error(....) statement because what it does is dependent on the error_reporting/display_errors/log_errors settings. Either turn on full php error reporting/display errors (you should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON anyway to get php to help you) or use the or die(...) statement that bl00dshooter suggested as it will always give you output on an error whereas the or trigger_error() statement won't. Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000457 Share on other sites More sharing options...
cags Posted January 23, 2010 Share Posted January 23, 2010 Debugging of your sql would be a lot better if you use... trigger_error("SQL: $sql, ERROR: ". mysql_error(), E_USER_ERROR); ...rather than what you already have. You will of course require error_reporting(E_ALL); and display_errors(1);. The reason you are getting problems is that in your first piece of code you were using backticks to surround column names (which is ok) but you were also using them for your string values (which is wrong). On the flip side in your second piece of code you are using single quotes for your string values (which is right) but also on your field names (which is wrong). $sql = "INSERT INTO contacts (firstname, surname, email, phone) VALUES ('$firstname', '$lastname', '$email', '$phone')"; Faux Edit: PFMaBiSmAd replied whilst I was posting there, but I figured since I had included different information I should still post.[/i] Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000459 Share on other sites More sharing options...
JamesThePanda Posted January 23, 2010 Author Share Posted January 23, 2010 Hi Here is what I have at the moment. I originally copyied the queries from PhpMyAdmin and that caused a few problem. I replaced trigger error with die also i have tried different variation witht he sigle quote double quote and back ticks. I keep getting the same thing happen again and again just a blank screen. no errors at all, but the values are not appearing in the database I realy puzzeled on this one Thanks James <?php error_reporting(E_ALL); // form information $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $phone = $_POST['phone']; $email = $_POST['email']; // End of Form information // SQL connect $host = "localhost"; $username = "root"; $password = ""; $db_name= "jvcom"; $tbl_name= "contacts"; mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database"); mysql_select_db ("$db_name") or die (mysql_error()); //end sql connect // sql query to insert contacts $sql = "INSERT INTO contacts ('firstname', 'surname', 'email', 'phone') VALUES ( $firstname, $lastname, $email, $phone)"; mysql_query($sql) or die(mysql_error()); echo $sql; // End Of MySQL insert into Database ?> Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000588 Share on other sites More sharing options...
Catfish Posted January 23, 2010 Share Posted January 23, 2010 if youare getting a blank screen yo uare not making it to the echo $sql call which means you have to be failing at some point. checked your actual php error log file? it may tell you something you want to know. Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000591 Share on other sites More sharing options...
Catfish Posted January 23, 2010 Share Posted January 23, 2010 mysql_select_db ("$db_name") or die (mysql_error()); change to: mysql_select_db ($db_name) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1000594 Share on other sites More sharing options...
schilly Posted January 25, 2010 Share Posted January 25, 2010 // sql query to insert contacts $sql = "INSERT INTO contacts ('firstname', 'surname', 'email', 'phone') VALUES ( $firstname, $lastname, $email, $phone)"; your query is still wrong. your string values need to be in quotes. not the field names. // sql query to insert contacts $sql = "INSERT INTO contacts (firstname, surname, email, phone) VALUES ( '$firstname', '$lastname', '$email', '$phone')"; Quote Link to comment https://forums.phpfreaks.com/topic/189507-newb-question-regard-insert-into-database/#findComment-1001358 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.