Errors Undefined variable

Notice : Undefined index: search in C:\xampp\htdocs\h\Untitled-1.php on line 9

Notice : Undefined index: x in C:\xampp\htdocs\h\Untitled-1.php on line 15

Notice : Undefined index: construct in C:\xampp\htdocs\h\Untitled-1.php on line 20

Notice : Undefined variable: foundnum in C:\xampp\htdocs\h\Untitled-1.php on line 35

$search = $_POST ['search'];

$search_exploded = explode (" ", $search);
    
foreach($search_exploded as $search_each)
{
$x = $_POST ['x']; 

$x++;
if($x==1)

$construct= $_POST ['construct'];

        
$construct .="name LIKE '%$search_each%'";

$foundnum = mysql_num_rows($run);
  }  
if ($foundnum==0)

before for underfined variable i used this line of a code

if(!empty($_POST['foundnum'])){
$foundnum= $_POST['foundnumt'];

but this time is not working,i am making pagination for a blog

I’m not sure how you expect anyone to understand your code when you just paste snippets. Which line is line 9, 15, 20 ans 35?

You are also using mysql_* functions which have been removed from the lastest version of PHP. You should be using mysqli_* or better still PDO.

2 Likes

And while you’re at it, print_r($_POST) so that we can actually see what’s IN your $_POST…cause… other than that, the only answer you’re going to be able to receive is ā€œ$_POST did not contain those indexes.ā€

And while you’re at it, print_r($_POST) so that we can actually see what’s IN your $_POST …cause… other than that, the only answer you’re going to be able to receive is ā€œ$_POST did not contain those indexes.ā€

dont understand this explain

Notice : Undefined index: search in C:\xampp\htdocs\h\Untitled-1.php on line 27
$search = $_POST [ā€˜search’];

Notice : Undefined variable: x in C:\xampp\htdocs\h\Untitled-1.php on line 35
$x++;

Notice : Undefined index: construct in C:\xampp\htdocs\h\Untitled-1.php on line 38

$construct= $_POST [ā€˜construct’];

Notice : Undefined variable: foundnum in C:\xampp\htdocs\h\Untitled-1.php on line 53

if ($foundnum==0)

change mysql in pdo

any help i am close to finish a job

Well, no, you’re not close to finishing it if you’re getting this many undefined variable errors.

print_r, as i advised you to do, prints out an array. For debugging purposes, I told you to print out the $_POST array so that you could see what was missing.

All of the errors seem to be coming from $_POST, so, most likely your errors are in the form you are posting to this script.

1 Like

I have typically avoided undefined errors two ways depending on what was being done with the variables.

One: define the variable to some ā€œdefaultā€ - be it zero, an empty string, etc. Then inside the conditional reassign the variables value to something else.

Two: put all of the code that needs the variable inside the conditional.

The problem you are having is because you are assigning a value inside a conditional that may or may not be truthy, resulting in the possibility of the variable not being defined. Then later in the code, outside of the conditional, code wants to use the variable. This may work when the conditional was truthy, but as you can see, if not you will get an undefined error.

i tried to print out array but didnt get nothing out

i fixed errors but cant display images from db

$servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "";

// Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 




 $sql = "SELECT  name, email,comment FROM blog1";
 $result = $conn->query($sql);
 $count=0;
 $images = array("user1.jpg", "user2.jpg", "user3.jpg"); // 


  
  
  if ($result->num_rows > 0) {
  // output data of each row
    while($row = $result->fetch_assoc()) {
    $count++;
	if($count==1){
    $count=0;
    echo '<hr width="700px">';
    } else {
	
	




       


}
	      
     
    

       echo '<img src="data:jpeg;base64,'.base64_encode($row['images']).'" width="25" height="25"      >';

I have a suspicion that your problem actually lies deeper than those errors if the $_POST array was empty but… alright. We’ll move on.

$sql = "SELECT name, email,comment FROM blog1";
while($row = $result->fetch_assoc()) {
echo '<img src="data:jpeg;base64,'.base64_encode($row['images'])...
You did not select a field named ā€˜images’ with your query, so $row does not contain a value for that index.

1 Like

I fixed is working now. Thanks a lot m_hutley , didnt see first i made db with no image row later i added images but i didnt in select.
One more thing

 $sql = "SELECT  name, email,comment,images FROM blog1";
 $result = $conn->query($sql);
 $count=0;
 $images = array("user1.jpg", "user2.jpg", "user3.jpg"); // 


  
  
  if ($result->num_rows > 0) {
  // output data of each row
    while($row = $result->fetch_assoc()) {
    $count++;
	if($count==1){
    $count=0;
    echo '<hr width="700px">';

if i want every second display short size of the hr what i have to do.

remove the $count = 0; line,
add an elseif that checks if the count is 3;
inside that elseif put the $count = 0;
also inside that elseif echo the shorter HR.

i tried but dont work

$count++;
if($count==1){
    echo '<hr width="700px">';
    $count = 0;	
} elseif($count==3){	
    echo'<hr width="500px">';
}

any suggestion

@capljina2 you would make your code so much easier to follow for yourself and others if you only indented it properly.

I have adjusted it this time. In doing so I see there is no space between echo and the following quote mark - whether this makes any difference I don’t know.

what you mean just add on code up if you see mistake

I’m sorry, I don’t understand that. I’m not even sure if this is a statement or a question.

1 Like

you would make your code so much easier to follow for yourself and others if you only indented it properly.

I have adjusted it this time. In doing so I see there is no space between echo and the following quote mark - whether this makes any difference I don’t know.

i dont understand this

Why are the iages in a separate array? They are stored in the database, aren’t they?