Jump to content

Recommended Posts

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');


?>

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.

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..

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..

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.

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

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";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.