Jump to content


Photo

Posting to database not working


  • Please log in to reply
6 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 06 September 2006 - 11:06 AM

Hi,

I have a page that updates the user's details into the database, I can get the page to display the data from the database so the user knows what to change but when I post the data back nothing gets actually put in the database. I get no errors either:

$id = $_SESSION['username'];
$query = "select * from users where username='$id'";

    //now we pass the query to the database 
	$result=mysql_query($query) or die("Could not get data.".mysql_error()); 

    //get the first (and only) row from the result 
    $row = mysql_fetch_array($result, MYSQL_ASSOC);

  $username=$row['username']; 
    $password=$row['password']; 
    $first_name=$row['first_name'];
	$maiden_name=$row['maiden_name'];
etc, etc

	if(isset($_POST["submit"])){    

    field_validator("first_name", $_POST["first_name"], "alphanumeric", 1, 15);
    field_validator("maiden_name", $_POST["maiden_name"], "alphanumeric", 1, 15);
//This function above is called from an include and works in the join form so I added it here

if(empty($messages)) {
        // registration ok, get user id and update db with new info:
        updateuser($strUsername = isset($_POST['username']) ? $_POST['username'] : "", 
		$strPassword = isset($_POST['password']) ? $_POST['password'] : "", 
		$strfirst_name = isset($_POST['first_name']) ? $_POST['first_name'] : "",
		$strmaiden_name = isset($_POST['maiden_name']) ? $_POST['maiden_name'] : "", 
etc);

html below here


ANy ideas on where I am going wrong?

Thanks,

G

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 September 2006 - 11:13 AM

wheres the update query then?
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 06 September 2006 - 11:33 AM

Its in a functions file and looks like so:

$query = "UPDATE `users` SET `first_name`='$first_name', `maiden_name`='$maiden_name', `last_name`='$last_name', etc

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 September 2006 - 11:35 AM

Take it out of the function and add it as one in the code as provided then see if you can add it to a function ok.

it is defently best to see if the update syntex works before haveing it as a function as there might be a few corrections need before the code gets added to a function ok.

we all crawl before we walk try ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 07 September 2006 - 09:13 AM

Ok so here is what I now have:

$id = $_SESSION['username'];
$query = "select * from users where username='$id'";

    //now we pass the query to the database 
	$result=mysql_query($query) or die("Could not get data.".mysql_error()); 

    //get the first (and only) row from the result 
    $row = mysql_fetch_array($result, MYSQL_ASSOC); 

    //now finally create variables with data we have got which will be used later 
    //we will set the value attribute of the appropriate form element to the value from the database 
    
        
    
    $username=$row['username']; 
    $password=$row['password']; 
    $first_name=$row['first_name'];
	$maiden_name=$row['maiden_name'];
	$last_name=$row['last_name']; 
    $address_line1=$row['address_line1']; 
    $address_line2=$row['address_line2']; 
    $town=$row['town']; 
    $county=$row['county'];
    $postcode=$row['postcode'];
    $daytime_phone=$row['daytime_phone'];
    $mobile_phone=$row['mobile_phone'];
    $evening_phone=$row['evening_phone'];

  
	if(isset($_POST["submit"])){    
/*field_validator("username", $_POST["username"], "alphanumeric", 4, 15);
    field_validator("password", $_POST["password"], "string", 4, 10);
    field_validator("confirmation password", $_POST["password2"], "string", 4, 10);*/
    field_validator("first_name", $_POST["first_name"], "alphanumeric", 1, 15);
    field_validator("maiden_name", $_POST["maiden_name"], "alphanumeric", 1, 15);
    field_validator("last_name", $_POST["last_name"], "alphanumeric", 1, 15);
    field_validator("address_line1", $_POST["address_line1"], "alphanumeric", 4, 15);
    field_validator("address_line2", $_POST["address_line2"], "alphanumeric", 4, 15);
    field_validator("town", $_POST["town"], "alphanumeric", 4, 15);
    field_validator("postcode", $_POST["postcode"], "alphanumeric", 4, 9);
    field_validator("daytime_phone", $_POST["daytime_phone"], "number", 1, 11);
    field_validator("mobile_phone", $_POST["mobile_phone"], "number", 1, 11);
    field_validator("evening_phone", $_POST["evening_phone"], "number", 1, 11);
    }

/*if(empty($messages)) {
        // registration ok, get user id and update db with new info:
        updateuser(*/$strUsername = isset($_POST['username']) ? $_POST['username'] : ""; 
		$strPassword = isset($_POST['password']) ? $_POST['password'] : "";
		$strfirst_name = isset($_POST['first_name']) ? $_POST['first_name'] : "";
		$strmaiden_name = isset($_POST['maiden_name']) ? $_POST['maiden_name'] : ""; 
		$strlast_name = isset($_POST['last_name']) ? $_POST['last_name'] : "";
		$straddress_line1 = isset($_POST['address_line1']) ? $_POST['address_line1'] : ""; 
		$straddress_line2 = isset($_POST['address_line2']) ? $_POST['address_line2'] : ""; 
		$strtown = isset($_POST['town']) ? $_POST['town'] : "";  
		$strcounty = isset($_POST['county']) ? $_POST['county'] : "";
		$strpostcode = isset($_POST['postcode']) ? $_POST['postcode'] : "";
		$strdaytime_phone = isset($_POST['daytime_phone']) ? $_POST['daytime_phone'] : "";
		$strmobile_phone = isset($_POST['mobile_phone']) ? $_POST['mobile_phone'] : "";
		$strevening_phone = isset($_POST['evening_phone']) ? $_POST['evening_phone'] : "";


$query = "UPDATE `users` SET `first_name`='$first_name', `maiden_name`='$maiden_name', `last_name`='$last_name', `address_line1`='$address_line1', `address_line2`='$address_line2', `town`='$town', `county`='$county', `postcode`='$postcode', `daytime_phone`='$daytime_phone', `mobile_phone`='$mobile_phone', `evening_phone`='$evening_phone'";

        $result=mysql_query($query, $link) or die("Died inserting login info into db.  Error returned if any: ".mysql_error()); 
        return true; 

As things stand the screen is blank when I load it, having debugged it it seems that the page is just running this last query to update the database. I am really unsure as to where to go from here...

Thanks,

G

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 11:17 AM

Maybe I'm missing something here, but shouldn't this...

`first_name`='$first_name',
`maiden_name`='$maiden_name',
`last_name`='$last_name',
etc... etc...

Be like this...

`first_name`='$strfirst_name',
`maiden_name`='$strmaiden_name',
`last_name`='$strlast_name',
etc... etc...

You've missed off the 'str' at the beginning of the variable name.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 07 September 2006 - 12:58 PM

I have added that, but no change...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users