Jump to content

Posting to database not working


genista

Recommended Posts

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:

[code=php:0]
$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
[/code]


ANy ideas on where I am going wrong?

Thanks,

G
Link to comment
https://forums.phpfreaks.com/topic/19885-posting-to-database-not-working/
Share on other sites

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.
Ok so here is what I now have:

[code=php:0]
$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;
[/code]

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
Maybe I'm missing something here, but shouldn't this...

[code]
`first_name`='$first_name',
`maiden_name`='$maiden_name',
`last_name`='$last_name',
etc... etc...
[/code]

Be like this...

[code]
`first_name`='$strfirst_name',
`maiden_name`='$strmaiden_name',
`last_name`='$strlast_name',
etc... etc...
[/code]

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

Regards
Rich

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.