Jump to content

[SOLVED] php form does not save in mysql


gwolff2005

Recommended Posts

Hi guys,

the form I have does not save anything in the mysql table. I have no clue what the reason could be: When I press the go on button it just goes on an empty page but does not save anything. It needs to ADD!!!!! the values to the mysql table and when pressing the button it needs to go to the page passiontest2.php. Please help!!!! Thanks

 

<?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"> </td>
      <td width="429"> </td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td><div align="right">$_SESSION[MM_firstname]</div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td> </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<br />Query: {$query}<br />Error Returned: " . mysql_error());
   //JavaScript page redirect...
// Close if/else


?>
</body>
</html>

Link to comment
Share on other sites

Hi taquitosensei. I did that and got the following error code

 

Notice: Undefined variable: newpo in /home/guntmar/public_html/passiontest.php on line 172

 

Notice: Undefined variable: newmo in /home/guntmar/public_html/passiontest.php on line 172

 

Notice: Undefined index: id in /home/guntmar/public_html/passiontest.php on line 172

 

Can you please give me a hint how to define them, because I thought I already had done it....

Link to comment
Share on other sites

I think i see the problem. When you load the page, you show the form, thus ou dont set the variables. However you still run the query. Try chaning

} 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<br />Query: {$query}<br />Error Returned: " . mysql_error());

 

to this

} 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<br />Query: {$query}<br />Error Returned: " . mysql_error());
}

Link to comment
Share on other sites

Hi miquesta,

 

thanks for the first step. So I dont see it anymore on the form, btu when I press "go on" the error comes up on the next page. I have two problems now:

How and where can I define the two variables nad the index and

2. question: Ho can I tell php that if I pres go on it should go on to page passiontest2.php

 

Thanks so much!!!

Link to comment
Share on other sites

Ok well right now you kind of have your page set up in a pretty bad way. Instead of making a function that shows the form just.. well.. have an html page where the form as. Set the action of that form to the page passion.php, and on that page you can have the code that has all the php from the page you first posted. Now you don't need a page called passion2.php because passion.php does all the stuff for you. So just to help you out here is what your pages should like like (kinda)

 

here is the html page with the form

this can be called form.html

<form id="form1" name="form1" method="post" action="passion.php">
  <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"> </td>
      <td width="429"> </td>
      <td width="220"><div align="right">Saturday, 03/28/09</div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td><div align="right">$_SESSION[MM_firstname]</div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td><div align="right"></div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td> </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>

 

and here is the php page passion.php (Note that in the above form, i set the action to this page. Whenever you make a form, the page with the code that processes the form should be set to the action of the form)

<?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'];
}

$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<br />Query: {$query}<br />Error Returned: " . mysql_error());
   //JavaScript page redirect...
// Close if/else


?>

 

Ok that should help you out a little bit. Hope that helps!

Link to comment
Share on other sites

Hi Mikesta,

 

thanks!!! What I dont understand is the fiollowing when I answer the questions and press go on the following comes up:

 

Notice: Undefined variable: currentpo in /home/guntmar/public_html/passiontest.php on line 169

Notice: Undefined variable: currentmo in /home/guntmar/public_html/passiontest.php on line 170

 

WHY??? I defined the sessions in the login page:

 

  $_SESSION['currentpo'] = $row['resultspo'];

  $_SESSION['currentmo'] = $row['resultsmo'];

Link to comment
Share on other sites

Hi Mikesta,

 

thanks!!! What I dont understand is the fiollowing when I answer the questions and press go on the following comes up:

 

Notice: Undefined variable: currentpo in /home/guntmar/public_html/passiontest.php on line 169

Notice: Undefined variable: currentmo in /home/guntmar/public_html/passiontest.php on line 170

 

WHY??? I defined the sessions in the login page:

 

   $_SESSION['currentpo'] = $row['resultspo'];

   $_SESSION['currentmo'] = $row['resultsmo'];

 

two questions. Did you change your pages to what I gave in my post? Also, the undefined variables probably refers to a variabled named currentpo as opposed to the session named currentpo (which are diffferent by the way)

 

post those specific lines and let me take a look at them

Link to comment
Share on other sites

Hi Mikesta,

 

thanks for being so patient :-)

To your questions. It was my last try to have everything in one php file and second: I defined the variables. After I did that. It does not show me that error anymore, but after Go on it shows a blank page but nothign is saved!

 

//You will need to update all the below variables
$mysqlhost = "localhost";
$mysqluser = "guntmar";
$mysqlpass = "je20sa05";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";
$currentpo = "resultspo";
$currentmo = "resultsmo";

Link to comment
Share on other sites

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.