Jump to content

UPDATE + CMS


worked

Recommended Posts

Hey there- I have a very simple Content Management System but I can't seem to figure out how to UPDATE the database with the new submitted content.

 

The content is successfully added to input text/textarea box's via the mysql_fetch_array(MYSQL_ASSOC).  When the submit button is pressed it should UPDATE the db, but it does not.  I'm new to MySQL, any help is really appreciated!  Thanks!

 

admin_page.php

<?php
$connect = mysql_connect("host", "username", "password");
mysql_select_db("database", $connect);
?>
<form method="post" action="insert.php">
<!-- main_headline -->
<?php
$query  = "SELECT title, content, link FROM main_headline";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td colspan="2" id="headline_cms">Title:<br><input name="title" type="text" size="90" value="<?php echo "{$row['title']}"; ?>"/></td>
  </tr>
  <tr>
    <td colspan="2" id="headline_cms">Link:<br><input name="link" type="text" size="90" value="<?php echo "{$row['link']}"; ?>"/></td>
  </tr>
  <tr>
    <td colspan="2" id="headline_cms">Content:<br><textarea name="content" cols="90" rows="5"><?php echo "{$row['content']}"; ?></textarea></td>
  </tr>
  <tr>
    <td colspan="2" id="headline_cms"><input type="hidden" name="id" value="<?php echo "{$row['id']}" ?>" /></td>
  </tr>
</table>
<?php } //end while statement ?>

 

insert.php

<?php
$title = $_POST['title'];
$link = $_POST['link'];
$content = $_POST['content'];
$id = $_POST['ID'];

$connect = mysql_connect("host", "username", "password");
mysql_select_db("database", $connect);

//Perform the query
$query = "UPDATE main_headline SET title='$title', link='$link', image='$image', content='$content'";

//Error checks
if(!$query) {
echo "Failure! Content Not Updated";
}else{ 
echo "Success! Content Updated";
}
?>

 

Link to comment
Share on other sites

Well I got it to update the table by simply adding "mysql_query" to the UPDATE command in the insert.php:

$query = mysql_query("UPDATE main_headline SET title='$title', link='$link', image='$image', content='$content'");

 

My only issue is that this method updates all the rows with the same content, ignoring the rest of the changes made to the admin_page.  What if I needed the UPDATE query to look at each row loaded on the admin_page via the mysql_fetch_array($result, MYSQL_ASSOC), and only changing the appropriate row.  How do you UPDATE content that is loaded to the admin_page via the fetch_array command?

 

$query  = "SELECT title, content, link FROM main_headline";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>

Link to comment
Share on other sites

Thanks for responding! I've updated the insert.php file to include a WHERE clause, but nothing happens now.  It echos the "Success! Content Updated" line but nothing changes in the db...  Here's the code now:

 

insert.php

<?php
$title = $_POST['title'];
$link = $_POST['link'];
$content = $_POST['content'];
$ID = $_POST['ID'];

$connect = mysql_connect("host", "username", "password");
mysql_select_db("database", $connect);

//Perform the query
$query = "UPDATE main_headline SET title='$title', link='$link', image='$image', content='$content' WHERE ID='$ID'";
$result = mysql_query($query);

//Error checks
if(!$result) {
echo "Failure! Content Not Updated";
}else{ 
echo "Success! Content Updated";
}
?>

 

Sorry if this all seems really confusing. I guess I'm just looking for a way to update multiple rows with an UPDATE command, where the form that updates the db is made up of textboxs dynamically populated via the mysql_fetch_array($result, MYSQL_ASSOC).  Any other help you can provide is greatly appreciated.  Thanks!

Link to comment
Share on other sites

Thank you again for responding... seems this one was a very simple solution.  I had the register globals = Off in my  php.ini settings.  The code above works if turned on.  Unfortunately most shared hosts are turning this off for security reasons, as did my local install of MAMP Pro (PHP 5 defaults to Off now).  So the solution to this mess is to declare the form variables as global variables, i.e. $ID=$_POST['ID'];. 

 

New mission, I am going to try and update the existing code above so that it is register globals = Off compatible. 

 

However, if someone wants to chime in with a solution, while I fiddle, I'm all ears... Thanks!

 

Oh, and you can test your register globals setting by coping this code snippet.  It will echo either true or false:

<?php
if (ini_get('register_globals')){echo "<font color=red>Registor global is ON</a><br>This is a security issue, please change settings inside your php.ini file</font>";}
else{echo "Register global is OFF";}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.