PHP - Array Error, Help!
Similar TutorialsMy script IS working, but I can't get around a blank array error when no errors exist. Below is my code, and as you can see, I am being handed this: Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 19 as a result of my ... if (is_array($error)) { ... .. I could do if (@is_array($error)) { (note the @), but I hate using that thing... I've tried several things with no luck, so any ideas welcome at this point. <?php if (isset($_POST['set_test'])) { if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['first_name'])) { $error[] = "Please enter a valid First Name"; } if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['last_name'])) { $error[] = "Please enter a valid Last Name"; } if (is_array($error)) { foreach ($error as $err_message) { echo $err_message . "<br />"; } } } ?> Hi guys this one page is giving me a headache. i posted earlier today with a question on how to insert multiple records and got given a quick demo of how to do it so ive gone away to look more and ive edited my code. however it still doesnt work. im wondering if ive coded a tiny bit wrong. id be really appreciative if you guys could help me out Code: [Select] <?php include 'dbc.php'; page_protect(); company(); $stafflist = mysql_query("SELECT * FROM StaffList WHERE full_name != 'Adam Carter' AND full_name != 'Jakata' AND branch = '$_SESSION[branch]' "); $id = $_POST['id']; if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $user_name = $_POST['user_name']; $Serviceamount = $Serviceamount['item']; $limit = count($id); echo $limit; $values = array(); // initialize an empty array to hold the values for($k=0;$k<$limit;$k++){ $user_name[$k] = check_input($user_name[$k]); $Serviceamount[$k] = check_input($Serviceamount[$k]); $values[$k] = "( '{$id[$k]}', '{$Serviceamount[$k]}')"; // build the array of values for the query string } $query = "INSERT INTO `Services` (Staffname, ServiceAmount) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { echo "$row record added"; } echo $values; } if (checkAdmin()) { ?> <html> <head> <title>Book Off Holiday</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script src="php_calendar/scripts.js" type="text/javascript"></script> <link href="styles.css" rel="stylesheet" type="text/css"> </head> <body> <form name="form" action="Newkpi.php" method="post"> <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main"> <tr> <td colspan="3"> </td> </tr> <td width="160" valign="top"> <?php if (isset($_SESSION['user_id'])) { } ?> <a href="admin.php">Admin CP </a> </td> <td width="732" valign="top"> <p> <h3 class="titlehdr">New KPI</h3> <table width="300px" border="0" align="Centre" cellpadding="2" cellspacing="0"> <tr bgcolor="#000050"> <td width="20px"><h3 class="Text2">Staff ID</h3></td> <td width="20px"><h3 class="Text2">Staff Member</h3></td> <td width="20px"><h3 class="Text2">Service Amount</h3></td> <td width="20px"><h3 class="Text2">Service Date</h3></td> <td width="20px"><h3 class="Text2">Forecast For Next Month</h3></td> <td width="20px"><h3 class="Text2">Product Sales</h3></td> <td width="20px"><h3 class="Text2">Clients This Month</h3></td> <td width="20px"><h3 class="Text2">Personel Retension</h3></td> <td width="20px"><h3 class="Text2">Total Retension</h3></td> <td width="20px"><h3 class="Text2">Colours</h3></td> <td width="20px"><h3 class="Text2">Cuts</h3></td> <td width="20px"><h3 class="Text2">Pre-Booking</h3></td> <td width="20px"><h3 class="Text2">Time Used</h3></td> </tr> <?php while ($rrows = mysql_fetch_array($stafflist)) {?> <tr> <td><h3 class="Text3"><input type="" name="id[]" id="id[]" size="4" value="<?php echo $rrows['id'];?>" /></h3></td> <td><h3 class="Text3"><input type="" name="user_name[]" id="user_name[]" value="<?php echo $rrows['full_name'];?>" /></h3></td> <td><h3 class="Text3"><input name="Serviceamount" type="text" size="4" id="Serviceamount"></h3></td> <td><h3 class="Text3"><input name="servicedate" type="text" size="4" id="servicedate"></h3></td> <td><h3 class="Text3"><input name="forecast" type="text" size="4" id="forecast"></h3></td> <td><h3 class="Text3"><input name="productsales" type="text" size="4" id="productsales"></h3></td> <td><h3 class="Text3"><input name="Clientsthismonth" type="text" size="4" id="Clientsthismonth"></h3></td> <td><h3 class="Text3"><input name="Personelret" type="text" size="4" id="Personelret"></h3></td> <td><h3 class="Text3"><input name="Totalret" type="text" size="4" id="Totalret"></h3></td> <td><h3 class="Text3"><input name="colours" type="text" size="4" id="colours"></h3></td> <td><h3 class="Text3"><input name="cuts" type="text" size="4" id="cuts"></h3></td> <td><h3 class="Text3"><input name="prebooking" type="text" size="4" id="prebooking"></h3></td> <td><h3 class="Text3"><input name="timeused" type="text" size="4" id="timeused"></h3></td> </tr> <?php } ?> </table> <input name="submit" type="submit" id="submit" value="Create"> </table> </form> </body> </html> <?php } ?> thank you in advance I've been thinking of using an assoc_array for validation code in php to have an associative of all the fields (name, email, message) and to see if there has been an "ERROR" declared in any of them? How would I first declare this array? and How would I check to see if they are empty or any of them contain "ERROR". Thanks Im getting the following error on this line: Code: [Select] Fatal error: Cannot use string offset as an array Code: [Select] $array['headers_style'] = 'color:'.$color['headers_text']['text'].'; background-color:'.$color['headers']['background'].';'; This error only occurs on one page, others that use this line dont have a problem. So i tried to delete parts of the code from the page causing the error but it still exists. So i deleted a huge peice of the code and the error vanished. So i eliminated portions of the huge code 1 at a time but the error stays until i delete all of the code. Anyone know why this page doesnt like the error? i could post the code but its a huge chunk(around 600 lines) i keep getting this error when there is no array. Warning: array_values() [function.array-values]: The argument should be an array in /home/learnat2/public_html/cart.php on line 37 but i put this code to prevent the error Code: [Select] $items = $_SESSION['items']; if(is_array($items)) { $quantity = array_count_values($items); $items = array_unique($items); } i still get the error. it should not display an error because $items is not an array, no values have been added. SOLVED database.php
________________________________________________________________________________________
$res = pg_query ($conn, "SELECT artist, composer, genre, title, album, label, price, description FROM music");
echo "<table border='1'>"; while($a = pg_fetch_array($res)){ echo "<td>" . $a['1'] . "</td>"; echo "<td>" . $a['2'] . "</td>"; echo "<td>" . $a['3'] . "</td>"; echo "<td>" . $a['4'] . "</td>"; echo "<td>" . $a['5'] . "</td>"; echo "<td>" . $a['6'] . "</td>"; echo "<td>" . $a['7'] . "</td>"; echo '<td><input type="checkbox" name="music[]" value="$ref"' . $row['0'] . $row['1'] . $row['2'] . $row['3'] . $row['4'] . $row['5'] . $row['6'] . $row['7'] . '"/></td>'; echo"</tr>"; } echo "</table>\n"; ?> _____________________________________________________________________________- shoppingbasket.php _____________________________________________________________________________- <form action="shoppingbasket.php" method="POST"> Send To Basket: <name="music"> <input type="submit" value="submit"> </form> cant get it to send data from database to this page shoppingbasket.php Hello I have the following code Code: [Select] <?php // STORE DATABASE VARIABLES $hostname_cnConnection = "localhost"; $database_cnConnection = "tekou"; $username_cnConnection = "root"; $password_cnConnection = "1234"; $cnConnection = mysql_pconnect($hostname_cnConnection, $username_cnConnection, $password_cnConnection); mysql_set_charset('utf8',$cnConnection); // CONNECT TO DATABASE mysql_select_db($database_cnConnection, $cnConnection); $data = mysql_query("SELECT * from jos_users") or die(mysql_error()); // Print "<table border cellpadding=3>"; while($info = mysql_fetch_array( $data )) { // echo $info['id']; $prod_id=$info['id'] ; // echo "Product ID " .$prod_id; //Show Product Id mysql_query("UPDATE jos_vm_user_info SET first_name = '".$info['name']." WHERE user_id = ".$info['id']) or die(mysql_error()); } echo $info['id']; ?> I want to read every $name and update in jos_vm_user_info the first_name but I think I have error in code because nothing happens. I quess that .$info['id'] doesnt return a single number but all numbers but I dont know how to fix it. Please someone to help me. Hi guys, I am pretty new to php and mysql, so please don't only tell me what to do without showing me. Would be very appreciated. Thanks in advance!! Here is what I am trying to do. I am trying to use php to print the fields a table in Mysql into a form page called index.php. I think I sucessfully did it, but not the best way (mixing php and html, because I couldn't figure it out how to do it seperately) Everything looks good, except when I press submit and now we go to the insert_stats.php where the error: Warning: Invalid argument supplied for foreach() in /insert_stats.php on line 8 insert_stats.php Code: [Select] <? include("sql.php"); $paddler = $_POST['paddler'] ; $practice = $_POST['practice'] ; $header = $_POST['header'] ; [b]foreach ($header as $value) [/b] { $insert="INSERT INTO pushup ($header) VALUES ('$value')"; mysql_query($insert) OR die(mysql_error()) ; } $insert2="INSERT INTO pushup (paddler_id , practice_id) VALUES ('$paddler' , '$practice')"; mysql_query($insert2) OR die(mysql_error()) ; mysql_close(); ?> index.php Code: [Select] <table width="250" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form id="pushup" name="pushup" method="post" action="insert_stats.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3" bgcolor="#E6E6E6"><strong>IDENTIFICATION</strong> </td> </tr> <tr> <td width="80%">Paddler ID</td> <td width="2%">:</td> <td width="18%"><input name="paddler" type="text" id="paddler" size="1" /></td> </tr> <tr> <td>Practice #</td> <td>:</td> <td><input name="practice" type="text" id="practice" size="1" /></td> </tr> <tr> <td colspan="3" bgcolor="#E6E6E6"><strong>ADD FITNESS STATS</strong> </td> </tr> <? include("sql.php"); $result = mysql_query("SELECT * FROM pushup"); $printed_headers = false; while ( $row = mysql_fetch_array($result) ) { if ( !$printed_headers ) { //print the headers once: echo "<tr>"; foreach ( array_keys($row) AS $header ) { if($header == 'paddler_id' || $header == 'practice_id' || $header == 'p_id') continue; if ( !is_int($header) ) { echo "<td>$header</td><td>:</td><td><input name='".$header."' type=\"text\" id='".$header."' size=\"1\" /></td></tr>"; } } $printed_headers = true; } } mysql_free_result($result); mysql_close(); ?> <tr> <td><input type="reset" name="Submit2" value="Reset" /></td> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </table> </td> </form> </tr> </table> I'm getting this error, but I'm not sure how to fix it (on my Wordpress site). Warning: usort() [function.usort]: The argument should be an array in /home2/lushwebs/public_html/4mulaevents/wp-content/themes/dailyedition/includes/talking-points.php on line 20 Warning: Invalid argument supplied for foreach() in /home2/lushwebs/public_html/4mulaevents/wp-content/themes/dailyedition/includes/talking-points.php on line 22 Any help appreciated. Thanks! Hi all,
I have a looping error in my PHP.
I am trying to add use a while statement to gather the users first and last name in to a string, add them to an array called $friendname, then check that the $friendname[] is only output/echoed once.
At the moment the code looks good to me but with multiple rows with the same name associated, the name is echoed the same amount of times as rows, instead of once as it is supposed to be.
Any ideas anyone?
while($row2 = mysqli_fetch_assoc($result2)) { $friendcode = $row2['Code']; $names = "SELECT FirstName, LastName from users WHERE Code = '$friendcode' AND Code != '$crewcode'"; $resultnames = mysqli_query($cxn,$names) or die ("Can't get friends names."); while($rownames = mysqli_fetch_assoc($resultnames)) { $friendname = array(); $newfriendname = ($rownames['FirstName'].' '.$rownames['LastName']); if (!in_array($newfriendname, $friendname)) { array_push($friendname, "$newfriendname"); echo $newfriendname; } } } friends if an array returns results as Array() how could i set an error message to it? i tried to do a if statement to the $variable but not working I am writing a data scraping script but i am getting fatal error.Can someone please help me. My code:- Code: [Select] <?php $con = mysql_connect("localhost","root",""); mysql_select_db("cakeshop", $con); $test = '<form name="select"> <p><font color="#000000" size="3"><select name="city" size="1" onchange="self.location.href=this.options[this.selectedIndex].value"> <option value="xxxwi.htm">Wisconsin</option> <option value="xxxwy.htm">Wyoming</option> </select><br clear="all"> </font><font color="#000000" size="2">Click on the state below where you want to send cake<br> or use the pull down menu above </font> </form> '; preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $test, $out); for($i=0;$i<=2;$i++) { $data = file_get_contents('http://xxx.com/'.$out[1][$i].''); preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $data, $city); //$klimit = sizeof($city); $klimit = count($city[1]); for($k=2 ;$k < $klimit; $k++) { $data = file_get_contents('http://xxx.com/'.$city[1][$k].''); //Error on this line $regex = '~<td\s+colspan="2"\s+width="350"><font\s+size="2">\s+<b>\s+(.*?) <\/b><br>(.*?) <br>(.*?),\s+(.*?)\s+<br>(.*?), (.*?)\s+<BR><BR><font\s+size="2"><img\s+src="\.\.\/images\/phone1.gif"\s+align="left"\s+hspace="4"\s+alt\s+=(.*)>\s+-\s+Phone\s+#\s+(.*?)\s+<\/font>\s+<BR>\s+<font\s+size\s+="1">~'; preg_match_all($regex, $data, $final); $jlimit = count($final[1]); for($j=0 ;$j < $jlimit; $j++) { $name = $final[1][$j]; $name = mysql_real_escape_string($name); $address = $final[2][$j]; $address = mysql_real_escape_string($address); $city = $final[3][$j]; $city = mysql_real_escape_string($city); $state = $final[4][$j]; $state = mysql_real_escape_string($state); $pincode = $final[6][$j]; $pincode = mysql_real_escape_string($pincode); $telephone = $final[8][$j]; $telephone = mysql_real_escape_string($telephone); mysql_query("INSERT INTO cakeshop (name, address, city, state, pincode, telephone) VALUES ('$name', '$address', '$city', '$state', '$pincode', '$telephone')"); } } } mysql_close($con); echo "Data scraping completed"; ?> Error message :- Code: [Select] Fatal error: Cannot use string offset as an array in C:\Users\Booone\AppData\Roaming\NuSphere\PhpED\projects\scraping1.php Error is on this line "$data = file_get_contents('http://xxx.com/'.$city[1][$k].'');" when i run the below code i get an error...
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
but when i take out the h>0 then there is no errors in the code. in table users, i need all columns in the array. How to get h>0 working?
$query = "SELECT * FROM users WHERE h>0 ORDER BY id"; $result = mysqli_query($link, $query); $data = array(); while($row1 = mysqli_fetch_array($result)) { $data[] = $row1; } print_r($data); Edited by kalster, 27 October 2014 - 07:38 PM. Hi Guys, I have 6 file upload fileds in an array. When i try to return the error messages at this link: http://www.php.net/manual/en/features.file-upload.errors.php i get an error displayed for every upload box. I was wondering if it is possible to simply return 1 error instead of the 6? For example rather than getting "No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded" i simply get "No File Uploaded". Also If only some files are uploaded no error should be displayed. The code i am using is: foreach ($_FILES['image']['error'] as $key => $error) { if ($error == UPLOAD_ERR_NO_FILE) { echo "No File Uploaded"; } I have tried using "break;" which works in leaving only 1 error, however the error is still displayed if less than 6 files are uploaded. All help greatly appreciated. Thanks Code: [Select] //Update the ranks if (isset($_POST['update'])){ $ids = implode(",", array_map('intval', $_POST['m'])); $ranks = implode(",", array_map('intval', $_POST['ranks'])); if ($ids < 1) message("Incorrect Data"); if ($ranks < 0) message("Incorrect Data"); foreach ($_POST['ranks'] as $muffins){ $db->query('UPDATE friends set rank = '.$muffins.' WHERE friend_id = '.$_POST['m'].' AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); } redirect("s.php?section=Friends","Thanks, Ranks Updated"); } friend_id = becomes blank? the $muffins work and each rank is showed respectivaly, but how can I join in another array in the loop? I tried another foreach in the loop, but it just made me run double the queries. Any idea? $muffins is working fine and $_POST['m'] is giving no results i need to have 2 dynamic arrays in that forloop, the $ranks and the $ids I have an array ($other_images) that contains image file names separated by semi colons. I can successfully echo the individual values using echo $other_images[0], $other_images[1], $other_images[2]... When I put it in a for loop, though, I run into the followng error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in... Here is my code: <?php $other_images = explode(";", $data['ItemAltImage']); for($i=0; $i<count($other_images); $i++) { echo "<p><img src='http:\\www.oppictures.com\SINGLEIMAGES\100\" . $other_images[$i] . "' /></p>"; } ?> This is the result of print_r($other_images): Array ( => UNV21200_3_2.JPG [1] => UNV21200_2_2.JPG [2] => UNV21200_4_1.JPG [3] => UNV21200_8_1.JPG [4] => UNV21200_5_1.JPG [5] => UNV21200_6_1.JPG [6] => UNV21200_7_1.JPG [7] => UNV21200_9_1.JPG ) Do you see where I'm making a mistake? Thanks I'm getting an error: Undefined offset: 1 in… I'm trying to display a 2D array after filling it. It displays fine if I echo it while it's being made, but after it is not working.
I'm making a pyramid solitaire game and am trying to set up the pyramid of cards. There are 7 arrays, each with a set amount of cards in them. The first array only gets one cards while the last array gets seven. This makes it like a pyramid. All the indexes that don't get a card are set to 0. So it looks like this where the 1's are cards:
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
The display2Darray function is what is causing the error. Specifically this line:
if($array[$i][$j] == 0)Heres my main code: <?php include_once "pyramidCard.php"; include_once "pyramidDeck.php"; $theDeck = new Deck(); $pyramid = array(array(), array(), array(), array(), array(), array(), array()); $size = 7; $theDeck->shuffleCards(); makePyramid($pyramid, $size, $theDeck); display2Darray($pyramid, $size); //************************ // FRUNCTIONS * //************************ function makePyramid($array, $size, $deck) { $row = 0; for($i = 0; $i < $size; $i++) { for($j = 0; $j < $size; $j++) { if($j > $row) { $array[$i][$j] = 0; //echo ". <br>"; } else { $array[$i][$j] = $deck->dealCards(); //echo "this card is ".$array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit()."<br>"; } } $row++; } } function display2Darray($array, $size) { for($i = 0; $i < $size; $i++) { for($j = 0; $j < $size; $j++) { if($array[$i][$j] == 0) echo " "; else echo $array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit(); } echo "<br>"; } } ?> i created an array like this while{ $ir = 0; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['id_sub']; $ir++; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['seminar_topic']; $ir++; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['seminar_mark']; $ir++; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['attendance']; $ir++; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['internal_mark']; $ir++; $stud[$row_mrk['id_sub']][$ir]=$row_mrk['external_mark']; $ir++; } echo "<input type='hidden' name='ar_std' id='ar_std' value='$stud' /> </table>" ; and it is able to print in the same page using foreach ($stud as $v1) { echo "$v1\n<br>"; foreach ($v1 as $v2) { echo "$v2\n<br>"; } } and in the next page $ar_stud[]=$_POST["ar_std"]; I'm getting an error PHP Warning: Invalid argument supplied for foreach() in save.php on line 25 PHP Stack trace: |