I have this code that is supposed to edit entries in my database but cannot figure out why it will not update the entry. All it does when I run it is erase the opclo and projectassigned mysql fields for the latest entry and it displays my success message. Can you please help me figure out what I have done wrong?
PHP Code:<?
//connect to mysql
//change user and password to your mySQL name and password
include("include/session.php");
mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die(mysql_error());
//select which database you want to edit
mysql_select_db(DB_NAME) or die(mysql_error());
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the projects
$result = mysql_query("select * from projects order by id");
//run the while loop that grabs all the project scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the projects
$projectname=$r["projectname"];//take out the title
$id=$r["id"];//take out the id
//make the title a link
echo "<a href='edit.php?cmd=edit&id=$id'>$projectname - Edit</a>";
echo "<br>";
}
}
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST['submit']))
{
$id = $_GET["id"];
$sql = "SELECT * FROM projects WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
projectname:<INPUT TYPE="TEXT" NAME="projectname" VALUE="<?php echo $myrow["projectname"] ?>" SIZE=30><br>
assigned to:<INPUT TYPE="TEXT" NAME="projectassigned" VALUE="<?php echo $myrow["projectassigned"] ?>" SIZE=30><br>
status:<INPUT TYPE="TEXT" NAME="opclo" VALUE="<?php echo $myrow["opclo"] ?>" SIZE=6><br>
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
<?
}
if ($_POST['submit'])
{
$pname = $_POST["projectname"];
$pass = $_POST["projectassigned"];
$status = $_POST["opclo"];
$id = $_GET["id"];
$sql = "UPDATE projects SET projectname='$projectname',projectassigned='$projectassigned',opclo='$opclo' WHERE id='$id'";
//replace news with your table name above
$result = mysql_query($sql);
echo mysql_error();
echo "Thank you! Information updated.";
}
}
?>


Reply With Quote

Bookmarks