Shockhazard30 Posted February 7, 2010 Share Posted February 7, 2010 I am new to mysql and php but I am trying to build a page that will hold all the links I frequently use. I would like to put a place on it to insert a link but what I have come up with is not working. any one have any ideas? <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> <?php $dbConn = mysql_connect("localhost", "root", "accessdb79"); if (!$dbConn){ print "<h3>problem connecting to database...</h3>\n"; } // end if ?> <form method = "post"> <input type = "text" name = "address" value = ""> <input type = "text" name = "linkdisplay" value = ""> <input type="hidden" name= "task" value= "insert"> <input type = "submit" value = "Add Link"></form> <?php if ($task == "insert"){ $query = "INSERT INTO `main`.`links` (`pkey`, `address`, `linkdisplay`) VALUES('null', '$address', '$linkdisplay');"; mysql_query($query, $dbConn); } // end if ?> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/191282-trying-to-insert-simple-info-into-database/ Share on other sites More sharing options...
gizmola Posted February 7, 2010 Share Posted February 7, 2010 You need to get post variables from the $_POST[] superglobals. Also your query should not have a closing ';'. Please use: [code=php:0] ... [/code] Around future code snippets. Quote Link to comment https://forums.phpfreaks.com/topic/191282-trying-to-insert-simple-info-into-database/#findComment-1008539 Share on other sites More sharing options...
Shockhazard30 Posted February 7, 2010 Author Share Posted February 7, 2010 should it look more like this? if ($task == "insert"){ $query = "INSERT INTO `main`.`links` (`pkey`, `address`, `linkdisplay`) VALUES('null', '$_POST[address]', '$_POST[linkdisplay]')"; mysql_query($query, $dbConn); } // end if I tried this and changed no other code but it still won't insert into the db Quote Link to comment https://forums.phpfreaks.com/topic/191282-trying-to-insert-simple-info-into-database/#findComment-1008559 Share on other sites More sharing options...
gizmola Posted February 7, 2010 Share Posted February 7, 2010 There are 2 issues I see. 1. You need to select a database with mysql_select_db. 2. You should not be passing 'null' which is string = "null" for the first parameter. I'm assuming that's an auto_increment integer key. If so, either pass null without the single quotes, or better yet, omit it entirely from the column list and values. I can't say for sure, but probably: if ($task == "insert"){ $query = "INSERT INTO `main`.`links` (`address`, `linkdisplay`) VALUES ('$_POST[address]', '$_POST[linkdisplay]')"; $result = mysql_query($query, $dbConn); if (!$result) { // Log out mysql_error(); } } // end if The main issue however, is that to catch errors you need to grab the result and if the result is false, check the mysql_error() function to determine what the problem is. Then you don't have to guess. Quote Link to comment https://forums.phpfreaks.com/topic/191282-trying-to-insert-simple-info-into-database/#findComment-1008568 Share on other sites More sharing options...
Shockhazard30 Posted February 7, 2010 Author Share Posted February 7, 2010 Thanks, That fixed it, I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/191282-trying-to-insert-simple-info-into-database/#findComment-1008581 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.