Jump to content

Help with update query?


Mr Chris

Recommended Posts

Hello All,

 

I have created a query whereby a user fills in the answer Yes or No and the query:

 

1) Checks to see all the answers have been filled in

 

2) Inserts the data into the database if the URL string is like so

survey/survey.php?sec=1&user_id=1

 

3) Updates the database if I ask it to update if the URL string is like so:

survey/survey.php?sec=1&user_id=1&update=yes

 

However points 1 and 2 work, but the update query does not work properly. It updates the existing data, but with blank values.  Ot's almost like the update query does not see the new posted values I've commented my code below to make it easier to read. Can anyone see why my update query does not work? It's almost like it does not see my variable values from the commented section 1?

<?php 

// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) 
{ 
$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) 
{ 
$result = mysql_query("Insert into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) values('$question_number','$the_answer','$site_id','$syndication','$user_id')"); 
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$result = mysql_query("Update cfm_site_survey set question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' where user_id=".$_GET['user_id']); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 
<form action="" id="survey" method="post"> 

<input type="radio" name="the_answer" value="Yes" class="radio"> 
<label>Yes</label> 
<div style="clear:left;"></div> 

<input type="radio" name="the_answer" value="No" class="radio"> 
<label>No</label> 
<div style="clear:left;"></div> 

<button type="submit" name="submit" value="submit">Proceed with Questionnaire... »</button> 

</form> 

Link to comment
Share on other sites

Thanks, thats a good idea.

 

But how would I do that, I thought it would be:

 

      $result = mysql_query("Update cfm_site_survey set question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' where user_id=".$_GET['user_id'] or die ('Invalid Query: ' . mysql_error());

 

But that errors out?

 

Thanks

 

Chris

Link to comment
Share on other sites

<?php 
// Set vars global
$question_number = '-1';
$the_answer = '-1';
$user_id = '-1'; 
// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) 
{ 
$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) 
{ 
$result = mysql_query("INSERT into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) VALUES('$question_number','$the_answer','$site_id','$syndication','$user_id')") or die error(mysql_error()); // included error
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$result = mysql_query("UPDATE cfm_site_survey SET question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' WHERE user_id=".$_GET['user_id']) or die error(mysql_error()); // included error

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 
<form action="" id="survey" method="post"> 

<input type="radio" name="the_answer" value="Yes" class="radio"> 
<label>Yes</label> 
<div style="clear:left;"></div> 

<input type="radio" name="the_answer" value="No" class="radio"> 
<label>No</label> 
<div style="clear:left;"></div> 

<button type="submit" name="submit" value="submit">Proceed with Questionnaire... »</button> 

</form> 

 

try that. if the update now changes the fields to -1 you know where the problem is.

Link to comment
Share on other sites

for future reference this is a MySQL related topic


<?php 

// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) 
{ 
$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) 
{ 
$result = mysql_query("Insert into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) values('$question_number','$the_answer','$site_id','$syndication','$user_id')"); 
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$userid = $_GET['user_id'];
$result = mysql_query("Update cfm_site_survey set question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' where user_id='$userid'"; 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 
<form action="" id="survey" method="post"> 

<input type="radio" name="the_answer" value="Yes" class="radio"> 
<label>Yes</label> 
<div style="clear:left;"></div> 

<input type="radio" name="the_answer" value="No" class="radio"> 
<label>No</label> 
<div style="clear:left;"></div> 

<button type="submit" name="submit" value="submit">Proceed with Questionnaire... »</button> 

</form> 

If laPistola's doesnt work try that.

Link to comment
Share on other sites

Thanks Guys,

 

I've tried both and Flames, your code does exactly the same as my code.  Inserts, but when it comes to updating it just adds blank values:

 

LaPistola.  I have tried your's  but I get errors on the query.  Take this line for example:

 


$result = mysql_query("UPDATE cfm_site_survey SET question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' WHERE user_id=".$_GET['user_id']) or die error(mysql_error()); // included error

 

I get the error

Parse error: syntax error, unexpected T_STRING in file.php on line 42.  There seems to be something wrong with the or die mysql error, which I can't work out?

 

Thanks

Link to comment
Share on other sites

Flames,

 

I've tried adding manual values to my UPDATE query ie:

 

<?php 

// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) 
{ 
$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) 
{ 
$result = mysql_query("Insert into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) values('$question_number','$the_answer','$site_id','$syndication','$user_id')"); 
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$userid = $_GET['user_id'];
$result = mysql_query("Update cfm_site_survey set question_number='1',the_answer='No',site_id='12',syndication='$syndication',user_id='$user_id' where user_id='$userid'"); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 

 

ie

Update cfm_site_survey set question_number='1',the_answer='No',site_id='12'

 

And it updates with the Manual Values, so i'm confused as to why it does not see the changes values I make via the form when I edit the data?

 

Thanks

Link to comment
Share on other sites

my bad try

 

<?php 
// Set vars global
$question_number = '-1';
$the_answer = '-1';
$user_id = '-1'; 
// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) 
{ 
$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) 
{ 
$result = mysql_query("INSERT into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) VALUES('$question_number','$the_answer','$site_id','$syndication','$user_id')") or die(mysql_error()); // included error
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$result = mysql_query("UPDATE cfm_site_survey SET question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' WHERE user_id='".$_GET['user_id']."'") or die(mysql_error()); // included error

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 
<form action="" id="survey" method="post"> 

<input type="radio" name="the_answer" value="Yes" class="radio"> 
<label>Yes</label> 
<div style="clear:left;"></div> 

<input type="radio" name="the_answer" value="No" class="radio"> 
<label>No</label> 
<div style="clear:left;"></div> 

<button type="submit" name="submit" value="submit">Proceed with Questionnaire... »</button> 

</form> 

Link to comment
Share on other sites

Thanks laPistola,

 

That works a treat, for both update and insert, but now my update query sets the answer to -1.  I've tried doing it like so :

 

<?php 
// Set vars global
$the_answer = $_POST['the_answer'];
// Instead of $the_answer = '-1'

 

But that does not post anything to the database.  But I just can't get my head round it, Why is the value of the_answer I check on my form not being seen in my UPDATE query?

Link to comment
Share on other sites

try

 

<?php 
// Set vars global

$question_number = $_GET["sec"]; 
$the_answer = $_POST['the_answer']; 
// $site_id = Global Variable 
// $syndication = Global Variable 
$user_id = $_GET["user_id"]; 

// 1. If submit is pressed Post/GET the values for all the variables 
if(isset($_POST['submit'])) { 

// 2. Create a $flag value to check for any form elements not filled in for error checking 
if (empty($the_answer)){ 
$error = "<div class='msg_box'><img src='../survey/images/error.jpg' class='align'> <span class='color:#CC0000;font-weight:bold;'>You Must specify an answer!</span></div>"; 
$flag=1; 
} 

// 3. If there is no answer NOT filled in ($flag=1) and the URL string is NOT update=yes, insert into the database and pick a location to go to 
if($flag != 1 && ($_GET['update'] != 'yes')) { 
$result = mysql_query("INSERT into cfm_site_survey(question_number,the_answer,site_id,syndication,user_id) VALUES('$question_number','$the_answer','$site_id','$syndication','$user_id')") or die(mysql_error()); // included error
$survey_id=mysql_insert_id(); 

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 
if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} 

// 3. OTHERWISE it is an update then UPDATE the table and go to the relevant location 
} else { 
$result = mysql_query("UPDATE cfm_site_survey SET question_number='$question_number',the_answer='$the_answer',site_id='$site_id',syndication='$syndication',user_id='$user_id' WHERE user_id='".$_GET['user_id']."'") or die(mysql_error()); // included error

if($the_answer == 'Yes'){ 
header("Location:survey.php?sec=2.1&user_id=".$user_id.""); 
exit(); 
} 

if($the_answer == 'No'){ 
header("Location:survey.php?sec=2.2&user_id=".$user_id.""); 
exit(); 
} 

} //End of if(isset($_POST['submit'])) 

?> 
<form action="" id="survey" method="post"> 

<input type="radio" name="the_answer" value="Yes" class="radio"> 
<label>Yes</label> 
<div style="clear:left;"></div> 

<input type="radio" name="the_answer" value="No" class="radio"> 
<label>No</label> 
<div style="clear:left;"></div> 

<button type="submit" name="submit" value="submit">Proceed with Questionnaire... »</button> 

</form> 

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.