Poddy Posted February 9, 2008 Share Posted February 9, 2008 Hi i'm trying to make a webform where the user types info and it stores it inside a mysql database along with pulling the questions from the database.. however each time i put php commands within the file it displays blank on my browser(firefox) i am using apache 2.2 and php 5 here's the code <html> <body> <?php $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'password'; $dbname = 'users'; // process form $conn = mysql_connect($dbhost, $dbuser, $dbpass); or die ('Error connecting to mysql'); mysql_select_db($dbname) or die ('Error selecting database'); ?> <div align="center"><h1>Title</h1></div> <p align="center">please answer these questions</p> <form action="insert.php" method="post" name="questions"> <label> <?php $query = "SELECT * FROM questions WHERE q_id="1""; $result = mysql_query($query); echo ('$result'); ?> </label> <input name="q1" type="text"><br> <label> <?php $query = "SELECT * FROM `questions` WHERE q_id="2""; $result = mysql_query($query); echo ('$result'); ?> </label> <input name="q2" type="text"><br> <label> <?php $query = "SELECT * FROM `questions` WHERE q_id="3""; $result = mysql_query($query); echo ('$query'); ?> </label> <input name="q3" type="text"><br> <label> <?php $query = "SELECT * FROM `questions` WHERE q_id="4""; $result = mysql_query($query); echo ('$result'); ?> </label> <input name="q4" type="text"><br> <br> <input name="submit" type="submit"> </p> </form> <?php mysql_close($conn); ?> </body> </html> and the insert code: <?php if(isset($_POST['submit'])) { $q1 = $_POST['q1']; $q2 = $_POST['q2']; $q3 = $_POST['q3']; $q4 = $_POST['q4']; $query = "INSERT INTO users WHERE user_id="12345" (q1 , q2 , q3 , q4) VALUES ('$q1', '$q2', '$q3', '$q4')"; mysql_query($query) or die('Error, insert query failed'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/ Share on other sites More sharing options...
budimir Posted February 9, 2008 Share Posted February 9, 2008 On your insert.php page put this: echo "Look, now it's not an empty page!"; You are not outputting any data on your insert.php page, neither you are redirecting that page. It's normal that it's blank!!! Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462752 Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2008 Share Posted February 9, 2008 Your first posted code contains at least one fatal parse error - Parse error: syntax error, unexpected T_LOGICAL_OR in ...\yourfile.php on line 12 When learning php, developing php code, or debugging php code, always turn on full php error reporting to get php to help you. Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462762 Share on other sites More sharing options...
Poddy Posted February 9, 2008 Author Share Posted February 9, 2008 thanks for the replies, but you didn't get the point i wanted... i am wondering where has the form gone, as i want to see it so i can submit data while seeing the questions it pulled from my database before the input fields. as for the full error reporting i understand it is the command error_reporting(E_ALL); from your signature, although i did not quite get where to put it, as i put it inside the page and got nothing, also tried to combine it with echo and still nothing.. Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462796 Share on other sites More sharing options...
revraz Posted February 9, 2008 Share Posted February 9, 2008 Sure they got the point you wanted, you just didn't understand the answer. If you have an error in the code, like a Parse Error, the page will be blank. If you turn on error reporting, then you'll see the error. thanks for the replies, but you didn't get the point i wanted... i am wondering where has the form gone, as i want to see it so i can submit data while seeing the questions it pulled from my database before the input fields. as for the full error reporting i understand it is the command error_reporting(E_ALL); from your signature, although i did not quite get where to put it, as i put it inside the page and got nothing, also tried to combine it with echo and still nothing.. Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462805 Share on other sites More sharing options...
AndyB Posted February 9, 2008 Share Posted February 9, 2008 Try viewing the html source code of your form page. That should show you what's happening. <label> <?php $query = "SELECT * FROM questions WHERE q_id="1""; $result = mysql_query($query); echo ('$result'); ?> </label> $result is an array, by the way. Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462827 Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2008 Share Posted February 9, 2008 The error_reporting and display_errors settings can be set in the main php.ini, a .htaccess file (when php is running as an apache module), a local php.ini (when php is running as a cgi wrapper), or in your script. However, fatal parse errors occur before your code is executed, so turning these settings on in your script would be of no help for showing fatal parse errors. As always the manual contains useful information on most things - http://www.php.net/manual/en/ref.errorfunc.php Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462839 Share on other sites More sharing options...
rhodesa Posted February 9, 2008 Share Posted February 9, 2008 Here are my comments on it <html> <body> <?php $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'password'; $dbname = 'users'; // process form ## Removed the semicolon after "$dbpass)" $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname) or die ('Error selecting database'); ?> <div align="center"><h1>Title</h1></div> <p align="center">please answer these questions</p> <form action="insert.php" method="post" name="questions"> <label> <?php ## You need to read up on using strings. You start a string on the next line with ", ## so when it gets to q_id=", it thinks you are ending the string, but you are just ## trying to put a quote in you string. You either need to escaep the quote(\") or ## just use single quotes. $query = "SELECT * FROM questions WHERE q_id='1'"; $result = mysql_query($query); ## More about strings...strings with single quotes won't evaluate variables inside it. ## Only variables in double quotes will be replaced with their values. But, $result is ## is a MySQL Resource. What is the field name you are trying to pull from the table? $row = mysql_fetch_array($result); echo $row['field']; ?> </label> <input name="q1" type="text"><br> <label> <?php ## See above comments $query = "SELECT * FROM `questions` WHERE q_id='2'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo $row['field']; ?> </label> <input name="q2" type="text"><br> <label> <?php ## See above comments $query = "SELECT * FROM `questions` WHERE q_id='3'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo $row['field']; ?> </label> <input name="q3" type="text"><br> <label> <?php ## See above comments $query = "SELECT * FROM `questions` WHERE q_id='4'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo $row['field']; ?> </label> <input name="q4" type="text"><br> <br> <input name="submit" type="submit"> </p> </form> <?php ##PHP will do this automatically ## mysql_close($conn); ?> </body> </html> <?php ## Is this code in the same file or in a seperate one from the form? ## I ask because you don't make a mysql connection. ##print_r($_POST); ## Uncomment this to have the POST variables printed if(isset($_POST['submit'])){ ## Added mysql_real_escape_string to prevent SQL Injection $q1 = mysql_real_escape_string($_POST['q1']); $q2 = mysql_real_escape_string($_POST['q2']); $q3 = mysql_real_escape_string($_POST['q3']); $q4 = mysql_real_escape_string($_POST['q4']); $query = "INSERT INTO users WHERE user_id="12345" (q1 , q2 , q3 , q4) VALUES ('$q1', '$q2', '$q3', '$q4')"; mysql_query($query) or die('Error, insert query failed'); ## You were missing a close brace }else{ echo "NO DATA POSTED"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90259-blank-page-when-adding-php-code-to-a-form/#findComment-462847 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.