Jump to content

Recommended Posts

Hi Guys,

I'm getting: Undefined variable: table and count(): Parameter must be an array or an object that implements Countable on the same line of code. I know I have defined $table as an array but it is still giving me an error. Here is the relevant code: The line of code where I have declared $table is inside the while loop

Thank you


$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();
}

//Fetch the result into an associative array
while ( $row = mysqli_fetch_assoc( $result ) ) {
  $table[] = $row;  //add each row into the table array
}

if ( count($table) != 1 ) {
  exit;
}
else
{
  
  
    //Collect the values from the database
  $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"];

}

 

Something about your assumption is wrong because both messages are definitely indicating that $table is not defined.

You can test it really easily: define $table as an empty array before the loop and see what happens.

And -- why are you moving one array into a new array?  Just do your loop using a fetch on the results instead of doing an unnecessary move.  And also why are you then moving your values from the table array to discrete variable names?  Another waste.  At that point you have your data stored in 3 places - the query resource, the table array and a set of distinct vars.

A standard array variable definition is either:

$table = array();

// or

$table = [];

This is your code:

//Fetch the result into an associative array
while ( $row = mysqli_fetch_assoc( $result ) ) {
  $table[] = $row;  //add each row into the table array
}

You should realize that the problem with the code you wrote, is that it might never enter the while loop, which appears to be the problem you are having.  Probably you are getting an empty result set, so the loop where $table gets an assignment is never entered, and thus $table is undefined prior to trying to pass it to count() after this loop.

You can (and should) make this error go away by simply adding an empty array assignment to $table, prior to the loop. 

Fixed

$table = [];
//Fetch the result into an associative array
while ( $row = mysqli_fetch_assoc( $result ) ) {
  $table[] = $row;  //add each row into the table array
}

 

Hi Thanks,

@gizmola - that got rid of the error message but my update form is still not updating. Every time I submit a "modification" and look at the database after, that record is now blank, even though it had data in it before. So something is happening in the UPDATE statement but not the desired result. 

Sorry the snippet of code I showed did not make it clear I was making an UPDATE form. @ginerjm the storing in the "set of distinct vars" was because I was populating HTML form from SELECT statement (passing variables to value attribute)

I was not getting an empty record set because I printed $table using print_r and It showed me the relevant data from the record I was on.

 

Hi Guys,

Thanks for the help. I have another problem. The UPDATE statement isn't running correctly. I have a hunch the FormId is not being passed properly to the UPDATE statement. If you could have a look please. Sorry for the long code:

<?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"];

}


$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 ("id is: $FormId");

?>

 

A lot of wasted coding here but have you tried echoing out the update query an looked at it to see if it is what you want?

Hint  -  All input tags will always be set in the POST array (except checkboxes and radios(?)) so the use of isset doesn't ensure that your user has provided any required input values.

Hey,

I echoed the update query and this is what it looks like (before posting). It looks ok to me.

UPDATE Form SET FirstName = '', LastName = '', email = '', age = '', Birthdate= '', FavLanguage = '' WHERE FormId = '175'

Sorry i've only been doing php for 3 months

At what point in the above process is the form supposed to be displayed to the user for editing?

You have a SELECT query presumably to get the data to display for editing. You then launch immediatley into updating with the posted data

Hey I did that, but the echo shows up on the submit page and not on the page after being posted. it looks like this:

 UPDATE Form SET FirstName = '', LastName = '', email = '', age = '', Birthdate= '', FavLanguage = '' WHERE FormId = '184'

Could it be POST? Not picking up the HTML fields? Then it updates to a blank record because the HTML inputs are blank?

 

Here is my HTML:

 


	
<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>
  

 

@Barand - So it 2 files, view.php & edit.php, View.php is fine, its just a basic select statement from the Form db table, that shows all the data and then has "Edit" and "Delete" links next to each record. When user clicks "Edit", it takes them to the edit.php, from which the above code is from. 

In that case why is there a SELECT query again?

All you need to do is UPDATE from the post data.

You are posting data but trying to get the id from GET - put the id in a hidden form field and get it from the POST data

I'm using GET because I have something like this from the Edit link from the view page:

https://name.domain.com/updateForm/edit1.php?user_id=155

and the echoed update statement shows that the FormId(P.K) is passing to it correctly

You need to ensure you only run your UPDATE query if the user has submitted the form.  Unless there's something you're not showing it looks like you're currently running it when the edit.php page is first loaded.  At that point, $_POST will be empty so all your variables are empty and you update the record to all empty values.

Use $_SERVER['REQUEST_METHOD'] to check if the request was a POST request and only run the query if so.

The general way of doing what you want is to first display the input form and have a button to either cancel/return from it or to Submit it.  That form should go to your update script which needs to do what Kicken has said  - check if the form has been POSTED to your update script.  If it is not from post then do a header call to go back to the input script.  If it is from the input script you then retrieve and check the input values and only THEN do the db update.

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.