1 (edited by Peter 2007-10-14 19:20)

Topic: Basic Mysql update script?

I'm trying to create a myaccount.php file to let users (members in my heavily customized PunBB) update their personal information.

I don't know PHP and can't get this to work:

if (empty($_POST['cmdUpdate']))
{
$sql = "UPDATE members SET firstname='$txtfirstname', surname='$txtsurname' WHERE id = '.$pun_user['id']'";
$result = mysql_query($sql) or die("SQL Update failed");
}

$result = $db->query('SELECT * FROM members WHERE id = '.$pun_user['id']) or error('Unable to fetch user data', __FILE__, __LINE__, $db->error());
$row = $db->fetch_assoc($result);

?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<p>First Name:<input type="text" name="txtfirstname" size="30" value="<? echo $row['firstname']; ?>" />
<p>Last name:<input type="text" name="txtsurname" size="30" value="<? echo $row['surname']; ?>" />
<p><input type="Submit" value="Update" name="cmdUpdate" />
</form>

I keep getting Syntax errors for this part: "if (empty ... Update failed");}". There's lots of regular punbb above this code and the rest works, so ignore those issues.

Any suggestions? If I can get some basic principles to work I should be able to figure out the rest.

And please don't tell me I should just use profile.php....

2

Re: Basic Mysql update script?

Just use profile.php. big_smile

Try altering that $sql line to:

$sql = 'UPDATE members SET firstname='.$txtfirstname.', surname='.$txtsurname.' WHERE id = '.$pun_user['id'];

3

Re: Basic Mysql update script?

Also, are you setting the vars correctly? i.e:

$txtfirstname = '\''.$db->escape($_POST['txtfirstname']).'\'';
$txtsurname = '\''.$db->escape($_POST['txtsurname']).'\'';

(I believe that's the correct syntax). smile

4 (edited by Peter 2007-10-15 16:22)

Re: Basic Mysql update script?

Excellent. Thanks MattF!

The syntax errors disappear.

I had to put a ! back into this line:

if (!empty($_POST['cmdUpdate']))

And yes, I had to add those vars settings as well.

It now works!

Still have a lot of puzzle work to do, to get user friendly messages - update succesful! - and integrate parts from profile.php etc. but this is a major basic principle that will make the rest a lot easier.

smile Thanks again!

5

Re: Basic Mysql update script?

You're welcome. smile

Might I also suggest you stop using short tags and use the full <?php tag? That shorthand may bite you in the backside one day. big_smile

6

Re: Basic Mysql update script?

I tried this but I just keeping getting error, unable to fetch user data...

7

Re: Basic Mysql update script?

That script above is only part of an overall setup. As it is above, it wouldn't work within PunBB as is.

8

Re: Basic Mysql update script?

MattF wrote:

That script above is only part of an overall setup. As it is above, it wouldn't work within PunBB as is.

ok, I have been searching a lot, what do I have to add, the actual connection part or is that defined in a page already?

9

Re: Basic Mysql update script?

What exactly are you wanting to do?

10

Re: Basic Mysql update script?

I was justing making a game, and making a page for updating your stuff and buying troops, just wanted to use punbb framework for it