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> 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. 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 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. 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. 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
Archived
This topic is now archived and is closed to further replies.