davidjones1990 Posted September 7, 2011 Share Posted September 7, 2011 Here is my code to update a blog entry. When submitted there is no error but the database is not updated with the new info. I have a similar script on a different page and it works fine so I am confused to why this is happening. I am also a PHP beginner. Here is my code: <?php include_once ("scripts/checkuserlog.php"); include_once ("scripts/connectToMysql.php"); if (!$_SESSION['idx']) { $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>'; include_once 'msgToUser.php'; exit(); } else if ($logOptions_id != $_SESSION['id']) { $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>'; include_once 'msgToUser.php'; exit(); } $date = date("m.d.y"); $workoutName = ''; $workoutDescription = ''; $id = $_GET['id']; if(isset($_POST['workoutName'])){ $workoutName = $_POST['workoutName']; $workoutDescription = $_POST['workoutDescription']; $blogUpdate = mysql_query("UPDATE blog SET workoutName='$workoutName', workoutDescription='$workoutDescription' WHERE id='$id' LIMIT 1") or die (mysql_error()); if ($blogUpdate){ $successMsg = 'Blog entry updated successfully'; } else { $errorMsg = 'Problems arose during the information exchange, please try again later.'; } } $sql = mysql_query("SELECT * FROM blog WHERE id='$id' LIMIT 1"); while($row = mysql_fetch_array($sql)){ $workoutName = $row['workoutName']; $workoutDescription = $row['workoutDescription']; $dateOfEntry = $row['datetime']; } ?> <!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>S.F.I - Edit Blog Entry</title> <link href="style/main.css" rel="stylesheet" type="text/css" /> <link href="style/layout.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="container"> <?php include_once ("bannerFiles/bannerTemplate.php"); ?> <?php include_once ("bannerFiles/bannerMenu.php"); ?> <br /><br /> <div id="content"> <table bgcolor="#DBE4FD" width="950px"> <form action="editBlogPost.php" method="post" enctype="multipart/form-data"> <tr> <td width="200px"><span class="blackText">Workout Name:</span></td><td width="650px"><input name="workoutName" type="text" id="workoutName" value="<?php echo ("$workoutName");?>"/> <span class="blackText">Date of entry: <?php echo ("$dateOfEntry"); ?></span></td></tr> <tr> <td><span class="blackText">Workout Description:</span></td><td><textarea name="workoutDescription" cols="75" rows="10" id="workoutDescription" /><?php echo ("$workoutDescription"); ?></textarea></td></tr> <tr><td><input name="submit" id="submit" type="submit" value="Update" /></td><td><span class="errorMsg"><?php echo ("$errorMsg"); ?><?php echo ("$successMsg"); ?></span></td> </tr> </form> </table> </div> <br /><br /> <?php include_once ("footerFiles/footerTemplate.php"); ?> </div> </body> </html> Thanks in advance for any help Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted September 7, 2011 Share Posted September 7, 2011 have you outputted $id to make sure it is what you expect before using it in your update query? Quote Link to comment Share on other sites More sharing options...
gristoi Posted September 7, 2011 Share Posted September 7, 2011 heres your problem: $id = $_GET['id']; your POSTING data from the form. Not sending the variable through $_GET[] Quote Link to comment Share on other sites More sharing options...
davidjones1990 Posted September 7, 2011 Author Share Posted September 7, 2011 Yep, the ID corresponds to the correct ID in the database Quote Link to comment Share on other sites More sharing options...
davidjones1990 Posted September 7, 2011 Author Share Posted September 7, 2011 Im getting the blog entry ID from the URL so I can use it to update that blog entry with the new posted information Is that right? or do I need to go back to basics Quote Link to comment Share on other sites More sharing options...
gristoi Posted September 7, 2011 Share Posted September 7, 2011 close, you are getting the blog entry via the url , but you are then posting the form back to itself which wipes out the id. add this into your form <input type="hidden" name ='id' value="".$_GET['id'].""/> that will in effect auto forward your get variable along with the form. then: if(isset($_POST['workoutName'])){ $id = $_POST['id']; $workoutName = $_POST['workoutName']; $workoutDescription = $_POST['workoutDescription']; $blogUpdate = mysql_query("UPDATE blog SET workoutName='$workoutName', workoutDescription='$workoutDescription' WHERE id='$id' LIMIT 1") or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
davidjones1990 Posted September 7, 2011 Author Share Posted September 7, 2011 Works now, although I put this in the form instead of what you said, I know your way is better but i just want to get it working for now <input type="hidden" name ='id' value="<?php echo ("$id"); ?>"/> Thanks for your help 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.