Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem updating row 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
It doesn't update the row at all. Also, script.php?file=EntryEdit&id=$id doesn't call $id in the URL.

Code:
function do_edit_entry($id, $entry_name, $entry_email, $entry_url, $entry_msn, $entry_aim, $entry_icq, $entry_yahoo, $entry_message) {

global $admin, $prefix, $dbi;

$result = sql_query("update ".$prefix."_guestbook set name='$entry_name', email='$entry_email', url='$entry_url', msn='$entry_msn', aim=$entry_aim', icq='$entry_icq', yahoo='$entry_yahoo', message='$entry_message', date='$entry_date', time='$entry_time', ip='$entry_ip' where id='$id'", $dbi);

Header("Location: script.php?file=EntryEdit&id=$id");

}

What's wrong? :-(
 
Again, I'm just asking the dumb questions. But I've found that the only question that is truly dumb is the one you don't ask.



What is in $result after the UPDATE statement? Is it TRUE or FALSE?

And does that query string really evaluate to the value you think it does? ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
I'm sorry... :-(

The value is false because $id is not being called.
 
Aren't you passing in that value? ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
Yes, I am. I made the sql query into a print command:

$result = sql_query("update guestbook set name='test', email='test', url='test', msn='test', aim=test', icq='test', yahoo='test', message='test!', date='test', time='test', ip='test' where id='1')
 
Just wondering...are the fields time and date varchars or TIME and DATE fields? If they are TIME and DATE, the value of test will not work(I don't think).

Rick If I have helped you just click the first link below to let me know :)
 
Ooooh!!! I think I finally saw the problem! Instead of this:

$result = sql_query("update guestbook set name='test', email='test', url='test', msn='test', aim=test', icq='test', yahoo='test', message='test!', date='test', time='test', ip='test' where id='1');

try this:

sql_query("update guestbook set name='test', email='test', url='test', msn='test', aim=test', icq='test', yahoo='test', message='test!', date='test', time='test', ip='test' where id='1');

I had that same problem a while ago on my mysql server. It frustrated me for so long until I figured it out!

Rick If I have helped you just click the first link below to let me know :)
 
No, "$result = mysql_query ("UPDATE....." is okay. The function for an UPDATE statement will return TRUE or FALSE for the success or failure of the query. And in keeping with Carlos' Law: "That which can be controlled, must be controlled.", I recommend storing that value so you can test it.

nivly, I was recommending that you do the string contatenation that you are doing in the function as an assignment statement. Do this:

$foo = "update ".$prefix."_guestbook set name='$entry_name', email='$entry_email', url='$entry_url', msn='$entry_msn', aim=$entry_aim', icq='$entry_icq', yahoo='$entry_yahoo', message='$entry_message', date='$entry_date', time='$entry_time', ip='$entry_ip' where id='$id'";

then print out the value of $foo. This test will see whether the query is what you think it should be. ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
Just wondering, are you using SQL or mySQL? Because last time you posted you used mySQL. Anyway.

SLEIPNIR214--Are you sure? I'm a lot more familiar with mysql so I'll tell you in those terms, but when I would say:
$q = "INSERT INTO...";
$res = mysql_query($q);
and leave that script how it was, it wouldn't execute.

When I changed it to this:
$q = "INSERT INTO...";
mysql_query($q);

it would run just fine. Is that just for INSERT queries?
NIVLY--I just think that if you are still haveing problems try it and if it doesn't work change it back because that was the only thing that made my querries not work.

Rick

Rick If I have helped you just click the first link below to let me know :)
 
Yeah, nivly, which database are you using? I'd hate to think I've been giving you bum advice all this time.


ristmo2001, the following code works for me with MySQL 3.23.49 and PHP 4.2.1 through Apache 1.3.23 on RH 7.3:

[tt]<?php

$conn = mysql_connect (&quot;localhost&quot;, &quot;foo&quot;, &quot;foo&quot;);

mysql_select_db (&quot;test&quot;, $conn);

$query = &quot;UPDATE foo set b = now()&quot;;

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

if ($result == TRUE)
{
if (mysql_affected_rows ($conn) > 0)
{
print &quot;did it&quot;;
}
else
{
print &quot;nope&quot;;
}
}
else
{
print &quot;bad query&quot;;
}
?>[/tt]


If I give it a query that is well-formed, and it updates at least on row, then I get &quot;did it&quot;. If I give it a query that is well formed, but does not update any rows, I get &quot;nope&quot;. And if I hand the function a query that is not well formed (if, for example, it references a column that does not appear in the table), then I get &quot;bad query&quot;.

Everything works exactly as expected. Do you get something else? ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
Bad query:

I edited the code a bit:

Code:
$result = sql_query(&quot;update &quot;.$prefix.&quot;_guestbook set name='$entry_name', email='$entry_email', url='$entry_url', msn='$entry_msn', aim=$entry_aim', icq='$entry_icq', yahoo='$entry_yahoo', message='$entry_message', date='$entry_date', time='$entry_time', ip='$entry_ip' where id='$id')&quot;, $dbi);

$result2 = sql_query ($result2, $dbi);

if ($result2 == TRUE)
{
     if (mysql_affected_rows ($dbi) > 0)
     {
          print &quot;did it&quot;;
     }
     else
     {
          print &quot;nope&quot;;
     }
}
else
{
     print &quot;bad query&quot;;
}
 
Of course you would get that output. $result2 is not set to a valid SQL statement. It's null at the time you pass it into the function to query the database.


You need to troubleshoot the problem. I recommend you stop trying things at random to see what will work. The random-try method may solve the problem, but without some kind of methodology, you will never be able to learn from your mistakes.

I hope the last paragraph doesn't put you off. What follows is a basic troubleshooting flowchart that can help you get this function working.



You are passing a complicated string concatenation into a function, and that function is not reacting as predicted.

Here are the steps I recommend you follow:

1. Learn SQL. Learn the peculiarities of the particular database engine you are using. Understand what databases and tables are, and how to use them. Craft SQL statments by hand and run them against the database via it's administrative frontend.

Can you make an SQL-based server do what you need it to?

If and only if you pass this test, will you move to step 2.


2. Learn all the functions that apply to your particular database backend. Learn what each function expects as in put, and what it will produce as output, including how the functions will indicate errors. Be prepared to trap every possible error any function could hand you.

Do you have a thorough familiarity with all of the database functions for your database?

If and only if you pass this test, will you go to step 3.


3. Perform the string concatentation without passing it into the function. Then print the value produced by that concatenation to the screen. Examine it visually. Does it look to you to be a well-formed SQL statement? This test only takes two lines:

[tt]$query = &quot;update &quot;.$prefix.&quot;_guestbook set name='$entry_name', email='$entry_email', url='$entry_url', msn='$entry_msn', aim=$entry_aim', icq='$entry_icq', yahoo='$entry_yahoo', message='$entry_message', date='$entry_date', time='$entry_time', ip='$entry_ip' where id='$id')&quot;;

print $query;
[/tt]

Do not attempt to use it to query the database. Just examine the string visually on the screen.

If this test fails, then you must find out where the string does wrong and why. Likely causes of problems are that one of the subsidiary strings you are concatenating is not set to a correct value.

Does this string look right to you?

If and only if your string passes this test, will you go to step 4.


4. Test the string against the database using some method to issue the command directly. Take the printout from step 3, copy and paste it into some other piece of software which can issue that SQL statement against your database.

Make whatever changes to the code necessary to make the program produce a string that modifies your databases in the way you need them modified.

Does this SQL statement do what you want it to do?

If and only if the string you produce correctly modifies your database, will you go to step 5.


5. Make sure that the code where you initiate your database connection works properly. If your earlier code is not correctly opening a database connection, then anything you do in the function will not operate correctly, since you are using a global $dbi as your database connector.

Make modifications necessary to your code to insure that the connection opens correctly, and that correct database is selected.

Is your code opening a connection to the database correctly, and selecting the appropriate database correctly?

If and only if your code passes this test, will you go to step 6.


6. Place in your function any code necessary to issue the UPDATE statement. Don't reedit your code to move the concatenation into your query function. You've already proven that $query has the correct information. Use that variable in your query function.

Place all the code necessary to trap any errors that are produced by your use of the query function. The demo code I last gave you in this thread will do admirably.

Run the program and make sure that the code changes the database in the way you want.

Does the code do what you want it to?

If and only if your function passes this test, is it working.



Nivly, I admire your drive in attempting to learn PHP database operations. You are charging in a biting off huge chunks of knowledge. The problem is, you are having a hard time swallowing those chunks once you've bitten them off.

Start slow. Understand what you've done. Build complexity on your understanding. ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
1. I know the basic commands of SQL.

2. I also know the basic functions of the database and my script.

3. The string looks correct to me:

Code:
update guestbook set name='test', email='test', url='test', msn='test', aim=test', icq='test', yahoo='test', message='test', date='test', time='test', ip='test' where id='3')

4. No, the SQL statement doesn't function correctly.

Code:
$query = &quot;update guestbook set name='$entry_name', email='$entry_email', url='$entry_url', msn='$entry_msn', aim=$entry_aim', icq='$entry_icq', yahoo='$entry_yahoo', message='$entry_message', date='$entry_date', time='$entry_time', ip='$entry_ip' where id='$id')&quot;;

print $query;

$result=sql_query($query,$dbi);

I'm not sure if this is what you meant for me to do. :~/
 
Your query isn't correct. The aim field has a closing quote, but not an opening qoute.
Add it and the query should work. //Daniel
 
Thank you, danielhozac, I fixed it, but it still doesn't update the row.
 
you fixed it in both places, right? both aim's were wrong If I have helped you just click the first link below to let me know :)
 
What version of PHP are you using nivly?
if its later than 4.1.0 all your variables will come from $_GET[variable] not from $variable. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
ristmo2001 - There was only one in my script.

KarveR - My web host is using PHP 4.0.6.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top