...

View Full Version : Create & Update don't seem to work on some columns



Ian b
08-11-2011, 07:14 PM
Hi,

I'm trying to create and update records in a database through html forms. Several columns, all of which are, title, author, date, subject_id, synopsis and content. All work apart from synopsis and content.

Synopsis and content can be read and displayed on the html form if i update them manually, but they just won't seem to create through the form.

My query statement for the create is:



// Clean Data
$title = mysql_php_prep($_POST['title']);
$author = mysql_php_prep($_POST['author']);
$date = mysql_php_prep($_POST['date']);
$subject_id = mysql_php_prep($_POST['subject_id']);
$synopsis = mysql_php_prep($_POST['synopsis']);
$content = mysql_php_prep($_POST['content']);


// Perform Insert
$query = "INSERT INTO articles (
title, author, date, subject_id, synopsis, content
) VALUES (
'$title', '$author', $date, $subject_id, '$synopsis', '$content'
)";
if (mysql_query($query, $connection)) {
// Success!
header("location: content.php");
exit;
} else {
// Display error message
echo "<p>subject creation failed.</p>";
echo "<p>" . mysql_error() . "</p>";
}



mysql_close($connection); ?>


My edit query is:



// Clean data
$id = mysql_php_prep($_GET['art']);
$title = mysql_php_prep($_POST['title']);
$author = mysql_php_prep($_POST['author']);
$subject_id = mysql_php_prep($_POST['subject_id']);
$synopsis = mysql_php_prep($_POST['synopsis']);
$content = mysql_php_prep($_POST['content']);

// Perform Update
$query = "UPDATE articles SET
title = '{$title}',
author = '{$author}',
subject_id = {$subject_id},
synopsis = '{$synopsis}',
content = '{$content}',
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Sucess
$message = "The article was successfully updated.";
} else {
// Failed
$message ="The article update failed.";
$message .= "<br />". mysql_error();
}

} else {
// Errors occurred
$message = "There were " . count($errors) . " error/s in the form.";
}
} // End: if(isset($_POST['submit]))


I don't get any errors, so i've been trying to echo $row['content'] etc., but to be honest the mysql manual confuses me so i'm not having much luck with that.

The content and synopsis are input and displayed with 'textarea', i thought maybe it's a problem with that type? As the others are 'input'.



<p>Article Synopsis:
<textarea cols="100" rows="5" input"text" class="default" />
<?php echo $sel_article['synopsis']; ?>
</textarea>
<p>Article Content:
<textarea name="content" cols="100" rows="20" input"text" class="default" />
<?php echo $sel_article['content']; ?>
</textarea>
</p>
<br />


Just to clarify, everything works apart from the content and synopsis, which will only display if i enter the data in the columns manually. Anybody help please?

edit: mysql 5.5.13 php 5.3.4

Old Pedant
08-11-2011, 08:59 PM
Well, you don't have a name= for the synopsis field, so it will never get passed back to the PHP code.

But I don't see why content doesn't work.

But why not DEBUG DEBUG DEBUG???

You correctly create $query as a separate variable (yeah on you!), so it's trivial.


// Perform Update
$query = "UPDATE articles SET
title = '{$title}',
author = '{$author}',
subject_id = {$subject_id},
synopsis = '{$synopsis}',
content = '{$content}',
WHERE id = {$id}";
echo "<hr>DEBUG SQL: " . $query . "<hr/>\n";

$result = mysql_query($query, $connection);

Look at what that debug shows you. What's right? What's wrong?

Ian b
08-11-2011, 09:51 PM
Thanks for reply. Not sure why name= isn't in textarea for the synopsis as it used to be, probably because i've been changing loads of stuff to see if i can get it to work. It doesn't work with name="synopsis" either though as i made sure.

As for the debug, i didn't know about it until i came to this forum, although you telling me how to implement it for that query was great. However, it doesn't say or do anything, it just creates the article and takes me to content.php as always but without the synopsis or content addition. I'm having a look at the log although from what i understand logs can be written in more than one place and i haven't found anything yet.

As an aside, i'm finding that learning to program is really difficult, do you have any tips of how to get good? I just get problems after problems and i'm like argh constantly. Like this problem, been pulling my hair out and like i have no idea how to fix it as to me everything should work, but yet it doesn't... which is why i ask on a forum. I guess it's just read lots of stuff and practice lots is it?

Old Pedant
08-11-2011, 10:53 PM
Ahhh...your DEBUG is being lost because your code is proceeding to the next page without paying attention to whether the update worked or not. That's not an error during normal processing, of course, but it's a killer when debugging.

What I usually do in a case like this: At the to of each page I add a debug variable, something like

$DODEBUG = true; // change to false when done debuggiing

And then in my code I have stuff such as

if ( $DODEBUG ) echo "<hr>DEBUG SQL: " . $query . "<hr/>\n";

and, in the case of code as you have it, something like

if ( ! $DODEBUG ) header("location: content.php");
exit;

In other words, while I'm debugging, I suppress the move to the next page, so that I can indeed see the debug messages come out.

***********

Debugging is an art form. But being able to debug is also AT LEAST as important as being able to write code.

I've seen many promising candidates for a programming job get blackballed by the interview team because one or more of the interviewers didn't think the candidate understood the debug process well enough! Truth! (Even happened to me, once. Though I went back later to same company and got hired. It wasn't because I don't know how to debug: They just thought I didn't emphasize it enough in my interview!)

Anyway, I need to tell you that I am *not* a PHP programmer. So what I showed you above is the kind of easy debug stuff that applies to most any programming language. But for techniques specific to PHP you should ask others.

Old Pedant
08-11-2011, 10:55 PM
By the by, for this specific case, you could have simply done


if ( $DODEBUG )
{
echo "<hr>DEBUG SQL: " . $query . "<hr/>\n";
exit;
}

In other words: Just to find out WHAT the query was, simply stop the page right after the debug.

Fix the query, remove the exit, and keep debugging.

Ian b
08-12-2011, 12:59 PM
Debugging hey... so that's the other half of the mystery. It felt like i was missing something. I thought about learning induction or something to help me.

As for the specific example you gave, it still just goes to content.php without errors or stopping the page.

If i make required fields:



$required_fields = array('title', 'author', 'date', 'subject_id', 'synopsis', 'content');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
}

if(!empty($errors)) {
redirect_to("new_article.php");
}


..it takes me back to new_article.php as if the content and synopsis are missing. If i take out content and synopsis from the required fields it parses and creates/edits the article, so it would seem that it's just not 'seeing' the content and synopsis which would mean that i haven't defined them somewhere i guess. Although where that is i have no idea. I will keep looking.

Thanks for the heads up on the importance of debugging though, i will look into it. Nice one.

Old Pedant
08-12-2011, 09:26 PM
As I said, you need to disable you redirects while debugging. They end up hiding any debugging you do. My usual practice is to replace the redirect with something like

echo "Here is where I would redirect to xxxx<hr>";
exit;

(Well, the equivalent, anyway, since I don't use PHP.)

Ian b
08-13-2011, 02:09 PM
Sorry, i misunderstood about disabling redirects and read it as the $DODEBUG disables them. I'm becoming a pain, aren't i? Apologies.

Anyway, the content and synopsis come back as undefined. Debug read out is:



Notice: Undefined index: synopsis in /Users/ian_b/Sites/uv/create_article.php on line 25 Notice: Undefined index: content in /Users/ian_b/Sites/uv/create_article.php on line 26 Here is where I would redirect to content.php?art=

DEBUG SQL: INSERT INTO articles ( title, author, date, subject_id, synopsis, content ) VALUES ( 'ewfdiweifj', 'fijwfoijoi', '2011-08-15', 4, '', '' )


And to be honest, i have no idea why they are undefined as i set them up as i was setting the others up. Maybe i misunderstand the meaning of the notice. Maybe i'm stupid i don't know.

I also tried:



$result = mysql_query($query) or trigger_error(mysql_error().'<br />Query was:'.$query,E_USER_ERROR);


..after the $query and it comes back as:



Query was:UPDATE articles SET id = 8, title = 'ewfdiweifj', author = 'fijwfoijoi', date = 2011-08-15, subject_id = 4, synopsis = '', content = '', WHERE id = 8 in /Users/ian_b/Sites/uv/edit_article.php on line 38


So yeah, synopsis and content fields are being sent as empty, even though i put stuff in the fields before submitting.

I'm a bit lost to be honest, think i will leave it for a day as getting a bit stressed so come back with a clean head hopefully.

Old Pedant
08-13-2011, 10:43 PM
I would take another close look at the HTML page.

I don't suppose you can show it live?

Also, you have a goof in the UPDATE query: You need apostrophes around the date= value.

That is:


UPDATE articles SET ... date = '2011-08-15', ...

You have them, correctly, in your INSERT.

Ian b
08-14-2011, 10:44 AM
Fixed. Was the HTML. Didn't enclose them within the action attribute. What a ridiculous mistake to make. Feel a bit silly now. Teach me to be blasť with HTML. Thanks Pedant.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum