Trium918 Posted April 10, 2007 Share Posted April 10, 2007 I implement a set of 3 pages. This is in output_fns.php the form that adds the user, and bookmark in a database table called bookmark. function display_add_bm_form() { // display the form for people to ener a new bookmark in ?> <form name=bm_table action="add_bms.php" method=post> <table width=250 cellpadding=2 cellspacing=0 bgcolor=#cccccc> <tr><td>New BM:</td><td><input type=text name=new_url value="http://" size=30 maxlength=255></td></tr> <tr><td colspan=2 align=center><input type=submit value="Add bookmark"></td></tr> </table> </form> <? } // add_bms.php /* This is the file that <form name=bm_table action="add_bms.php" method=post> is submitted to */ <? require_once("bookmark_fns.php"); session_start(); do_html_header("Adding bookmarks"); check_valid_user(); if (!filled_out($_POST)) { echo "You have not filled out the form completely. Please try again."; display_user_menu(); do_html_footer(); exit; } else { // check URL format if (strstr($new_url, "http://")===false) $new_url = "http://".$new_url; // check URL is valid if (@fopen($new_url, "r")) { // try to add bm if (add_bm($new_url)) echo "Bookmark added."; else echo "Could not add bookmark."; } else echo "Not a valid URL."; } // get the bookmarks this user has saved if ($url_array = get_user_urls($valid_user)); display_user_urls($url_array); display_user_menu(); do_html_footer(); ?> /* This is the function that is called from "add_bms.php" that add the user and url to the database. My problem is that the $valid_user isn't being entered in the database. When I do a 'select username from Bookmark' it is blank. The url is ok because it is being entered into the database.*/ function add_bm($new_url) { // Add new bookmark to the database echo "Attempting to add ".htmlspecialchars($new_url)."<BR>"; global $valid_user; if (!($conn = db_connect())) return false; // check not a repeat bookmark $result = mysql_query("select * from bookmark where username='$valid_user' and bm_URL='$new_url'"); if ($result && (mysql_num_rows($result)>0)) return false; // insert the new bookmark if (!mysql_query( "insert into bookmark values ('$valid_user', '$new_url')")) return false; return true; } I was thinking maybe the session_start(); in "add_bms.php" has something to do with it because the $valid_user has no value. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 10, 2007 Share Posted April 10, 2007 session_start() will allow you to store variables in the $_SESSION[] array. But I don't see that array used in your code. Variables outside of $_SESSION will not be kept between script runs. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted April 10, 2007 Author Share Posted April 10, 2007 session_start() will allow you to store variables in the $_SESSION[] array. But I don't see that array used in your code. Variables outside of $_SESSION will not be kept between script runs. That's my point. The first code is a form that's submitting to add_bms.php. In add_bms.php there is a function called add_bm($new_url). Inside that function the username is suppose to be entered into a the database as well as the bm_url, but the username isn't going in. Question, should I use $_Session['valid_user'] in add_bms.php or inside the function. Note that the session is started in "add_bms.php": Quote Link to comment Share on other sites More sharing options...
Trium918 Posted April 10, 2007 Author Share Posted April 10, 2007 I implement a set of 3 pages. This is in output_fns.php the form that adds the user, and bookmark in a database table called bookmark. function display_add_bm_form() { // display the form for people to ener a new bookmark in ?> <form name=bm_table action="add_bms.php" method=post> <table width=250 cellpadding=2 cellspacing=0 bgcolor=#cccccc> <tr><td>New BM:</td><td><input type=text name=new_url value="http://" size=30 maxlength=255></td></tr> <tr><td colspan=2 align=center><input type=submit value="Add bookmark"></td></tr> </table> </form> <? } // add_bms.php /* This is the file that <form name=bm_table action="add_bms.php" method=post> is submitted to */ <? require_once("bookmark_fns.php"); session_start(); do_html_header("Adding bookmarks"); check_valid_user(); if (!filled_out($_POST)) { echo "You have not filled out the form completely. Please try again."; display_user_menu(); do_html_footer(); exit; } else { // check URL format if (strstr($new_url, "http://")===false) $new_url = "http://".$new_url; // check URL is valid if (@fopen($new_url, "r")) { // try to add bm if (add_bm($new_url)) echo "Bookmark added."; else echo "Could not add bookmark."; } else echo "Not a valid URL."; } // get the bookmarks this user has saved if ($url_array = get_user_urls($valid_user)); display_user_urls($url_array); display_user_menu(); do_html_footer(); ?> /* This is the function that is called from "add_bms.php" that add the user and url to the database. My problem is that the $valid_user isn't being entered in the database. When I do a 'select username from Bookmark' it is blank. The url is ok because it is being entered into the database.*/ function add_bm($new_url) { // Add new bookmark to the database echo "Attempting to add ".htmlspecialchars($new_url)."<BR>"; global $valid_user; if (!($conn = db_connect())) return false; // check not a repeat bookmark $result = mysql_query("select * from bookmark where username='$valid_user' and bm_URL='$new_url'"); if ($result && (mysql_num_rows($result)>0)) return false; // insert the new bookmark if (!mysql_query( "insert into bookmark values ('$valid_user', '$new_url')")) return false; return true; } I was thinking maybe the session_start(); in "add_bms.php" has something to do with it because the $valid_user has no value. Quote Link to comment 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.