Jump to content

Recommended Posts

Hi Guys,

I've been trying to put everything related to validating the form and updating the database  within:

 if(!empty($_POST['Submit'])) { ... }

My brackets seem to match up, but it is still not updating (although some progress has been made as I am not being "blank rowed" anymore).

Now I am getting a : Undefined variable: updatequery error, on the line where I echo $updatequery. This must mean that my UPDATE statement variable is not even been set.

Can anyone see where I'm going wrong please?

 


<?php

if(!empty($_POST['Submit'])) {
$fname="";
$lname="";
$email1="";
$age1="";
$birthday1="";
$fav_language1="";


if(isset($_POST['fname'])){
  $fname=$_POST['fname'];
}

if(isset($_POST['lname'])){
  $lname=$_POST['lname'];
}

if(isset($_POST['email'])){
  $email1=$_POST['email'];
}

if(isset($_POST['age'])){
  $age1=$_POST['age'];
}


if(isset($_POST['birthday'])){
  $birthday1=$_POST['birthday'];
}

if(isset($_POST['fav_language'])){
  $fav_language1=$_POST['fav_language'];
}


  $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', 
    email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE  FormId = '$FormId'";


     

    $result1 = mysqli_query( $link, $updatequery );


}



echo ("update query is: $updatequery </br>");



 echo ("id is: $FormId </br>");

 ?>

 

This post was not at all helpful for us to help you. 

You are showing us the same old code here.  Why?  Did you not make any improvements?

Also - show how us the echo of the query. 

And stop relying on isset to validate your input.  Did you not read my previous post about how all form elements are always set in the POST array (except checkboxes) so therefore you have to do more than apply the isset function to ensure you actually have some data?

Hi,

Sorry - there is changes to the code, I added in 

if(!empty($_POST['Submit'])) {

at the top and closed the bracket at the end of the code & it matches up. 

I am trying to use echo on the query but that is the undefined variable error I am getting. Do you suggest I use empty instead of isset? 

Ok, ok, here is the complete code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Document </title>
</head>
<body>



<?php
include ("db/connect.php");
include ('includes/error.php');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);




$FormId = isset($_GET['user_id']) ? $_GET['user_id'] : ""; 





$queryselect = "SELECT FormId, FirstName, LastName, Email, Age, Birthdate, FavLanguage FROM Form WHERE FormId = '$FormId'";
$result = mysqli_query($link, $queryselect);


if (!$result) {
    printf("Error in connection: %s\n", mysqli_error($link));
  exit();
}

 $table = [];

while ( $row = mysqli_fetch_assoc( $result ) ) {
   $table[] = $row;  
}



if ( count($table) != 1) {
  exit;
}
else
{
  
  print_r($table);

  $first_name = $table[0]["FirstName"];
  $last_name = $table[0]["LastName"];
  $email = $table[0]["Email"];
  $age = $table[0]["Age"];
  $birthday = $table[0]["Birthdate"];
  $favlanguage = $table[0]["FavLanguage"];

}



if(!empty($_POST['Submit'])) {

$fname="";
$lname="";
$email1="";
$age1="";
$birthday1="";
$fav_language1="";


if(isset($_POST['fname'])){
  $fname=$_POST['fname'];
}

if(isset($_POST['lname'])){
  $lname=$_POST['lname'];
}

if(isset($_POST['email'])){
  $email1=$_POST['email'];
}

if(isset($_POST['age'])){
  $age1=$_POST['age'];
}


if(isset($_POST['birthday'])){
  $birthday1=$_POST['birthday'];
}

if(isset($_POST['fav_language'])){
  $fav_language1=$_POST['fav_language'];
}



  $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', 
    email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE  FormId = '$FormId'";


     echo ("update query after posing is: $updatequery </br>");

    $result1 = mysqli_query( $link, $updatequery );


}



echo ("update query is: $updatequery </br>");



 echo ("id is: $FormId </br>");

  


?>





	
<h2>Update the entry</h2>




    <form name="funform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="<?php echo($first_name); ?>"><span id="errorfname"></span><br>



  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="<?php echo($last_name); ?>"><span id="errorlname"></span><br>



  <label for="email">Email:</label><br>
  <input type="text" id="email" name="email" value="<?php echo($email); ?>"><span id="erroremail"></span>&nbsp;  &nbsp;  <span id="errorpattern"></span><br>


    

  
   <label for="age">Age:</label><br>
  <input type="number" id="age" name="age" value="<?php echo($age); ?>"><span id="errorage"></span><br>


  
   <label for="birthday">Birthday:</label><br>
  <input type="date" id="birthday" name="birthday" value="<?php echo($birthday); ?>"><span id="errorbday"></span><br>




  <p>Choose your favorite Web language:</p>
  


   <input type="radio" id="html" name="fav_language" value="<?php echo($favlanguage); ?>">
  <label for="html" class="radiostyle">HTML</label><br>
  <input type="radio" id="css" name="fav_language" value="<?php echo($favlanguage); ?>">
  <label for="css" class="radiostyle">CSS</label><br>
  <input type="radio" id="javascript" name="fav_language" value="<?php echo($favlanguage); ?>">
  <label for="javascript" class="radiostyle">JavaScript</label><br><br><br>
  <span id="errorlang"></span><br>
  




<?php



mysqli_close($link);

?>

</body>
</html>

 

You haven't done anything to fix the problems.  Despite my suggestions on isset not being the right tool.

Q  If you don't have a get parm for the id, WHY do you continue on with your script?

I know you guys said to "Use $_SERVER['REQUEST_METHOD'] to check if the request was a POST request and only run the query if so." 

But it can also be done using if(!empty($_POST['Submit'])) right?

Well, read up on the best way to validate the input items. Then add some code to send a message to the user when he doesn't provide an id.  And stop putting all those blank lines in your code.   And learn how to read and take to heart what you are being told.  You have posted the same bad code 3 times here.

Since the 'submit' element is simply a button, looking for empty is not what you want to do.  The suggestion to check the requestmethod is the thing to do.  After that (if present) you do the empty check on the REAL input values.  Better yet  check if the item has the proper value according to your app's needs.  If any of the inputs are not valid, output an error message along with the form back.  Only if everything is present and valid do you move onto the query process and update.

BTW since you obviously read the part about using a check for request method, how come you haven't put it into your php code yet?  You are wasting your time and ours

4 minutes ago, webdeveloper123 said:

But it can also be done using if(!empty($_POST['Submit'])) right?

If you have a form element named Submit and it is included in the submitted data and it has a non-empty value, then sure you can do that. 

That's a lot of if's though compared to just use $_SERVER['REQUEST_METHOD'].

42 minutes ago, webdeveloper123 said:

I am trying to use echo on the query but that is the undefined variable error I am getting.

One of your echo statements is outside of your check for if the data is posted.  You only define the variable inside that if statement, so if the if is skipped because the page was not posted to your variable will be undefined.

I did some cleanup and added a lot of comments.  Please read thru it slowly and listen to what I am saying.  This may not be the best method but I hope I am conveying some of the logic that you need to be thinking about.

//*************************************
$errmsg = '';
//  stop using mixed cases.  this is not javascript
$formid = isset($_GET['user_id']) ? $_GET['user_id'] : ""; 
if ($formid == '')
	$errmsg = "Missing user id<br>";  

//  why are you doing this here?
$queryselect = "SELECT FormId, FirstName, LastName, Email, Age, Birthdate, FavLanguage FROM Form 
			WHERE FormId = '$formid'";
//  Don't run the query if you have no user id
$result = mysqli_query($link, $queryselect);
if (!$result) 
{
	//  you should have already checked the connection before you get to this point
	echo "Error in QUERY: <br>", mysqli_error($link);
	exit();
}
//   You are retrieving the results of a query but then you are handling the results of the user's input
//   Does not make sense
$row = mysqli_fetch_assoc($result); 
//  you should not be here if you haven't  verified that the user has subtmitted the form
$fname = (empty($_POST['fname'])) ? $_POST['fname'] : '';
$lname = (empty($_POST['lname'])) ? $_POST['lname'] : '';
$email = (empty($_POST['email'])) ? $_POST['email'] : '';
$age = (empty($_POST['age'])) ? $_POST['age'] : '';
$birthday = (empty($_POST['birthday'])) ? $_POST['birthday'] : '';
$fav_language = (empty($_POST['fav_language'])) ? $_POST['fav_language'] : '';
//  Add some lines of php code to validate your inputs.  Add to the errmsg if necessary using '.=' operator
//
//  if everything is valid now do the update
if (!empty($errmsg))
{
	echo $errmsg;
	exit();
}
$updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', email = '$email1', 
			age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' 
		WHERE  FormId = '$formid'";
echo "update query is: $updatequery </br>";
if ($result1 = mysqli_query($link, $updatequery))
	echo "Update query has been run";
else
	echo "Update query failed to run";
//  Now what?

Hopefully my comments will help you to see what we are seeing and wondering about.

BTW  where do you actually send the input form to the user so that they can submit it to this?  In another script?  And why not use a input value for the user id that you want?

Hey Thanks,

I ran that code but I get:  Undefined index: fname all the way through each field up to and including fav_language

I took your points on board and i've come up with this:



<?php
include ("db/connect.php");
include ('includes/error.php');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);



$FormId = isset($_GET['user_id']) ? $_GET['user_id'] : ""; 

$sqlid = "SELECT * FROM Form WHERE FormId = '$FormId'";



$resultid = mysqli_query($link, $sqlid);

if($resultid->num_rows == 0) {
     echo ("User does not exist");
     exit();
} else {
  
     echo ("Found User!");
}





$queryselect = "SELECT FormId, FirstName, LastName, Email, Age, Birthdate, FavLanguage FROM Form WHERE FormId = '$FormId'";
$result = mysqli_query($link, $queryselect);


if (!$result) {
    printf("Error in connection: %s\n", mysqli_error($link));
  exit();
}

 $table = [];

while ( $row = mysqli_fetch_assoc( $result ) ) {
   $table[] = $row;  
}



if ( count($table) != 1) {
  exit;
}
else
{
  
  print_r($table);
  
   
  $first_name = $table[0]["FirstName"];
  $last_name = $table[0]["LastName"];
  $email = $table[0]["Email"];
  $age = $table[0]["Age"];
  $birthday = $table[0]["Birthdate"];
  $favlanguage = $table[0]["FavLanguage"];

}


if ($_SERVER['REQUEST_METHOD'] == 'POST') {

if(!empty($_POST['fname'])){
  $fname=$_POST['fname'];
}

if(!empty($_POST['lname'])){
  $lname=$_POST['lname'];
}

if(!empty($_POST['email'])){
  $email1=$_POST['email'];
}

if(!empty($_POST['age'])){
  $age1=$_POST['age'];
}


if(!empty($_POST['birthday'])){
  $birthday1=$_POST['birthday'];
}

if(!empty($_POST['fav_language'])){
  $fav_language1=$_POST['fav_language'];
}



  $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', 
    email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE  FormId = '$FormId'";


   

    $result1 = mysqli_query( $link, $updatequery );
    
    echo ("update query after posing is: $updatequery </br>");

}



echo ("update query is: $updatequery </br>");



 echo ("id is: $FormId </br>");

  


?>

But everytime I hit submit, it comes up with User not Found, even though that user exists

49 minutes ago, ginerjm said:

BTW  where do you actually send the input form to the user so that they can submit it to this?  In another script?  And why not use a input value for the user id that you want?

No, I've got the HTML and PHP in one script, i'm using PHP_SELF. The userid gets picked up automatically from the URL using GET

My code is backwards  I should have used !empty.

AS for you code;   once again you posted dozens of blank lines that make it tedious to wade thru. And you are still doing multiple things that don't go together.

I wish you luck.  Signing off this topic.

  • 2 weeks later...
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.