Jump to content

[SOLVED] "Undefined offset" notices driving me crazy


johnc71

Recommended Posts

I've tried everything and can't seem to get rid of the annoying "undefined offset" notices.  The codedoes what it suppose to do but I would like ot know what I am doing wrong here.  I searched for similar posts but can't seem to find the answer for my code. 

 

Thanks in advance for your help!

 

$sql1="SELECT * FROM mytable where active='yes'";
$result1=mysql_query($sql1);
while($rows1=mysql_fetch_array($result1)){
	$count='4';
	$id[]=$rows1['id'];
	for($i=0;$i<$count;$i++){
		$sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
		$result2=mysql_query($sql2);
		}
	}

Link to comment
Share on other sites

$sql1="SELECT * FROM mytable where active='yes'";
$result1=mysql_query($sql1);
while($rows1=mysql_fetch_array($result1)){
	$count='4';
	$id[]=$rows1['id'];
}
if(isset($id) || count($id)>0){
	$sql2="UPDATE mytable SET active='no' WHERE `id` in (".implode(',',$id).")";
	$result2=mysql_query($sql2);

}

 

try that.. and what is the error line? and the exact message if possible

Link to comment
Share on other sites

Your code will set all "active" values to be "no".  :(

 

I edited my original post with more clarifications and exact error I am receiving.  Thanks for your help!

 

Here is the exact code:

 

<?php
$host="myhost"; 
$username="myusername";
$password="mypassword";
$db_name="mydatabase";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql1="SELECT * FROM mytable where active='yes'";
$result1=mysql_query($sql1);
while($rows1=mysql_fetch_array($result1)){
	$count='4';
	$id[]=$rows1['id'];
	for($i=0;$i<$count;$i++){
		$sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
		$result2=mysql_query($sql2);
		}
	}

 

Here is the result:

Notice: Undefined offset: 1 in /mycode.php on line 15

Notice: Undefined offset: 2 in /mycode.php on line 15

Notice: Undefined offset: 3 in /mycode.php on line 15

Notice: Undefined offset: 2 in /mycode.php on line 15

Notice: Undefined offset: 3 in /mycode.php on line 15

Notice: Undefined offset: 3 in /mycode.php on line 15

 

Link to comment
Share on other sites

Could you explain fully what you're trying to do? I find the following code difficult to understand:

		$count='4';
	$id[]=$rows1['id'];
	for($i=0;$i<$count;$i++){
		$sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
		$result2=mysql_query($sql2);

 

Specifically the $id[]=$rows1['id'] line.

 

$rows1['id'] will return a non array value (I presume a number), however you're adding the value of $row['id'] to the $id[] array. Then next your telling PHP to run the code within the for loop 4 times, which iterates through the the $id array. << This happens everytime PHP loops through the while loop.

 

I am not surprised why you're getting the notices. As most of the time keys 2 and 3 will not be set.

Link to comment
Share on other sites

Thanks for your reply. What I am trying to do is, connect to db, update first 4 records where active is "yes" to "no".

 

I am planning to sell pin numbers so lets say I have DB with 100 records. Let's say customer buys 4 pins. I would like to go to DB, retrive first 4 available pins ("Active" = yes) and then set the "Active" field to "No" so that same pins will not be resold.

 

Hope this makes sense.

 

Link to comment
Share on other sites

try

$sql1="SELECT * FROM mytable where active='yes'";
$result1=mysql_query($sql1);
while($rows1=mysql_fetch_array($result1)){
	$id[]=$rows1['id'];
}
$count=4;
for($i=0;$i<$count;$i++){
	$sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
	$result2=mysql_query($sql2);
}

Link to comment
Share on other sites

It would be better if you added LIMIT 4 to the end of your query. This way rather than MySQL having to return all rows which are active, it'll just return 4 results.

 

I'd then use this code:

$sql1 = "SELECT * FROM mytable where active='yes' LIMIT 4";
$result1 = mysql_query($sql1);

while($rows1 = mysql_fetch_array($result1))
{
    $id[] = $rows1['id'];
}

if(is_array($id))
{
    $sql2 = "UPDATE mytable SET active='no' WHERE id IN(" . implode(',', $id) . ')';
    mysql_query($sql2);
}

Link to comment
Share on other sites

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.