changing a field

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
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

changing a field

Post by dull1554 »

heres my code

Code: Select all

if(!isset($_POST['submit'])){
Print <<< EOT

<form method="POST" action="index.php?l=admin.php&admin=usertype">
User name: &nbsp;<input type="text" name="user"><br>
User type: &nbsp; <select name=type>
<option value=user>USER</option>
<option value=mod>MOD</option>
<option value=admin>ADMIN</option>
<option value=frozen>FROZEN</option>
</select>
<input type="submit" value="Submit" name="submit">

</form>

EOT;
}
else{
$user = $_POST['user'];
$type = $_POST['type'];
$sql = mysql_query("INSERT INTO user (type)
		VALUES('$type') WHERE user='$user'");
if($sql){
    echo $user." was changed to or has become:  ".$type;
}
elseif(!$sql){
    echo $user." was not changed";
}
else{
    echo "Problem executing the script!";
}

}
in case you cant tell i want to change the "type" field where "user" is equal to $user, i don't think what i have is right....or if it is i got some problems

does anybody know how to do this?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

UPDATE user SET type='$type' WHERE user='$user'
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Instead of INSERT you want to use UPDATE, try this out:

Code: Select all

<?php
$sql = mysql_query("UPDATE user SET type='$type' WHERE user='$user'");
?>
Edit:
Damn you Mark :P
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

now my script is this

Code: Select all

if(!isset($_POST['submit'])){
Print <<< EOT

<form method="POST" action="index.php?l=admin.php&admin=usertype">
User name: &nbsp;<input type="text" name="user"><br>
User type: &nbsp; <select name=type>
<option value=user>USER</option>
<option value=mod>MOD</option>
<option value=admin>ADMIN</option>
<option value=frozen>FROZEN</option>
</select>
<input type="submit" value="Submit" name="submit">

</form>

EOT;
}
else{
$user = $_POST['user'];
$type = $_POST['type'];
$sql = mysql_query("UPDATE users SET type='$type' WHERE user='$user'");
if($sql){
    echo $user." was changed to or has become:  ".$type;
}
elseif(!$sql){
    echo $user." was not changed";
}
else{
    echo "Problem executing the script!";
}

}
but it still returns user type not changed, and i checked my database and nothing has changed
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$sql = mysql_query("UPDATE users SET type='$type' WHERE user='$user'") or die(mysql_error());
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Hmm, try seeing what the MySQL error is:

Code: Select all

<?php
$sql = mysql_query("UPDATE users SET type='$type' WHERE user='$user'") or die("Query error: mysql_error()");
?>
Edit:
AGAIN! Grrr :twisted:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

:?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i swear i need to sleep, it returned unknown colum name, so i checked the database, when i created the database rether the "user" i had "usr"

so it works super now.....thanks a mill guys
Post Reply