Mr Chris Posted November 29, 2008 Share Posted November 29, 2008 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> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 29, 2008 Share Posted November 29, 2008 Have you tried putting you sql into a variable then echoing it? Always use mysql_error() after queries to see if a error is returned. Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
Flames Posted November 30, 2008 Share Posted November 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 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> Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted November 30, 2008 Author Share Posted November 30, 2008 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? Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 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> Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted November 30, 2008 Author Share Posted November 30, 2008 Thanks, but nope, it's an utterly wierd one. The update query runs, but it just does not post the new value I have selected for the_answer in the database. Just don't get it at all and wracked my brian for hours now!! Thanks for your help though! Quote Link to comment Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 how many fields need to change in the table? is it just the_answer one?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.