n000bie Posted November 8, 2009 Share Posted November 8, 2009 Hello i got this code to fetch data from database but it is not working it displays 7 Array Array Array Array Array Array if (isset($_POST['showrecord'])) { $query='SELECT * FROM artus_test'; $result=mysql_query($query); $numrows=mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ $mypdata[] = $row['test_id']; $mypdata[] = unserialize($row['organ']); $mypdata[] = unserialize($row['design']); $mypdata[] = unserialize($row['start_month']); $mypdata[] = unserialize($row['start_year']); $mypdata[] = unserialize($row['end_month']); $mypdata[] = unserialize($row['end_year']); foreach ($mypdata as $value) { echo $value."<br/>"; } } } help me please Quote Link to comment Share on other sites More sharing options...
bob2588 Posted November 8, 2009 Share Posted November 8, 2009 can i see the page the form is on Quote Link to comment Share on other sites More sharing options...
n000bie Posted November 8, 2009 Author Share Posted November 8, 2009 this is all the code i got <?php ////////////////////////////function defination/////////////////////// function showMonth() { $arr=array("January","February","March","April","May","June","July","August","September","October","November","December"); for($i=0;$i<count($arr);$i++) { echo "<option value='".$arr[$i]."'>".$arr[$i]."</option>"; } } function showYear() { $d=getdate(); $curYear=$d['year']; for($i=$curYear;$i>($curYear-50);$i--) { echo "<option value='".$i."'>".$i."</option>"; } } // connect to database define("HOST","localhost"); define("USER","root"); define("PASS",""); define("DATABASE","artuscom"); $con = mysql_connect(HOST,USER,PASS); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db(DATABASE, $con); // ends here // start post if (isset($_POST['submit'])) { $org = serialize($_POST['curEmpName']); $des = serialize($_POST['curEmpDesignation']); $start_month = serialize($_POST['curEmpStartMonth']); $start_year = serialize($_POST['curEmpStartYear']); $end_month = serialize($_POST['curEmpEndMonth']); $end_year = serialize($_POST['curEmpEndYear']); $query="Insert into artus_test values('','$org','$des','$start_month','$start_year','$end_month','$end_year')"; $result=mysql_query($query); if ($result) { echo 'success'; } else { echo 'something failed...';} } // show records if (isset($_POST['showrecord'])) { $query='SELECT * FROM artus_test'; $result=mysql_query($query); $numrows=mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ $mypdata[] = $row['test_id']; $mypdata[] = unserialize($row['organ']); $mypdata[] = unserialize($row['design']); $mypdata[] = unserialize($row['start_month']); $mypdata[] = unserialize($row['start_year']); $mypdata[] = unserialize($row['end_month']); $mypdata[] = unserialize($row['end_year']); foreach ($mypdata as $value) { foreach ($value as $key) { echo $key."<br/>"; } } //echo "ID: ".$row['test_id'].", Organization:".unserialize($row['organ']).", Designation:".unserialize($row['design']).", Start-month:".unserialize($row['start_month']).", Start-year:".unserialize($row['start_year']).", End-month:".unserialize($row['end_month']).", End-year:".unserialize($row['end_year'])."<br/>"; //$arr = unserialize($row['organ']); } } // if (isset($_POST['showpostvalues'])) { foreach ($_POST['curEmpName'] as $value) { // Do something with each valid friend entry ... if ($value) { echo $value."<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form name="form1" method="post" action=""> <div style="border:2px solid red; margin-bottom:20px;" id="copyme"> <td height="50"><em>* </em>Organization Name<br /></td> <td colspan="2"> <input type="text" maxlength="64" size="35" name="curEmpName[]" class="required" title="Please enter organization name" value=""/> </td> <td> </td> </tr> <tr> <td height="50"><em>* </em>Designation</td> <td colspan="2"><input type="text" maxlength="64" size="35" name="curEmpDesignation[]" class="required" title="Please enter your designation" value=""/></td> <td> </td> </tr> <tr> <td height="50"><em>* </em>Time Period: <br /> </td> <td ><select name="curEmpStartMonth[]" class="validate-selection" title="Please select your Time period"> <option selected="selected" value="-1">Month </option> <?php showMonth();?> </select></td> <td width="75"><select name="curEmpStartYear[]" class="validate-selection" > <option selected="selected" value="-1">Year </option> <?php showYear();?> </select></td> <td>to <select name="curEmpEndMonth[]" > <?php $d=getdate();?> <option selected="selected" value="<?php echo $d['month'];?>"><?php echo $d['month'];?> </option> </select> <select name="curEmpEndYear[]" > <option selected="selected" value="<?php echo $d['year'];?>"><?php echo $d['year'];?> </option> </select></td> </div> <div id="copyhere"></div> <a href="javascript: copyTrue();">Add more</a> <input name="submit" type="submit" value="submit" /> <input name="showpostvalues" type="submit" value="show post values" /> <input name="showrecord" type="submit" value="Show Record" /> </form> </body> </html> <script> function copyTrue(){ var t = document.getElementById('copyme').cloneNode(true); document.getElementById('copyhere').appendChild(t); } </script> Quote Link to comment Share on other sites More sharing options...
bob2588 Posted November 8, 2009 Share Posted November 8, 2009 i belive you problem is coming form the mysql_num_rows Function try this if (isset($_POST['showrecord'])) { $query='SELECT * FROM artus_test'; $result=mysql_query($query); $numrows=mysql_fetch_array($result); while($row = mysql_fetch_assoc($result)){ $mypdata[] = $row['test_id']; $mypdata[] = unserialize($row['organ']); $mypdata[] = unserialize($row['design']); $mypdata[] = unserialize($row['start_month']); $mypdata[] = unserialize($row['start_year']); $mypdata[] = unserialize($row['end_month']); $mypdata[] = unserialize($row['end_year']); foreach ($mypdata as $value) { echo $value."<br/>"; } } } Quote Link to comment Share on other sites More sharing options...
n000bie Posted November 8, 2009 Author Share Posted November 8, 2009 I have tried that it only displays 8 Array Array Array Array Array Array any more idea Quote Link to comment Share on other sites More sharing options...
bob2588 Posted November 8, 2009 Share Posted November 8, 2009 i am looking Quote Link to comment Share on other sites More sharing options...
bob2588 Posted November 8, 2009 Share Posted November 8, 2009 this line of code here $query="Insert into artus_test values('','$org','$des','$start_month','$start_year','$end_month','$end_year')"; $result=mysql_query($query); do you have column in the data base that the data should be enter into ?? Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 8, 2009 Share Posted November 8, 2009 the error is in your HTML. you have [] in the name attribute of your form elements.. that tells the PHP that the form element values being passed is an array.. i.e. <select name="curEmpEndMonth[]" > ... unless you have multiple elements with the same name and hence you need to pass an array of values, change it to <select name="curEmpEndMonth" > Quote Link to comment Share on other sites More sharing options...
bob2588 Posted November 8, 2009 Share Posted November 8, 2009 hmm did not think of that Quote Link to comment Share on other sites More sharing options...
n000bie Posted November 9, 2009 Author Share Posted November 9, 2009 @bob2588 yes i got a table and all the fields all the data are saved correctly, problem is when i want to display the records @joel24 form elements will be in array i.e. generated by javascript there will be more than 1 fields so there will be multiple elements function copyTrue(){ var t = document.getElementById('copyme').cloneNode(true); document.getElementById('copyhere').appendChild(t); } </script> well the problem is not when saving only when displaying the data Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 9, 2009 Share Posted November 9, 2009 thats because when you save it you're using serialize() and the array is retaining its structure, then later on you pull that from the DB and unserialize() and then try to echo it when you can't echo an array. for your submit you may want to have a for loop and store only values, not an array. then you won't need to use serialize(); is it the entire form that is replicated? or just individual elements... i.e. would the occurrences of element1 be the same as the count of element 2? if the entire form is replicated, you could do something like if (isset($_POST['submit'])) { $org = $_POST['curEmpName']; $des = $_POST['curEmpDesignation']; $start_month = $_POST['curEmpStartMonth']; $start_year = $_POST['curEmpStartYear']; $end_month = $_POST['curEmpEndMonth']; $end_year = $_POST['curEmpEndYear']; for ($i = 0; $i < count($org); $i++) { $query="Insert into artus_test values('','{$org[$i]}','{$des[$i]}','{$start_month[$i]}','{$start_year[$i]}','{$end_month[$i]}','{$end_year[$i]}')"; $result=mysql_query($query); if ($result) { echo 'success'; } else { echo 'something failed...';} $i++; } } then get rid of the "unserialize" when you pull the data from the DB. Quote Link to comment 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.