Page 1 of 1

Update sql records with php problem

Posted: Sun Apr 23, 2006 6:15 am
by gillsm1
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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):

Code: Select all

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> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  <a href="deletearticle.php?article_id=$line[article_id]">delete</a>
<br>$line[subheadline]<br>
<i>created by: <b>$line[editor]</b>
 &nbsp category: <b>$line[section]</b>
   &nbsp 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):

Code: Select all

<?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


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Apr 23, 2006 10:07 am
by feyd
does "headline<article id>:" show? If so, switch the code in the hidden field to that code.

Posted: Sun Apr 23, 2006 10:54 am
by John Cartwright
when you get your code to work, try adding a ' into one your input fields :wink:

* cough cough * mysql_real_escape_string() all input going into the query