tmharrison Posted February 6, 2007 Share Posted February 6, 2007 My MySQL database is: only 1 table it is called Tours, I have only 2 fields: id_num and url I'm using an outside webhost provider I have a form field on my index.html page that is id_num, with the submit button under it. All I need is for the user to type in a "Tour Number" which is the id_num field on the index.html page, click the submit button and if the database finds this id_num it will then redirect to the specific url that is the other field in the database. (i haven't figured out how to do a pop-up if it isn't there, but that will be my next question, i think it would be ? javascripted?) on to the problem at hand.... I have done this: <?php if (isset($_POST['submitted'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); $id_num = $_POST['id_num']; $get_url = mysql_query("SELECT url FROM Tours WHERE id_num='$id_num'"); $url = mysql_result($get_url, 0); header("Location: $url"); exit(); } ?> and all I ever get is a blank page, that is sitting on the url of my website/the gotoTour.php (php page that is above) but it is just a blank screen? I am not seeing what the problem is? Any suggestions would be much appreciated at this point. Thanks.....Tina Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 6, 2007 Share Posted February 6, 2007 Put some debugging code on the mysql query: <?php $q = "SELECT url FROM Tours WHERE id_num='$id_num'"; $get_url = mysql_query($q) or die("There was a problem with the query:<pre>$q</pre><br>" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; } else exit("No URLs found"); ?> Ken Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 still getting a blank screen maybe it's the way that i am trying to connect to the database...you know the: if (isset($_POST['submitted'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); can i just but the database access information on this php page instead of calling it the way that i am above? i am just worried about safety an all? anyway, maybe it's just because i'm blonde!!!! this is what i made the php look like after you gave me your response, i probably still did something wrong there? who knows. id10t i am sometimes! <?php if (isset($_POST['submitted'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); $q = "SELECT url FROM Tours WHERE id_num='$id_num'"; $get_url = mysql_query($q) or die("There was a problem with the query:<pre>$q</pre><br>" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; } else exit("No URLs found"); } ?> thanks for your help, i need it as you can see! Tina Quote Link to comment Share on other sites More sharing options...
btherl Posted February 6, 2007 Share Posted February 6, 2007 You can try a "brute force" debugging technique. Try commenting out your entire script and see if you get a blank screen. Then try commenting out just one half (the second half). I would also try commenting out the require_once(), and commenting out just the mysql lines. Make sure you don't break the syntax for an if statement with all this commenting Anyway, sooner or later you will find a line or lines which cause the blank screen. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 where in the script are you outputting something to the browser other than if there are "no urls found"? if $_POST['submitted'] isn't set, it will cause a blank screen if there are urls found there will be a blank screen Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 Thanks for trying to help, however, I'm going to have to put it down for the night, it's getting really late. I just wish there was an easy way of just trying to do this one field and check the database for an exact match to that field and then it will go and fetch the url, and when it finds the url, it just goes there with the header() function. I seem to be getting further and further away from were i need to be. Just more confused. I will be watching the posts tomorrow, if anyone has the perfect solution for me. I'm just lost right now, it's hard when your new to this as well. If someone wants to take a stab, i just have 1 database = Tours fields are id_num and url index page has 1 form field on it it is called id_num w/submit button underneath just need to be able to have the user type into the field id_num a number, the database goes and checks to see if the id_num matches anything and if it does it returns the url that is the 2nd field in the database, using the header() function if at all possible. Thanks guys, you are the greatest! Tina Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 crap....i'm sorry genericnumber1 i didn't see your post! I actually have no clue, as to what you are saying, i wish i did, i am just going off of the book that i have been reading which is Larry Ullman's PHP and MySQL, i've been reading it for a week, now, and am trying to make something work that I don't see anywhere in his book, you know the perfect script that will work for me! ha ha I actually made a perfect PHP only script for my registration page, it worked first time out, but now when I try to put this other thing together with MySQL, i'm getting lost on the whole idea of connecting to the database and everything. Do you want to be my guru on this? Maybe not so much as just give me the script, but maybe a better understanding of what things really mean. I have learned a lot, but stupid things are not explained as well in the book. Thanks so much! Tina Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 try this... <?php if (isset($_POST['submitted'])) { echo 'form was submitted <br />'; require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); echo 'require worked <br />'; $q = "SELECT url FROM Tours WHERE id_num='$id_num'"; $get_url = mysql_query($q) or die("There was a problem with the query:$q" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; echo 'script completed! url is ' . $url; } else { exit("No URLs found"); } } else { echo 'the form wasn\'t submitted'; } ?> and just see if it got all the way to "script completed" and shows the correct url Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 man you are quick! i entered what you put in here is what the output was: the form wasn't submitted i guess this is bad? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 It means the script isn't working, but it's easily fixed. Post the form you are using here and we can check it out. as a side note you aren't setting $id_num before using it Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 again another stupid question how do i post my index.html page? all it is is one field? maybe i can just post the following: <form action="gotoTour.php" method="post" name="tours" target="_self"> <span class="graytext"> <input name="id_num" type="text" class="graytext1" id="textfield"></span><br></br> <label for="Enter"></label> <input type="image" src="images/enter.jpg" width=76 height=18 border=1 id="Enter"> </p> </form> I hope this is what you wanted? Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 Ok, the beginning of my reply sounded like I was saying you were asking me a stupid question! I was referring to myself not anyone else. I seem to be asking stupid questions, like how to post things, and such! I'm sorry if I offended anyone! I am just mad at myself for all the questions I have to ask. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 The main problem of your script is the first line, "submitted" on the form isn't set so it isn't executing.. okie dokie, let's do a breakdown of your script for a learning experience <?php // this line says "if 'submitted' on the form is set, do what's between the { }" - your main problem if (isset($_POST['submitted'])) { // Include the files required for your mysql connection require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); // Your second problem is on this next line, $id_num isn't set, $_POST['id_num'] is $q = "SELECT url FROM Tours WHERE id_num='$id_num'"; // Query the database $get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error()); // Make sure there was a result if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; // Another problem, you're not doing anything with $url } else { exit("No URLs found"); } } ?> this would be how you correct your script... <?php // Checking to make sure id_num isn't empty, it will always be set if submitted by the form // so it's better to check to see if it's empty. if (!empty($_POST['id_num'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); // Fix the other problem, use $_POST['id_num'] $q = "SELECT url FROM Tours WHERE id_num='{$_POST['id_num']}'"; $get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; // at least do something with the variable so you know everything went well echo "The url is: $url"; } else { exit("No URLs found"); } } else { // Let the user know if he or she didn't submit something, dont leave him or her in limbo echo 'You didn't submit anything in the form!'; } ?> feel free to ask questions, that's what we're here for. PS. this script is at risk of a sql injection, a security risk if this is going to be used for something other than learning. Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 IT WORKED!!!!! OH MY GOD! the only thing was in your echo 'You didn't submit anything in the form!'; I had to make it look like this: echo 'You didn\'t submit anything in the form!'; something about a parse error? ok, it's kind of working, now how to make it actually go to that url that is in the database with the header() function? you are so sweet! Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 oh yeah and not make it a security risk either? confused on that one. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 Sorry about forgetting to escape my quote. To redirect just replace the line echo "The url is: $url"; with header("location:$url"); -- To make the script secure from SQL injection you would wanna do this... (I did the header change above as well and fixed my quote problem) <?php if (!empty($_POST['id_num'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); // Clean the $_POST['id_num'] to avoid sql injection if(get_magic_quotes_gpc()) { $_POST['id_num'] = stripslashes($_POST['id_num']); } $_POST['id_num'] = mysql_real_escape_string($_POST['id_num']); $q = "SELECT url FROM Tours WHERE id_num='{$_POST['id_num']}'"; $get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; // Redirect the user header("location:$url"); } else { exit("No URLs found"); } } else { echo 'You didn\'t submit anything in the form!'; } ?> Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 YOU ARE THE GREATEST, I AM SO THANKFUL! I haven't done what you have just posted, but I know it will work, you are the best. What would be really cool is: instead of just having the echo "You didn't submit anything in the form" is there a way that javascript can work within php? like it would be cool, if a little alert box came up and said: Not Found - Contact "my email or something" to find out how to get setup you know something like that? it's not what I would actually say or anything, but I think you know what I'm saying. P.S. it's good I can figure some things out, see, I was able to see that parse error! ha ha, actually dw8 saw it, i just went to the line it told me to fix. but at least i knew what it was. Tina Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 oops, something isn't working: says the following: Warning: Cannot modify header information - headers already sent by (output started at /home/virttcom/public_html/VHDdb/mysql_connect.php:14) in /home/virttcom/public_html/gotoTour.php on line 21 seems something on line 21 is being sent prior to the header() function Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 think i can't do this: $get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error()); Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 6, 2007 Share Posted February 6, 2007 you can fix that error with output buffering. simply place ob_start(); at the very top of your page then ob_end_flush(); at the bottom of your page. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 for the javascript you could do something like... <?php if (!empty($_POST['id_num'])) { require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php'); // Clean the $_POST['id_num'] to avoid sql injection if(get_magic_quotes_gpc()) { $_POST['id_num'] = stripslashes($_POST['id_num']); } $_POST['id_num'] = mysql_real_escape_string($_POST['id_num']); $q = "SELECT url FROM Tours WHERE id_num='{$_POST['id_num']}'"; $get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error()); if (mysql_num_rows($get_url) > 0) { $rw = mysql_fetch_assoc($get_url); $url = $rw['url']; // Redirect the user header("location:$url"); } else { echo "<script language=\"javascript\"> alert('Not Found - Contact "my email or something" to find out how to get setup'); </script>"; } } else { echo "<script language=\"javascript\"> alert('You didn\'t submit anything!'); </script>"; } ?> When you ask if javascript can "work within php" the thing is that PHP parses the file and only sends the HTML to the browser. In truth PHP doesn't know or care about javascript, it just cares about sending and parsing and javascript doesn't know or care about PHP, all it cares about is being parsed by the user's browser. As for the header error, make sure there are no line breaks or white space or characters before "<?php" or after "?>" I dont suggest using output buffering, that's just a bandaid covering up the underlying problem. Quote Link to comment Share on other sites More sharing options...
tmharrison Posted February 6, 2007 Author Share Posted February 6, 2007 YOU ARE THE MOST SWEETEST PERSON ON THE PLANET! YOU ARE SO HELPFUL YOU ARE MAKING ME CRY, I AM NOT KIDDING! IT WORKS SO GREAT! I have been reading and reading this darn book, for a week, struggling to figure it out, you get it going with me with little or no information in less than an hour. I am so glad that you were on the site tonight and that you replied! I can't tell you thank you enough. If you need a good tax person, let me know I will repay the favor! I'm a CPA. you have a question all you have to do is ask! you need your taxes done, just ask! I can't wait to get the javascript thingy going as well, I read that in a back chapter about the output buffering and sessions, in the extended topics, but it didn't seem like something i needed? i was wrong! you can always pm me, i guess that's what it is called? I am so happy right know, i am going to sleep good, knowing that you have gotten me on the right track. Thank you again,! I hope to talk with you tomorrow! I have to get some sleep. Sincerely, Tina Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 6, 2007 Share Posted February 6, 2007 Glad you got it working. Quote Link to comment Share on other sites More sharing options...
only one Posted February 6, 2007 Share Posted February 6, 2007 <?php if (isset($_POST['submitted'])) { require_once ('home/virttcom/public_html/VHDdb/mysql_connect.php'); $id_num = $_POST['id_num']; $get_url = mysql_query("SELECT url FROM Tours WHERE id_num='$id_num'"); $url = mysql_result($get_url, 0); header("Location: $url"); exit(); } ?> you dont the the "/" beofre home in your requireonce function... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 6, 2007 Share Posted February 6, 2007 this question has already been solved only one. although you are correct it doesnt really matter. 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.