gillms1 Posted April 23, 2006 Share Posted April 23, 2006 Hi, I want to update some existing records in an sql database and am having some problems. I have one script which lists all articles currently in the database (editarticlelist.php): include "admin_header.php"; include "db_connect.php"; // Open DB and init cutoff $link = opendb(); //$cutoff = strtotime("-".$max_days." day"); //$cutoff = date("Y-m-d H:i",$cutoff); // Get all article titles in timeframe and that are published $query = "SELECT * FROM articles"; //$query = $query." AND pubdate >= \"$cutoff\""; $result = mysql_query($query,$link) or die("Query failed: $query"); echo "<h3>Please select article to edit</h3><p>"; // Display each article title while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { PRINT <<<HTML <b><a href="editarticle2.php?article_id=$line[article_id]"> $line[headline] </b></a>           <a href="deletearticle.php?article_id=$line[article_id]">delete</a> <br>$line[subheadline]<br> <i>created by: <b>$line[editor]</b>   category: <b>$line[section]</b>   published on: $line[created]<p> HTML; } // End while mysql_close($link); include('admin_footer.php'); ?> and i have another which displays the selected article in a form ready for updating (editarticle2.php): <?php include "admin_header.php"; //include "db_connect.php"; $dbcnx = @mysql_connect('localhost', 'root', ''); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('chronicle')) { exit('<p>Unable to locate the chronicle ' . 'database at this time.</p>'); } if(isset($_POST['submit'])) { $article_id = $_POST['article_id']; $headline = $_POST['headline']; $subheadline = $_POST['subheadline']; $content = $_POST['content']; $sql = ("UPDATE articles SET headline='$headline', subheadline='$subheadline', content='$content' WHERE article_id='$article_id'"); $result = mysql_query($sql) or die("$sql failed: " . mysql_error()); echo "($sql)"; echo "<b>Thank you! News UPDATED Successfully!<br>You'll be redirected to Home Page after (4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=editarticlelist.php>"; } else { $article_id = $_GET[article_id]; $sql = ("SELECT * FROM articles WHERE article_id=$article_id"); $result = mysql_query($sql) or die("$sql failed: " . mysql_error()); $myrow = mysql_fetch_assoc($result); $headline = $myrow["headline"]; $subheadline = $myrow["subheadline"]; $content= $myrow["content"]; ?> <br> <h3>::Edit News</h3> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="article_id" value="<? echo $myrow[article_id]?>"> headline<?php echo $article_id ?>: <input name="headline" size="40" maxlength="255" value="<?php echo $headline; ?>"> <br> subheadline: <textarea name="subheadline" rows="7" cols="30"><?php echo $subheadline; ?></textarea> <br> content: <textarea name="content" rows="7" cols="30"><?php echo $content; ?></textarea> <br> <input type="submit" name="submit" value="Update News"> </form> <?php } ?> <?php include('admin_footer.php'); ?> the form displays fine, with the data populated. I change the data and press submit, I get the update successful method, however the records dont change in my database. the (echo $sql) query reads : (UPDATE articles SET headline='Sony cuts price of PlayStation 3', subheadline='Sony has cut the price of its best-selling PlayStation 2 (PS2) by $20 in the US.', content='The pircs' WHERE article_id='')Thank you! News UPDATED Successfully! You'll be redirected to Home Page after (4) Seconds so there is no article_id being read into the query, but i cant see why this is? :s can anyone help? Thanks, Sunny Link to comment https://forums.phpfreaks.com/topic/8174-update-sql-records-with-php-problem/ Share on other sites More sharing options...
mb81 Posted April 23, 2006 Share Posted April 23, 2006 [!--quoteo(post=367665:date=Apr 23 2006, 09:03 AM:name=gillms1)--][div class=\'quotetop\']QUOTE(gillms1 @ Apr 23 2006, 09:03 AM) [snapback]367665[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hi, I want to update some existing records in an sql database and am having some problems. I have one script which lists all articles currently in the database (editarticlelist.php): include "admin_header.php"; include "db_connect.php"; // Open DB and init cutoff $link = opendb(); //$cutoff = strtotime("-".$max_days." day"); //$cutoff = date("Y-m-d H:i",$cutoff); // Get all article titles in timeframe and that are published $query = "SELECT * FROM articles"; //$query = $query." AND pubdate >= \"$cutoff\""; $result = mysql_query($query,$link) or die("Query failed: $query"); echo "<h3>Please select article to edit</h3><p>"; // Display each article title while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { PRINT <<<HTML <b><a href="editarticle2.php?article_id=$line[article_id]"> $line[headline] </b></a>           <a href="deletearticle.php?article_id=$line[article_id]">delete</a> <br>$line[subheadline]<br> <i>created by: <b>$line[editor]</b>   category: <b>$line[section]</b>   published on: $line[created]<p> HTML; } // End while mysql_close($link); include('admin_footer.php'); ?> and i have another which displays the selected article in a form ready for updating (editarticle2.php): <?php include "admin_header.php"; //include "db_connect.php"; $dbcnx = @mysql_connect('localhost', 'root', ''); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('chronicle')) { exit('<p>Unable to locate the chronicle ' . 'database at this time.</p>'); } if(isset($_POST['submit'])) { $article_id = $_POST['article_id']; $headline = $_POST['headline']; $subheadline = $_POST['subheadline']; $content = $_POST['content']; $sql = ("UPDATE articles SET headline='$headline', subheadline='$subheadline', content='$content' WHERE article_id='$article_id'"); $result = mysql_query($sql) or die("$sql failed: " . mysql_error()); echo "($sql)"; echo "<b>Thank you! News UPDATED Successfully!<br>You'll be redirected to Home Page after (4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=editarticlelist.php>"; } else { $article_id = $_GET[article_id]; $sql = ("SELECT * FROM articles WHERE article_id=$article_id"); $result = mysql_query($sql) or die("$sql failed: " . mysql_error()); $myrow = mysql_fetch_assoc($result); $headline = $myrow["headline"]; $subheadline = $myrow["subheadline"]; $content= $myrow["content"]; ?> <br> <h3>::Edit News</h3> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="article_id" value="<? echo $myrow[article_id]?>"> headline<?php echo $article_id ?>: <input name="headline" size="40" maxlength="255" value="<?php echo $headline; ?>"> <br> subheadline: <textarea name="subheadline" rows="7" cols="30"><?php echo $subheadline; ?></textarea> <br> content: <textarea name="content" rows="7" cols="30"><?php echo $content; ?></textarea> <br> <input type="submit" name="submit" value="Update News"> </form> <?php } ?> <?php include('admin_footer.php'); ?> the form displays fine, with the data populated. I change the data and press submit, I get the update successful method, however the records dont change in my database. the (echo $sql) query reads : (UPDATE articles SET headline='Sony cuts price of PlayStation 3', subheadline='Sony has cut the price of its best-selling PlayStation 2 (PS2) by $20 in the US.', content='The pircs' WHERE article_id='')Thank you! News UPDATED Successfully! You'll be redirected to Home Page after (4) Seconds so there is no article_id being read into the query, but i cant see why this is? :s can anyone help? Thanks, Sunny[/quote]It probably has to do with how you are calling the page and if you are sending the article_id variable as a POST or a GET replace all the $_POST & $_GET with a $_REQUEST and see what happens. Link to comment https://forums.phpfreaks.com/topic/8174-update-sql-records-with-php-problem/#findComment-29856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.