updating db with blank records?

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

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

updating db with blank records?

Post by C_Calav »

hi guys,

can anyone see anything wrong with this code?

when i update, it updates everything with blank records.

i have used a pretty much simmilar script for updating stock and it works fine.

thanx guys.

Code: Select all

<?php
    $db = mysql_pconnect('**', '**', '**') or die ("Could not connect to database");

    mysql_select_db('models') or die ("Could not select database!"); 

    if ($_SERVER['REQUEST_METHOD'] == "POST") 
    {

	$ud_C_First = stripslashes($_POST["ud_C_First"]);
	$ud_C_Last = stripslashes($_POST["ud_C_Last"]);
 	$ud_C_Email = stripslashes($_POST["ud_C_Email"]);		
	$ud_C_Phone = stripslashes($_POST["ud_C_Phone"]);
	$ud_C_Address1 = stripslashes($_POST["ud_C_Address1"]);	
	$ud_C_Address2 = stripslashes($_POST["ud_C_Address2"]);
	$ud_C_City = stripslashes($_POST["ud_C_City"]);
	$ud_C_Country = stripslashes($_POST["ud_C_Country"]);
	

	$query = "UPDATE customer SET C_First='$ud_C_First',C_Last='$ud_C_Last',C_Email='$ud_C_Email',C_Phone='$ud_C_Phone',C_Address1='$ud_C_Address1',C_Address2='$ud_C_Address2',C_Country='$ud_C_Country',C_City='$ud_C_City' WHERE C_ID='$C_ID'";

        #execute SQL statement
	$result = mysql_query($query, $db) or die (mysql_error()."<br />Couldn't execute query: $query");
	
	##mysql_query($query);

	print "<br><b>details updated</b><br><br>";
	echo"$C_ID";
	echo"$ud_C_First";
	echo"$ud_C_Last";
	echo"$ud_C_Email";
	echo"$ud_C_Phone";
	}
	
?>
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I'm guessing all the echos at the end return the correct results, and no errors show...

echo mysql_affected_rows($result);

See what that does...
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

yeah you are right Steveo31, i guess just checking, it gives the right results and no errors. but then i check the DB and all the fields are blank except for the ID which i didnt update.

will put echo mysql_affected_rows($result); in
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey Steveo31 what does is echo mysql_affected_rows($result); ment to tell me anyway?

i put it at the very bottm and gave me a error

Code: Select all

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /var/users/modelair/modelaircraft.co.nz/htdocs/update_details.php on line 366
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

it means that $result isn't a valid resource as in mysql doesn't know what $result is so there could be something wrong with your query that isn't updateing correctly.

echo out your query and run that directly agianst you db to make sure it is working and updating correctly because you may not be getting the values that you are expecting in your query.

This is just supposed to be updating one row at a time right?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi phpScott,

yip, updating just one row at a time.

i tested the sql out in MySql and it worked fine, updated the the row.

any other ideas?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

we could do something stupid like echo out $result to make sure we are getting something back from the query either true or false.
try using echo mysql_affected_rows($result); agian but right after where you submit the update. you should get something back
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

phpScott,

i have set my page up like this, just in case it helps.

--
php - select details

html

php - update
--

i put mysql_affected_rows($result); in again, same error, i added it in just after the submite button, (not sure if thats where you ment or not.

Code: Select all

<div id="buttons">
<input type="reset" value="Reset" class="btn" />
<input type="submit" value="Continue" class="btn" />
<?php mysql_affected_rows($result);?>
</div>
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

this is where the mysql_affected_rows() should go.

Code: Select all

<?php
$result = mysql_query($query, $db) or die (mysql_error()."<br />Couldn't execute query: $query");
echo mysql_affected_rows();
?>
sorry got the syntax wrong. I use a db class that stores it as one of the pieces of information so I forgot the right way of calling it.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey phpScott,

it gave me a 1 when i was on the update page,

then when i clicked update,

it gave me a 0 on the update page.

what does this tell you?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

It sounds to me that on the second update you are not getting the $C_ID value being passed. so it doesn't know which line to update.
Are you still getting that value when when you click on the update button or any values that you are echoing out after you click?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

no im not getting that value after i click the submit button, i guess thats where the problem is!

so do i just put the cartID into a hidden text box again and post to next page?
----

can you check please if im doing this the right way?
cart > echo cartID in a hidden text box

details > get cartID, echo into hidden text box again

summary > get cartID, echo into hidden text box again

update_deatils > get cartID
can you see what im trying to do?
i do all this just so i know the usser's deatils and cart details.

is there any easier way of doing.. seems like im repeating alot.

thanx for you help phpScott, will check in morning off to bed, damn work!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

without see how the rest of the page looks i would have to say yes.
In the form with the update button you will have to

Code: Select all

<?php

<input type="hidden" name="cartID" id="cartID" value="<?php echo $cartID;?>" />
?>
only because the values are coming in as $_POST variables.

But if the values are being updated as the page loads the first time is this page allowing them to do more updates to their account and that is the reason for the update button?
Post Reply