I have spent the last 3 hours trying to get this code working. I don’t understand why it’s telling me that ‘id’ is undefined.
My aim was to be able to update pictures in my little table. So far I can’t update it because of the error I’m getting.
Here’s the whole code:
<?php
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Edit Details</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5">
</head>
<body>
<div id="content">
<div class="about">
<h2>Modify Entry</h2>
<div class="main">
<?php
global $record;
global $oldPic2;
global $oldPic3;
global $id;
$con = mysql_connect('localhost', 'root', '') or die(mysql_error()) ;
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("peopledb", $con);
if(!isset($_POST['submit'])){
$q = "SELECT * from image_table WHERE id = $_GET[id]";
$result = mysql_query($q) or die(mysql_error());
$record = mysql_fetch_array($result);
$oldPic2 = $record['img2'];
$oldPic3 = $record['img3'];
}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div style="font-weight:bold; margin-top:5px; border: 1px solid #C6CFE1; padding:5px 0 5px 5px;background-color: #F5F2EE;">
<table cellspacing="6">
<tr>
<td>
<b>Name:</b>
</td>
<td>
<input type="text" name="name" id="name" value ="<?php echo $record['name']; ?>" />
</td>
<td>
<b>Image 1:</b>
</td>
<td>
<input type="text" name="breed" id="breed" value ="<?php echo $record['img']; ?>" />
</td>
<td>
<b>Image 2:</b>
</td>
<td>
<input type="text" name="img2" id="img2" size="16" value ="<?php echo $record['img2']; ?>" />
</td>
</tr>
</table>
<table>
<tr>
<td style="padding-right: 15px;">
<img src="test/<?php echo $record['img']; ?>" width="45" height="40" />
</td>
<td>
<input type="file" name="pic" value="Browse">
</td>
</tr>
<tr>
<td>
<img src="test/<?php echo $record['img2']; ?>" width="45" height="40" />
</td>
<td>
<input type="file" name="pic2" value="Browse">
</td>
</tr>
<tr>
<td>
<img src="test/<?php echo $record['img3']; ?>" width="45" height="40" />
</td>
<td>
<input type="file" name="pic3" value="Browse">
</td>
</tr>
</table>
<p />
<b>Description:</b> <br />
<script>edToolbar('description'); </script>
<textarea rows="10" cols="60" id="description" name="description" align="center"><?php echo $record['description']; ?></textarea><br /><br />
<br /><br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Submit Update" />
</div>
</form>
<?php
if(isset($_POST['submit'])){
$pic =($_FILES['pic']['name']);
$pic2 =($_FILES['pic2']['name']);
$pic3 =($_FILES['pic3']['name']);
if( $pic == ""){
$pic = $record['img'];
}
if( $pic2 == ""){
$pic2 = $oldPic2;
}
if( $pic3 == ""){
$pic3 = $oldPic3;
}
$id = $_GET[id];
$q = "UPDATE image_table SET `name`='$_POST[name]',
`description`='$_POST[description]',
`img`= '$pic',
`img2`= '$pic2',
`img3`='$pic3',
WHERE id = $_POST[id]";
mysql_query($q) or die(mysql_error());
$target = "test/";
//$target1 = $target."/".$record['id'];
$target1 = $target . basename( $_FILES['pic1']['name']);
$target2 = $target . basename( $_FILES['pic2']['name']);
$target3 = $target . basename( $_FILES['pic3']['name']);
if(move_uploaded_file($_FILES['pic']['tmp_name'], $target1)) {
}
if(move_uploaded_file($_FILES['pic2']['tmp_name'], $target2)) {
}if(move_uploaded_file($_FILES['pic3']['tmp_name'], $target3)) {
echo "Image updated successfully";
}else{
echo"There was an error updating your picture";
}
}
?>
</div>
</div>
<div class="list">
</div>
</div>
</body>
</html>
Any help would be highly appreciated.
Note: The select statement works, but it’s the when I try updating that it throws the error message. Id underfined 🙁
I finally got t working. Declaring the id as global did the trick. Previously, the id was not available outside the block where it was assigned a value.
A nice way to learn new things 🙂