Hector
Hector

Reputation: 682

Update Mysql column field based on email address

My DB has columns: ID, first_name, email, password, level

I have a form that i am trying to update the 'level' column based on the 'email address' entered of the existing user.

Right now i have a basic form that just inserts the info, but i need it to update existing users based on the email value.

This is what i have

<form action="update.php" method="post">

<input type="hidden" name="action" value="update" />

<fieldset>
<label  for="email" />Email Address:</label>
<input  value="" type="text" name="email" id="email" />
<label  for="level" />Level:</label>
<input  value="vip" type="text" name="level" id="level" />
<input class="button" type="image" src="/img/right/get-started-button.png" />
</fieldset>
</form>

----update.php------

<?php
$email = $_POST['email'];
$level = $_POST['level'];

mysql_connect ("localhost", "username", "pass") or die ('Error: ' . mysql_error());
mysql_select_db ("db_name");

$query="INSERT INTO users (email, level)VALUES ('".$email."','".$level."')";

mysql_query($query) or die ('Error updating database');

echo "Database Updated With: " .$email. " ".$level ;

?>

Upvotes: 2

Views: 3976

Answers (2)

Drakkainen
Drakkainen

Reputation: 1142

I'm not sure If i uderstand your question correctly, but if you are looking for the sql update:

    UPDATE users Set level='some_value' WHERE email="some_email_address"

So you could do:

    $query="UPDATE users SET level='" .$level."' WHERE email='" .$email."'";

That is if I understood your question correctly. As in you are trying to update an existing table, based on the email address typed into the form.

Upvotes: 1

jdarling
jdarling

Reputation: 2518

Not knowing what version of MySQL your using, you can use INSERT ON DUPLICATE KEY UPDATE syntax if your on 5+: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

If your using an older version then a simple select id limit 1 should suffice to find if the record exists.

BTW: you should be using mysql_real_escape_string (or similar) before you execute your sql statement. Its also a good idea to always use back ticks ` around your field names just in case you hit a reserved word or invalid symbol in your field names.

Upvotes: 2

Related Questions