storing form data in mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

storing form data in mysql

Post by gwolff2005 »

Hi guys,
I need urgently your help.
I am developing a psychological test. The users are logging onto a site from there is a link to the test page.
The mysql database contains the following columns:

id int(11) No
username var(20) No
password varchar(20) No
firstname varchar(30) No
resultspo char(2) No
resultsm char(2) No
resultspassion char(3) No

The user fills out the form and then presses the button. NOW How can I do the following. As you can see below in the code, the answers of the questions need to be saved to different rows of the current user. In this case the answer of question numer one needs to be ADDED to the already existing value of row resultspo in mysql, question two needs to be added to the value of resultsmo in mysql, as well as number three needs to be added then to resultsmo.
After adding all the values, the page needs to go to the next page which is call passiontest2. Please help. How can I do that???
Thanks!

Code: Select all

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"><?php echo $_SESSION['MM_firstname']; ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="repo1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="remo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>
</body>
</html>
 
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

Firstly the structure of your MySQL table seems a little funny - you should try to avoid storing all the data in one table. I would advise at least two tables in this instance:
table users

id int(11) No
username var(20) No
password varchar(20) No
firstname varchar(30) No

table answers
id int(11) No
resultspo char(2) No
resultsm char(2) No
resultspassion char(3) No

Your form action obviously needs to point to your script...

Code: Select all

 
<?php
 
//retrieve form data
$po_ans = $_POST['po1'];
 
//add data to already existing MySQL columns use
$query = 'UPDATE `answers` SET `restultspo`= `restultspo`+'.$po_ans.' WHERE `restultspo` = `restultspo`';
 
?>
 
This can be used for all of the radio buttons that you have used, simply change the variable name appropriately. I also noticed there seem to be various typo's in your code - lines 59 and 78 don't seem to have the correct names!

Hope this helps!

Phill
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hoi Phill,

thanks for the answer and for mentioning the mistakes :-) However, how an I write in php, that the things should be added to the user whose seesion it is currently. As you can see form my initial post, i have for example his first name, id, etc.. But I have no clue how to tell mysql where to save it.
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

Hi,

I'm not sure if I completely understand what you are asking for now?!
I think you already have a login page that works and that all you want to do is have their answers saved...

To do this you need to check signed in user exists (you seem to be using sessions so the user id would be appropriate and you probably already do this!!) and use the SQL query below.

Code: Select all

 
$query = 'UPDATE `answers` SET `restultspo`= `restultspo`+'.$po_ans.' WHERE `restultspo` = `restultspo` AND `id` = '.$_SESSION['id']';
 
Hope this answers your question!
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

yeah it does :-) i will try it straight and will let you know in a couple of minutes..
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hi Philln,

I am stucked:
This is now my code:

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php 
//You will need to update all the below variables 
$mysqlhost = "localhost"; 
$mysqluser = "guntmar"; 
$mysqlpass = "je20sa05"; 
$mysqldb = "guntmar"; 
$mysqltable = "users"; 
$username = "site username"; 
 
// End Variables 
 
mysql_connect($mysqlhost, $mysqluser, $mysqlpass); 
mysql_select_db($mysqldb); 
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'"; 
 
$result = mysql_query($query) or die("There was a mysql error"); 
while($row = mysql_fetch_array($result)){ 
    $currentpo = $row['resultspo']; 
    $currentmo = $row['resultsmo']; 
} 
 
$pagepo = ($_POST['po1'] + $_POST['po4']); 
$pagemo = ($_POST['mo2'] + $_POST['mo3']); 
 
$newpo = ($currentpo + $pagepo); 
$newmo = ($currentmo + $pagemo); 
 
$query = "UPDATE " . $mysqltable . " SET resultspo = '" . $newpo . "', resultsmo = '" . $newmo . "' WHERE username = '" . $username . "'"
AND `id` = '.$_SESSION['id']'; 
 
mysql_query($query) or die("There was a mysql error"); 
 
header('Location: passiontest2.php');  //Relative or absolute path to your next page. 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"><?php echo $_SESSION['MM_firstname']; ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>
</body>
</html>
but i get the following error

Code: Select all

Parse error: syntax error, unexpected '=' in /home/guntmar/public_html/passiontest.php on line 34
What is wrong??
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

I think you have an extra double quote on that line...


"UPDATE " . $mysqltable . " SET resultspo = '" . $newpo . "', resultsmo = '" . $newmo . "' WHERE username = '" . $username . "'
AND `id` = '.$_SESSION['id']';

Above should now work!
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hi philln I changed that and get now

Code: Select all

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/guntmar/public_html/passiontest.php on line 34

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php 
//You will need to update all the below variables 
$mysqlhost = "localhost"; 
$mysqluser = "guntmar"; 
$mysqlpass = "je20sa05"; 
$mysqldb = "guntmar"; 
$mysqltable = "users"; 
$username = "site username"; 
 
// End Variables 
 
mysql_connect($mysqlhost, $mysqluser, $mysqlpass); 
mysql_select_db($mysqldb); 
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'"; 
 
$result = mysql_query($query) or die("There was a mysql error"); 
while($row = mysql_fetch_array($result)){ 
    $currentpo = $row['resultspo']; 
    $currentmo = $row['resultsmo']; 
} 
 
$pagepo = ($_POST['po1'] + $_POST['po4']); 
$pagemo = ($_POST['mo2'] + $_POST['mo3']); 
 
$newpo = ($currentpo + $pagepo); 
$newmo = ($currentmo + $pagemo); 
 
$query = "UPDATE " . $mysqltable . " SET resultspo = '" . $newpo . "', resultsmo = '" . $newmo . "' WHERE username = '" . $username . "'
AND `id` = '.$_SESSION['id']';
 
mysql_query($query) or die("There was a mysql error"); 
 
header('Location: passiontest2.php');  //Relative or absolute path to your next page. 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"><?php echo $_SESSION['MM_firstname']; ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>
</body>
</html>
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hi Philln, now it works....

Now I get the message there was a mysql error..... But I have everything right: database, etc... what could be the problem?
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

It looks like you need to use something like:

Code: Select all

 
if (isset($_POST['Submit'])) {
INSERT SQL QUERIES HERE
}
As you are going to be submitting empty queries. Also the best way to check is to echo the queries that you are sending. Then try them in phpmyadmin or whichever DBMS you are using.Then it'll let you know what type of error it is and where it is occurring. I assume the initial database connection and user verification works. I have also noticed that you mix the use of single and double quotes when constructing your queries. Double quotes allow the use of variables i.e.

Code: Select all

$location = 'Beach';
echo "I went to the $location";
outputs: I went to the Beach

Code: Select all

echo 'I went to the $location';
outputs: I went to the $location

Just a note - if your script just checks the password and does not need it otherwise do not select it from your database when users login - just return the username or id or both.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hey Philln,

I get the following output on my website:
Parse error: syntax error, unexpected T_STRING in /home/guntmar/public_html/passiontest.php on line 16

What is wrong?? Thanks

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php 
//You will need to update all the below variables 
$mysqlhost = "localhost"; 
$mysqluser = "guntmar"; 
$mysqlpass = "je20sa05"; 
$mysqldb = "guntmar"; 
$mysqltable = "users"; 
$username = "username"; 
 
// End Variables 
if (isset($_POST['Submit'])) {
INSERT SQL QUERIES HERE
};
mysql_connect($mysqlhost, $mysqluser, $mysqlpass); 
mysql_select_db($mysqldb); 
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'"; 
 
$result = mysql_query($query) or die("There was a mysql error"); 
while($row = mysql_fetch_array($result)){ 
    $currentpo = $row['resultspo']; 
    $currentmo = $row['resultsmo']; 
} 
 
$pagepo = ($_POST['po1'] + $_POST['po4']); 
$pagemo = ($_POST['mo2'] + $_POST['mo3']); 
 
$newpo = ($currentpo + $pagepo); 
$newmo = ($currentmo + $pagemo); 
 
$query = "UPDATE ".$mysqltable." SET resultspo = '".$newpo."', resultsmo = '".$newmo."' WHERE username = '".$username."' AND id   '".$_SESSION['id']."'";
 
mysql_query($query) or die("There was a mysql error"); 
 
header('Location: passiontest2.php');  //Relative or absolute path to your next page. 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"><?php echo $_SESSION['MM_firstname']; ?></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>
</body>
</html>
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

Sorry - I think you have become a little confused. The if (isset($_POST['Submit'])) simply checks to ensure the submit button has been pressed - if it has the code within it it then processed.

Personally I would consider restructuring your page as follows...

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php
//You will need to update all the below variables
$mysqlhost = "localhost";
$mysqluser = "guntmar";
$mysqlpass = "je20sa05";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";
 
// End Variables
 
mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb);
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'";
 
$result = mysql_query($query) or die("There was a mysql error");
while($row = mysql_fetch_array($result)){
    $currentpo = $row['resultspo'];
    $currentmo = $row['resultsmo'];
}
 
function showForm () {
echo 
"<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">$_SESSION['MM_firstname']</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>";
}//Close showForm()
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<?php 
if (!isset($_POST['Submit'])) {
showForm();
} else {
$pagepo = ($_POST['po1'] + $_POST['po4']);
$pagemo = ($_POST['mo2'] + $_POST['mo3']);
 
$newpo = ($currentpo + $pagepo);
$newmo = ($currentmo + $pagemo);
 
$query = "UPDATE ".$mysqltable." SET resultspo = '".$newpo."', resultsmo = '".$newmo."' WHERE username = '".$username."' AND id   '".$_SESSION['id']."'";
 
mysql_query($query) or die("There was a mysql error");
//JavaScript page redirect...
echo '<script>location.href="passiontest2.php"</script>'
}// Close if/else
 
?>
</body>
</html>
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: storing form data in mysql

Post by gwolff2005 »

Hi Philln,

thanks so much for your help. I owe you something for that... I really wanna get that done!
I use now the code you wrote and it tells me.
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/guntmar/public_html/passiontest.php on line 29

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php
//You will need to update all the below variables
$mysqlhost = "localhost";
$mysqluser = "guntmar";
$mysqlpass = "je20sa05";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";
 
// End Variables
 
mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb);
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'";
 
$result = mysql_query($query) or die("There was a mysql error");
while($row = mysql_fetch_array($result)){
    $currentpo = $row['resultspo'];
    $currentmo = $row['resultsmo'];
}
 
function showForm () {
echo 
"<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">$_SESSION['MM_firstname']</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>";
}//Close showForm()
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<?php 
if (!isset($_POST['Submit'])) {
showForm();
} else {
$pagepo = ($_POST['po1'] + $_POST['po4']);
$pagemo = ($_POST['mo2'] + $_POST['mo3']);
 
$newpo = ($currentpo + $pagepo);
$newmo = ($currentmo + $pagemo);
 
$query = "UPDATE ".$mysqltable." SET resultspo = '".$newpo."', resultsmo = '".$newmo."' WHERE username = '".$username."' AND id   '".$_SESSION['id']."'";
 
mysql_query($query) or die("There was a mysql error");
//JavaScript page redirect...
echo '<script>location.href="passiontest2.php"</script>'
}// Close if/else
 
?>
</body>
</html>
philln
Forum Newbie
Posts: 12
Joined: Thu Apr 23, 2009 9:22 am

Re: storing form data in mysql

Post by philln »

Ok this is another problem with the quotes that I overlooked...

simply change the double quotes at the start and end of the echo command to single quotes. Then line 42 also needs to read like so:

Code: Select all

<td><div align="right">'.$_SESSION['MM_firstname'].'</div></td>
Hopefully thats it.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

mysql error

Post by gwolff2005 »

Hi Philln,
I rewrote the code a little bit and now it works... However. It says still there was a mysql error...

Code: Select all

<?php require_once('Connections/Login.php'); ?>
<?php
session_start();
?>
<?php
//You will need to update all the below variables
$mysqlhost = "localhost";
$mysqluser = "guntmar";
$mysqlpass = "je20sa05";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";
 
// End Variables
 
mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb);
 
$query = "SELECT * FROM " . $mysqltable . " WHERE username='" . $username . "'";
 
$result = mysql_query($query) or die("There was a mysql error");
while($row = mysql_fetch_array($result)){
    $currentpo = $row['resultspo'];
    $currentmo = $row['resultsmo'];
}
 
function showForm () {
    echo <<<SHOWFORM
<form id="form1" name="form1" method="post" action="">
  <table width="699" border="0" align="center" cellspacing="5" class="style1">
    <tr>
      <td colspan="3"><div align="center"><span class="style4">Your Passion Test</span></div></td>
    </tr>
    <tr>
      <td width="24">&nbsp;</td>
      <td width="429">&nbsp;</td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">$_SESSION[MM_firstname]</div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>1</td>
      <td><strong>I like talking to people. </strong><br />
          </b></td>
      <td><div align="left">
          <input name="po1" type="radio" value="0" />
        0
        <input name="po1" type="radio" value="1" />
        1
        <input name="po1" type="radio" value="2" />
        2
        <input name="po1" type="radio" value="3" />
        3
        <input name="po1" type="radio" value="4" />
        4
        <input name="po1" type="radio" value="5" />
        5</div></td>
    </tr>
    <tr>
      <td>2</td>
      <td>It is important for me what other people think. </td>
      <td><p>
          <label></label>
          <label></label>
          <input name="mo2" type="radio" value="0" />
        0
        <input name="mo2" type="radio" value="1" />
        1
        <input name="mo2" type="radio" value="2" />
        2
        <input name="mo2" type="radio" value="3" />
        3
        <input name="mo2" type="radio" value="4" />
        4
        <input name="mo2" type="radio" value="5" />
        5<br />
        <br />
      </p></td>
    </tr>
    <tr>
      <td>3</td>
      <td class="style1">My level of excitement is high right now. </td>
      <td><p>
          <label></label>
          <input name="mo3" type="radio" value="0" />
        0
        <input name="mo3" type="radio" value="1" />
        1
        <input name="mo3" type="radio" value="2" />
        2
        <input name="mo3" type="radio" value="3" />
        3
        <input name="mo3" type="radio" value="4" />
        4
        <input name="mo3" type="radio" value="5" />
        5<br />
      </p></td>
    </tr>
    <tr>
      <td>4</td>
      <td>I like solving mathematical tasks. </td>
      <td><input name="po4" type="radio" value="0" />
        0
        <input name="po4" type="radio" value="1" />
        1
        <input name="po4" type="radio" value="2" />
        2
        <input name="po4" type="radio" value="3" />
        3
        <input name="po4" type="radio" value="4" />
        4
        <input name="po4" type="radio" value="5" />
        5</td>
    </tr>
  </table>
  <p align="center">
    <input name="Submit" type="submit" class="style1" value="go on" />
  </p>
</form>
SHOWFORM;
}//Close showForm()
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Passion Test</title>
<style type="text/css">
<!--
.style1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #000033;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
    font-size: 20px;
    color: #996600;
}
-->
</style>
</head>
 
<body>
<?php 
if (!isset($_POST['Submit'])) {
showForm();
} else {
$pagepo = ($_POST['po1'] + $_POST['po4']);
$pagemo = ($_POST['mo2'] + $_POST['mo3']);
 
$newpo = ($currentpo + $pagepo);
$newmo = ($currentmo + $pagemo);
} 
$query = "UPDATE ".$mysqltable." SET resultspo = '".$newpo."', resultsmo = '".$newmo."' WHERE username = '".$username."' AND id   '".$_SESSION['id']."'";
 
mysql_query($query) or die("There was a mysql error");
//JavaScript page redirect...
echo '<script>location.href="passiontest2.php"</script>'
// Close if/else
 
?>
</body>
</html>
Post Reply