...

View Full Version : Can't see values in array! Please Correct Me



mwm
02-26-2007, 03:13 PM
I'm pulling info from a table in mysql that has notes on customers. I can see the values being pulled by echoing them on the edit1.php page. But when the array is sent to editfinal.php I do not see the values of the array. What i want to do is update the note_tbl. What am I doing wrong? Am I not doing a step?

Thanks


###### This is edit1.php


<?
session_start();
include "conn.php";
$id=$_GET['id'];

echo "<input type=hidden name=id value=$id>";
?>



<html>
<body>

<h1><p align="center"></center></h1>
<head><link rel=stylesheet type=text/css href=menu.css></HEAD>
<h3><p align="center"><font color=red>Edit Contact</font></center></a></h3>
<p align="center"><a href="search.php?"><img src="search.gif" width='14' height='14' border="0"></a>
<a href="addressbook.php?"><img src="home.gif" width='14' height='14' border="0"></a></center>
<FORM ACTION="editfinal.php" METHOD="POST">
<p align="center"><input type="submit" name="submit" value="Save">
</center>

<?
$mysql = " select note from note_tbl where id ='$id'";
echo $mysql;

$result= mysql_query($mysql) or die ("Can't connect");


while($row = mysql_fetch_array($result)) {
echo "<br>";
echo $row['note'];
echo "<input type=hidden name=note[] value='".$note[$i]."'>";
}




?>



####### This is editfinal.php



<?
session_start();
include "conn.php";

echo $id;



echo "<pre>";
print_r($_POST);
echo "</pre>";


$sql="UPDATE note_tbl SET note = '$note' where id = '$id' ";
$result=mysql_query($sql) or die ("Could Not Update");


header("Location:addressbook.php");

?>

Fumigator
02-26-2007, 03:27 PM
The two obvious problems I can see is your "id" hidden input is not inside the <form> tags, and you are missing a </form> tag. Always validate your HTML markup.

Other issues you should fix are:

-Use <?php rather than <?

-Don't assume register_globals is turned on. Use $_POST['notes'] rather than $notes.

-Also, if you have more than one row in your note_tbl with the same id, your update query is going to write the same note to all rows, and you are ambiguous as to which value in your $_POST['note'] array you are using (You did build an array in your first script).

mwm
02-26-2007, 06:01 PM
Thanks. made changes you suggested and i can now see the array values on the editfinal.php page. But I still can't up date the table. What do you all think.

###edit1.php


<?
session_start();
include "conn.php";
$id=$_GET['id'];


?>



<html>
<body>

<h1><p align="center"></center></h1>
<head><link rel=stylesheet type=text/css href=menu.css></HEAD>
<h3><p align="center"><font color=red>Edit Contact</font></center></a></h3>
<p align="center"><a href="search.php?"><img src="search.gif" width='14' height='14' border="0"></a>
<a href="addressbook.php?"><img src="home.gif" width='14' height='14' border="0"></a></center>
<FORM ACTION="editfinal.php" METHOD="POST">
<p align="center"><input type="submit" name="submit" value="Save">
</center>

<?
$mysql = " select noteid,note from note_tbl where id ='$id'";



$result= mysql_query($mysql) or die ("Can't connect");
echo "<p align = center>";
echo "</center>";
echo "<table border=\"0\">";
echo "<th></th>";

if (mysql_num_rows($result) == 0)
{
echo "<center><font color = red>None!</font></center>";
}
else
{


$j = 0;

while ($row = mysql_fetch_array($result)) {

$note= $row['note'];
$noteid= $row['noteid'];

for ($i= 0; $i<count($note); $i ++) {

}

echo ($j++%2==0) ? '<tr bgcolor="#FFFFCC">' : '<tr bgcolor="#FFFFCC">';
print '
<td>
<input type="text" name="note[]" value="'.$row['note'].'">

</td>
';
echo "<td>" .($row['noteid']) . "</td>";
echo "<input type= hidden name=noteid[] value=$noteid>";
print '
<td>

<a href="addtouch.php?id='.$row['id'].'"></a>

</td>
';

}
}
echo '</table>';
echo "</center>";

echo "<hr>";

?>

</body>
</form>
</html>


####editfinal.php


<?
session_start();
include "conn.php";

echo $id;

$noteid = $_POST['noteid'];

echo "<pre>";
print_r($_POST);
echo "</pre>";





$query1 = "update note_tbl
set note = '{$_POST['note']}'
where noteid = '{$_POST['noteid']}'";


$result1 = mysql_query($query1) or die ("Could not close");


?>

Fumigator
02-26-2007, 06:24 PM
It looks like you are just about there. As you have it set up, you have created a couple of 2-level arrays $_POST['note'] and $_POST['noteid'], so you need to create a loop to update each row in your table, rather than performing only one update query as you are doing right now.



for ($i = 0; $i < count($_POST['note']); $i++) {
$query1 = "update note_tbl
set note = '{$_POST['note'][$i]}'
where noteid = '{$_POST['noteid'][$i]}'";
$result1 = mysql_query($query1) or die ("Bad update query, sql = ".$query1." and error is ".mysql_error());
}

mwm
02-26-2007, 07:56 PM
Yep that worked great. Thank You :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum