johnc71 Posted June 24, 2008 Share Posted June 24, 2008 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/ Share on other sites More sharing options...
phpSensei Posted June 24, 2008 Share Posted June 24, 2008 you're referring to an array key that doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573026 Share on other sites More sharing options...
teng84 Posted June 24, 2008 Share Posted June 24, 2008 $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 Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573031 Share on other sites More sharing options...
johnc71 Posted June 24, 2008 Author Share Posted June 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573043 Share on other sites More sharing options...
wildteen88 Posted June 24, 2008 Share Posted June 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573428 Share on other sites More sharing options...
johnc71 Posted June 25, 2008 Author Share Posted June 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573949 Share on other sites More sharing options...
sasa Posted June 25, 2008 Share Posted June 25, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-573987 Share on other sites More sharing options...
wildteen88 Posted June 25, 2008 Share Posted June 25, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-574346 Share on other sites More sharing options...
johnc71 Posted June 26, 2008 Author Share Posted June 26, 2008 Thanks everyone! It works fine now. Quote Link to comment https://forums.phpfreaks.com/topic/111630-solved-ampquotundefined-offsetampquot-notices-driving-me-crazy/#findComment-574775 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.