php form does not save 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

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

php form does not save in mysql

Post by gwolff2005 »

When I change update to insert i get the error message:
There was a mysql error
Query: Insert users SET resultspo = '10', resultsmo = '10' WHERE id = '18'
Error Returned: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = '18'' at line 1

when I leave it with update i get a white page after clicking "submit" but nothing happens.

There will be two more pages with questions, which have to be added on top of the value in the defined rows, that is whuy I chose update and not insert because with insert it would be overwritten, right??
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php form does not save in mysql

Post by Benjamin »

The syntax for insert queries are different and cannot have a WHERE clause.

Code: Select all

 
INSERT INTO users (resultspo, resultsmo) VALUES ('10', '10');
 
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: php form does not save in mysql

Post by gwolff2005 »

Hi astions,
thanks but I need I think then update, because these values are taken out from a test, which varies from user to user....
As I said. When I write update and I do the test it seems to process but nothing is stored in the database. Where is the mistake??

That is the code:

Code: Select all

<?php ini_set("display_errors","2");
ERROR_REPORTING(E_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 = "";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";  
$currentpo = "resultspo";
$currentmo = "resultsmo";
$id = $_SESSION['id']; 
 
// 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 = "Insert ".$mysqltable." SET resultspo = '".$newpo."', resultsmo = '".$newmo."' WHERE 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>
Last edited by Benjamin on Sat Apr 25, 2009 1:00 am, edited 2 times in total.
Reason: Changed code type from text to php, removed password
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php form does not save in mysql

Post by Benjamin »

Line 176 in the code above.

Please be sure to remove your password when posting code.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: php form does not save in mysql

Post by gwolff2005 »

Thanks astions (for removing the password :-)

But I still dont understand: When I write update the page seems to process but nothing is stored in mysql.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php form does not save in mysql

Post by Benjamin »

The MySQL query located on line 176 is not written correctly. I posted the correct syntax above.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: php form does not save in mysql

Post by gwolff2005 »

Hi astions yeah, I got that. The problem is that I need to updat ethe table and not to insert. So I changed insert to update: and the form is being processed but nothing is stored. How can I UPDATE the table and where is the mistake when having this code:

Code: Select all

<?php ini_set("display_errors","2");
ERROR_REPORTING(E_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 = "***";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";  
$currentpo = "resultspo";
$currentmo = "resultsmo";
$id = $_SESSION['id']; 
 
// 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 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>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php form does not save in mysql

Post by Benjamin »

I see, change line 176 to this:

Code: Select all

 
$query = "UPDATE `" . $mysqltable . "` SET resultspo = '" . mysql_real_escape_string($newpo) . "', resultsmo = '" . mysql_real_escape_string($newmo) . "' WHERE id = '" . mysql_real_escape_string($_SESSION['id']) ."'";
 
If that does not work for you, please post the actual query by echoing it, or any other PHP errors that you see. You may need to enable error reporting.
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: php form does not save in mysql

Post by gwolff2005 »

Hi astions Hey hey hey!!!!!!! :drunk:
Now it works Thank you!!! I just have now two tiny questions to you

2. What can I do that after pressing go on it does not show a blank page but goe son to passiontest2.php?

I tried this code but it does not work

Code: Select all

<?php 
if (isset($_POST['submit'])) // if form was submitted 
{ 
?> 
 
<script language="JavaScript"> 
<!-- 
document.location="#signup"; 
//--> 
</script> 
 
<?php 
} 
?>

Thanks!!
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

Re: php form does not save in mysql

Post by eskio »

I do not understand why you want a blank page after you submit the page?
Any way, instead of
document.location="#signup";
you should indicate the file name like

Code: Select all

document.location="mypage.php";
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: php form does not save in mysql

Post by gwolff2005 »

No, you got me wrong. I dont wanna have a blank page. I get at the moment one but I wanna refer to another page. Your suggestion with changing the "sign" with the name does not help. The script has a flaw... I dont know which one and where..

Code: Select all

<?php ini_set("display_errors","2");
ERROR_REPORTING(E_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 = "";
$mysqldb = "guntmar";
$mysqltable = "users";
$username = "username";  
$currentpo = "resultspo";
$currentmo = "resultsmo";
$id = $_SESSION['id']; 
$url = "http://www.guntmarwolff.com/passiontest2.php" ;
 
// 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 = resultspo + '".$newpo."', resultsmo = resultsmo + '".$newmo."' WHERE id = '".$_SESSION['id']."'";
 
 mysql_query($query) or die("There was a mysql error<br />Query: {$query}<br />Error Returned: " . mysql_error());
}
?>
</body>
</html>
Last edited by Benjamin on Sat Apr 25, 2009 12:03 pm, edited 1 time in total.
Reason: Removed password, Changed code type from text to php.
Post Reply