Jump to content

No errors and database not updating


davidjones1990

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/246625-no-errors-and-database-not-updating/
Share on other sites

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());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.