webdeveloper123 Posted March 23, 2022 Share Posted March 23, 2022 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"]; } Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/ Share on other sites More sharing options...
Barand Posted March 23, 2022 Share Posted March 23, 2022 If your query returns no results then the while() loop doesn't execute. $table is defined only when the loop does execute. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594485 Share on other sites More sharing options...
webdeveloper123 Posted March 23, 2022 Author Share Posted March 23, 2022 it does return results, there is a while record set of about 200 records Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594486 Share on other sites More sharing options...
webdeveloper123 Posted March 23, 2022 Author Share Posted March 23, 2022 you see the code at the bottom: table[0], i'm using that data, and I know there is data in that because it is populating my form Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594487 Share on other sites More sharing options...
requinix Posted March 23, 2022 Share Posted March 23, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594490 Share on other sites More sharing options...
ginerjm Posted March 23, 2022 Share Posted March 23, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594493 Share on other sites More sharing options...
gizmola Posted March 23, 2022 Share Posted March 23, 2022 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 } Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594498 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594510 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 5 minutes ago, webdeveloper123 said: @ginerjm the storing in the "set of distinct vars" was because I was populating HTML form from SELECT statement Not necessary to do that. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594511 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 Then how would you do it? Build a record set using SELECT and then use foreach? Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594512 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594513 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594519 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594521 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 It doesn't look good to me. There are no values in it. Show us what it will look like when it actually runs. Echo just before calling query function Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594522 Share on other sites More sharing options...
Barand Posted March 24, 2022 Share Posted March 24, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594525 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 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> <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> Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594527 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 @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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594530 Share on other sites More sharing options...
Barand Posted March 24, 2022 Share Posted March 24, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594533 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594537 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 Oh i'm using SELECT again to populate the update form Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594539 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 I tried that hidden input, didn't do it Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594540 Share on other sites More sharing options...
kicken Posted March 24, 2022 Share Posted March 24, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594541 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594543 Share on other sites More sharing options...
webdeveloper123 Posted March 24, 2022 Author Share Posted March 24, 2022 Thanks to both of you. Everyone is saying the same thing about submitting, even on different forums. I'll go off and try that & let you guys know how I got on. Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594544 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 And that's how it's done here!! Quote Link to comment https://forums.phpfreaks.com/topic/314614-undefined-variable-error-but-that-variable-has-been-declared/#findComment-1594545 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.