Jump to content

UPDATE from form data


Lodius2000

Recommended Posts

so i have a php variable $var

it contains form data that I want to update a table field because I misspelled a word in the field

 

the field in my db might be one of 2 things, varchar(255) or text,

 

here is my stab at the SQL to get what I want done

 

article_id is an INT

 

UPDATE table_name SET field_name = '$var' WHERE article_id = '$id'

 

is all my punctation and syntax correct, because this doesnt work, coincidentally, I have tried to change the contents of the same 2 fields directly from phpmyadmin but that doesnt seem to work either, is this a permission thing, if my db user profile has full priviliages

 

THANKS

Link to comment
Share on other sites

<?php
function process_form(){
global $db, $id, $title, $article;

$edited_title = mysql_real_escape_string($_POST['title']);
$edited_article = mysql_real_escape_string($_POST['body']);


//check to see if title has changed, if changed, update db
if ($title != $edited_title){

	$db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'");
	print "Updated <strong>$title</strong> in the database.<br />\n";
}

//check to see if body has changed, if changed, update db
if ($article != $edited_article){
	$db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'");
	print "Updated <strong>article body</strong> in the database.<br />\n";
}
}
?>

 

this prints "Updated article body in the database." no matter what changes were made, even if nothing was altered in $article, is still prints that, and it has never printed "Updated $title in the database." even when I do make changes there.

 

thanks

Link to comment
Share on other sites

<?php
function process_form(){
global $db, $id, $title, $article;

$edited_title = mysql_real_escape_string($_POST['title']);
$edited_article = mysql_real_escape_string($_POST['body']);


//check to see if title has changed, if changed, update db
if ($title != $edited_title){

	$db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'") or die(mysql_error());
	print "Updated <strong>$title</strong> in the database.<br />\n";
}

//check to see if body has changed, if changed, update db
if ($article != $edited_article){
	$db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'") or die(mysql_error());
	print "Updated <strong>article body</strong> in the database.<br />\n";
}
}
?>

 

Try that and see what errors you get.

Link to comment
Share on other sites

DARKWATER:

 

done so a query no looks like

 

$db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'")OR die(mysql_error());

 

so i brought up a page i want to edit, typed 'test' in both the $_POST['title'] and $_POST['body'] fields, posted and it printed

 

Updated 3 months already! test in the database.

Updated article body in the database.

 

 

but there was no change in the record

 

interestingly, i went back to the same record and added test onto only $_POST['title'] and I got the same confirmation as above

 

 

 

will try yours also BrianM

Link to comment
Share on other sites

dblogin.php will not be posted, it works on every other page i have so it is correct,

pear/db.php can be found at  http://pear.php.net/package/DB/docs/1.7.14RC1/

 

a sample url for $_GET['id'] would be /index.php?id=12

 

i have also printed all the relevant variables to make sure they contain the modified values ie TEST TEST TEST and they all do, hust dont know whats going on

 

 

<?php

//this page edits articles
session_start();
require ('../../../install/PEAR/DB.php');
require ('../../../../dbfiles/db_login.php');
$db->setErrorHandling(PEAR_ERROR_DIE);
$db->setFetchMode(DB_FETCHMODE_ASSOC);

//print a text box
function input_text($element_name, $values){
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '"/>';
}

//print a textarea
function input_textarea($element_name, $values){
print '<textarea cols="75" rows="50" name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}



//grabs article_id for later query
$id = $_GET['id'];


if ($_SESSION['username']){

if($_POST['_submit_check']){
	if($form_errors = validate_form()){
		show_form($form_errors);
	} else {
		process_form();
	}
} else {
	show_form();
}
} else {
print '<a href="../index.php">Log In dummy</a>';
}


function show_form($errors = '') {
global $db, $id;

$items = $db->getRow('SELECT article_title, article FROM text_cms WHERE article_id = ?',
array($id));

$title = stripslashes($items['article_title']);
$article = stripslashes ($items['article']);


//If form has been submitted, get defaults from submitted variables
if ($_POST['_submit_check']){
	$defaults = $_POST;
} else {
	$defaults = array('title' => $title,
					  'body'  => $article);
}

if ($errors){
	print 'Please correct these errors: <ul><li>';
	print implode('</li><li>', $errors);
	print '</li></ul>';
}

print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
//begin the unique form

print '<br />';
print "Article Title";
print "<br />\n";
input_text('title', $defaults);
print "<br /><br />\n";
print 'Article Body';
print "<br />\n";
input_textarea('body', $defaults);

print "<br /><br />\n";

input_submit('submit', 'Edit Article');

print '<input type="hidden" name="_submit_check" value="1" />';
print "\n";
print '</form>';
}

function validate_form(){

if (trim(strlen($_POST['title'])) > 255){
	$errors[] = 'Article title is too long, please shorten it';
}

return $errors;

}

function process_form(){
global $db, $id, $title, $article;

$edited_title = mysql_real_escape_string($_POST['title']);
$edited_article = mysql_real_escape_string($_POST['body']);


//check to see if title has changed, if changed, update db
//if ($title != $edited_title){

	$db->query("UPDATE text_cms SET article_title = '$edited_title' WHERE article_id = '$id'")OR die(mysql_error());
	print "Updated <strong>$title</strong> in the database.<br />\n";
//}

//check to see if body has changed, if changed, update db
//if ($article != $edited_article){
	$db->query("UPDATE text_cms SET article = '$edited_article' WHERE article_id = '$id'")OR die(mysql_error());
	print "Updated <strong>article body</strong> in the database.<br />\n";
//}

print '<a href="../managearticle/index.php">Go back to article management</a>';


}
?>

 

thanks

Link to comment
Share on other sites

it sounds like we might all be coming to the same conclusion, i am talking with people at work and they all seem to think it could be something with my host because the db user does have full permissions, that was one of the first things i checked

 

I think i may take it up with my host

 

if someone could give me a confirmation that my sql should be doing what I want it to do, like if the sytax is right, i could call this solved, I just dont know UPDATE statements too well, so i want to make sure of that first

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.