Updating information in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Updating information in database

Post by bobby »

Hi Guys,

I'm just working through a tutorial from the book php bible second edition. IM having problems with one part of the tutorial.

Below is the code I feel where the problem lies. every time i add data to the form and press enter in order to commit it to the database. I get the message "There was a problem inserting your text" This line is in the code below.

But im confused as to why its getting triggered. I have been trying to work out wgat the code above does and what it means and so far i cant see anything wrong.

would anybody have any ideas as to why i get the error message

Code: Select all

// Insert edited entry
    $edit_date = $_POSTї'edit_date'];
    // Escape single-quotes and apostrophes
    $blogtext = addslashes($_POSTї'blogtext']);
    $query = "UPDATE mylog SET blogtext = '$blogtext' WHERE date = $edit_date";
    $result = mysql_query($query);
    if (mysql_affected_rows() == 1) {
      header("Location: db_login.php");
    } else {
      echo "There was a problem inserting your text.";
      exit;
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Your information is not being inserted. If you have a look in your database you should be able to check this. If you change your code to the following:

Code: Select all

// Insert edited entry 
    $edit_date = $_POST['edit_date']; 
    // Escape single-quotes and apostrophes 
    $blogtext = addslashes($_POST['blogtext']); 
    $query = "UPDATE mylog SET blogtext = '$blogtext' WHERE date = $edit_date"; 
echo mysql_errno() . ": " . mysql_error(). "\n";
    $result = mysql_query($query); 
    if (mysql_affected_rows() == 1) { 
      header("Location: db_login.php"); 
    } else { 
      echo "There was a problem inserting your text."; 
      exit;
It should give you an error message, which should help you figure out whats causing the problem
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

Hi


My original code works it updates the data in the table but still return the error message. However i tried your code and youir code also upodates the data in the database but still returns with this message

0: There was a problem inserting your text

Not sure what the importance of the 0: is
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

OK i ahve looked up the importance of the zero and its a code that gets generated by using the mysql erno function to help u figure out where the problem is.

But the thing is zero means there is no error

but there is a problem

lol
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sounds like you are trying to update more than one row.

Code: Select all

if (mysql_affected_rows() == 1) { 
// could be changed to 
    if (mysql_affected_rows() > 0) {
This only evaluates if its one row affected. You sure that you are not updating two rows?

Also, try to use correct quoting

Code: Select all

$query = "UPDATE mylog SET blogtext = '$blogtext' WHERE date = $edit_date";
// could be...
    $query = "UPDATE mylog SET blogtext = '$blogtext' WHERE date = '$edit_date'";
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

You are right

All my rows get effected when i add data regardless of the user.

im learning php at the momment and have bought the php bible second edition it looks like the book has errors in its code becuase i just copied the code out of the book. i was hoping to get it working then try modifying it.

would u be able to suggest a solution to my problem then once its working i can try and modify it
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well...
If the date in your table is just date-field (DDMMYY or similiar) I would change it to reflect a timestamp-field or datetime-field. As you add blogs, you will most likely not have two blogs at the same time anymore.

Then this kind of update would work better.

But, I'd recommend the use of id's instead. Add a field to the database with the name ID, type INT and make it auto_inc and indexed. Everytime a blog is inserted this would increase by one. You can later refer to that instead when updating. ( WHERE ID = '26' ) This makes it impossible to change more than one row.
im learning php at the momment
Then you've come to the right place if you have questions. ;)
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

ok dude i will take on board your suggestions about the date and ID field,

I have also made chnages to the two lines u mentioned but still i get the same error
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Very wierd.
Is this the only code that is in that file?
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

i could post the whole if u like
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

If it's not to big, please do. If it's huge, make it a personal note.
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

sorry dude not sure if this is to big, part of me thinks it is but i didnt know what u meant by personal note

Code: Select all

<?php
include("C:\Program Files\Apache Group\Apache2\htdocs\db_password.inc");
mysql_connect($hostname, $user, $password);
mysql_select_db("weblogs");

// Validate this user
$test_username = $_POST&#1111;'test_username'];
$query = "SELECT password
          FROM login
          WHERE username = '$test_username'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 1) &#123;
  echo "Something is wrong";
  exit;
&#125;
$password_row = mysql_fetch_array($result);
$db_password = $password_row&#1111;0];

if ($_POST&#1111;'test_password'] == $db_password && $_POST&#1111;'test_password'] != "") &#123;
  if ($_POST&#1111;'Submit'] == 'Enter') &#123;
   // Insert edited entry 
    $edit_date = $_POST&#1111;'edit_date']; 
    // Escape single-quotes and apostrophes 
    $blogtext = addslashes($_POST&#1111;'blogtext']); 
    $query = "UPDATE mylog SET blogtext = '$blogtext' WHERE date = '$edit_date'"; 
    echo mysql_errno() . ": ". mysql_error(). "\n" ; 
    $result = mysql_query($query); 
    if (mysql_affected_rows() == 0) &#123; 
      header("Location: db_login.php"); 
    &#125; else &#123; 
      echo "There was a problem inserting your text."; 
      exit; 



    &#125;
  &#125; else &#123;
    // Show the form with the appropriate entry filled in
    $php_self = $_SERVER&#1111;'PHP_SELF'];
    $test_password = $_POST&#1111;'test_password'];
    $edit_date = $_POST&#1111;'edit_date'];
    $query = "SELECT blogtext FROM mylog WHERE date = $edit_date";
    $result = mysql_query($query);
    if (mysql_num_rows($result) == 0) &#123;
      echo "No entry matches that date";
      exit;
    &#125;
    $entry_row = mysql_fetch_array($result);
    // When you get text from a SQL database,
    // you may need to strip backslashes from single-quotes.
    $blogtext = stripslashes($entry_row&#1111;0]);

$form_str = <<< EOFORMSTR
<HTML>
<HEAD>
<TITLE>Weblog data edit screen</TITLE>
</HEAD>
<BODY>
<FORM ACTION="$php_self" METHOD="POST">
<P>Text:<BR><TEXTAREA NAME="blogtext" COLS=75 ROWS=20 WRAP="VIRTUAL">$blogtext</TEXTAREA></P>
<INPUT TYPE="hidden" NAME="test_username" VALUE="$test_username">
<INPUT TYPE="hidden" NAME="test_password" VALUE="$test_password">
<INPUT TYPE="hidden" NAME="edit_date" VALUE="$edit_date">
<P><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Enter"></P>
</FORM>
</BODY>
</HTML>
EOFORMSTR;
    echo $form_str;
  &#125;
&#125; else &#123;
  mail("me@localhost", "Weblog snoop", "Someone from $REMOTE_ADDR is trying to get into your weblog entry screen.");
&#125;
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sorry, I can't spot it.
I'm hoping that this is an old test you pasted as it still contains the == 1.

Did you change the datefield in the database to a datetime field? If so, you need to alter the values in those, as just changing the field-type wont help if there is data allready stored within.

Code: Select all

031012 => 031012 00:00:00
031012 => 031012 00:00:00
// it's still the same values...
Hopefully someone else will see anything.
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

hi

Around the 28th line or so i did make the changes u mentioned.

Thank for your time
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

Anybody out there who can offer any suggestions
Post Reply