Forum Moderators: coopster

Message Too Old, No Replies

Editing and updating Data

editing user information

         

m4tt

9:54 am on Jul 5, 2005 (gmt 0)

10+ Year Member



HI

I am trying to successfully edit data. Here is my script:

Can anyone steer my in the right direction? Thanks

<?php
include("db.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_GET['id']))
{
$query = "SELECT user_id, url ".
"FROM tbl_auth_user ".
"WHERE user_id = '{$_GET['id']}'";
$result = mysql_query($query) or die('Error : '. mysql_error());
list($user_id, $url) = mysql_fetch_array($result, MYSQL_NUM);

}
else if(isset($_POST['url']))
{
$url = $_POST['url'];

// update the user
$query = "UPDATE tbl_auth_user ".
"SET url = '$url' ".
"WHERE user_id = '$id'";
mysql_query($query) or die('Error : ' . mysql_error());

echo "User '$url' updated";

}

?>
<form method="post">
<input type="hidden" name="user_id" value="<?=$user_id;?>">
<table width="700" border="0" cellpadding="2" cellspacing="1" class="box">
<tr>
<td width="100">Url</td>
<td><input name="title" type="text" class="box" id="url" value="<?=$url;?>"></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="update" type="submit" class="box" id="update" value="Update Article"></td>
</tr>
</table>
<p align="center"><a href="cms-admin.php">Back to admin page</a></p>
</form>

dreamcatcher

11:04 am on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whats the problem exactly? Are you seeing errors? Can you give us a little more information please?

m4tt

11:19 am on Jul 5, 2005 (gmt 0)

10+ Year Member



Sorry, it lets me change the data and then I hit update and nothing happends.

m4tt

11:25 am on Jul 5, 2005 (gmt 0)

10+ Year Member



Basically, the script doesn't execute. I think it relates to this section:

}
else if(isset($_POST['url']))
{
$url = $_POST['url'];

// update the user
$query = "UPDATE tbl_auth_user ".
"SET url = '$url' ".
"WHERE user_id = '$id'";
mysql_query($query) or die('Error : ' . mysql_error());

echo "User '$url' updated";

}

But I can't work out why it doesn't update the value.

dreamcatcher

1:10 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your hidden form field is called 'user_id', so should your query in fact be:

WHERE user_id = '".$_POST['user_id']."'

dc

m4tt

9:44 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Thanks for that however I now get this error:

Parse error: parse error, unexpected T_STRING in /home/circle61/public_html/mattdesign/cust/admin/edit.php on line 43

// update the user
$query = "UPDATE tbl_auth_user ".
"SET title = '$url'".
"WHERE user_id = '".$_POST['user_id'].";
mysql_query($query) or die('Error : ' . mysql_error());

echo "User '$url' updated";

Line 43 is below mysql_error..

jatar_k

9:57 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you missing a quote or something at the end of this line?

"WHERE user_id = '".$_POST['user_id'].";

m4tt

10:40 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Tried that and got the same error..

m4tt

10:46 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Ok I have got that working but it still isn't updating.What I am trying to do is update a user's url, I am using the user_id as the id key so when editing it should go edit.php?id="admin" and then it offers a form with the url 'admin' for editing.

<html>
<head>
<title>Edit a user</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include("db.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_GET['id']))
{
$query = "SELECT user_id, url ".
"FROM tbl_auth_user ".
"WHERE user_id = '{$_GET['id']}'";
$result = mysql_query($query) or die('Error : '. mysql_error());
list($user_id, $url) = mysql_fetch_array($result, MYSQL_NUM);

}
else if(isset($_POST['url']))
{
$url = $_POST['url'];

// update the user
$query = "UPDATE tbl_auth_user ".
"SET title = '$url'".
"WHERE user_id = '".$_POST['user_id']."'";
mysql_query($query) or die('Error : ' . mysql_error());

echo "User '$url' updated";

}

?>
<form method="post">
<input type="hidden" name="user_id" value="<?=$user_id;?>">
Url: <input name="title" type="text" class="box" id="url" value="<?=$url;?>">
<input name="update" type="submit" class="box" id="update" value="Update User">
</form>
</body>
</html>

jatar_k

11:04 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what happens do you get an error?

have you tried outputting your update string to the browser and then pasting it into command line or phpmyadmin?

m4tt

11:20 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



No I haven't tried that, when you say output can you output it to display in the browser?

jatar_k

11:23 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$query = "SELECT user_id, url ".
"FROM tbl_auth_user ".
"WHERE user_id = '{$_GET['id']}'";

echo '<p>',$query;

or

$query = "UPDATE tbl_auth_user ".
"SET title = '$url'".
"WHERE user_id = '".$_POST['user_id']."'";

echo '<p>',$query;

I always output my queries to the browser when I am having trouble and then paste them into the commandline to test them right in mysql