Tim C
Tim C

Reputation: 5714

Mysql update using php variable giving error

I hope someone can help me with this one

I have a page displaying different members of a sports team when user clicks on a specific player a new page opens 'edit.php' where the selected(clicked) players info is displayed in a form. (Information such as name, lastname, phone nr and email is contained within the form)

Lets say the players name changed (very unlikely but just an example) the coach can update it. When he clicks the submit button the database should get updatated with the new information

However when I change the name to John and click submit I get the following error: Query failed:Unknown column 'John' in 'field list'

Ive spend the last couple of hours debugging this with no luck, would greatly appreciate it if someone can point me in the right direction. Code follows:

    if(isset($_POST['submit'])){
$firstname = $_POST['firstname'];

$query = "UPDATE player_info SET name =".$firstname." WHERE player_id =".$selectedplayer;
mysql_query($query)
or die ("Query failed:" .mysql_error());
}//end if

else
{
echo '<form name="update" method="post">';
while($row =mysql_fetch_array($result)){

 $name = $row['name'];
 $surname = $row['surname'];
 $phone = $row['contact_number'];
 $email =$row['email'];
 $position = $row['position'];  

echo "Name: <input type=\"text\" value= '$name' name='firstname' /><br>";
echo "Last Name: <input type=\"text\" value= '$surname' /><br>";
echo "Phone nr: <input type=\"text\" value= '$phone' /><br>";
echo "Email: <input type=\"text\" value= '$email' /><br>";
echo "Position: <input type=\"text\" value= '$position' /><br>";    
} //end while
} //end else

echo'<input type="submit" name="submit">';
echo'</form>';

Upvotes: 0

Views: 190

Answers (2)

George
George

Reputation: 36794

You need some quotes around your $firstname variable. (Where your field isn't an integer):

$querie = "UPDATE player_info SET name ='".$firstname."' WHERE player_id =".$selectedplayer;

I'm assuming you are using 'John' in your input. Because you've left out the quotes, your MySQL looks like SET name=John and since MySQL can't find 'John' as a field, it tells you.

Never-the-less:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Upvotes: 5

sharafatalee
sharafatalee

Reputation: 134

Try with quotes around the name like this

'".$firstname."'

$query = "UPDATE player_info SET name ='".$firstname."' WHERE player_id =".$selectedplayer;

Upvotes: 1

Related Questions