-1

I am trying to overwrite the current data in MySQL to be able to update everything. I am new to this I don't see any errors with the below code:

PHP code:

<?php
// see if the form has been completed
if (isset($_POST['submit'])){
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];

    if($firstname && $surname){
        // connect to the server
        include_once("php_includes/db_conx.php");

        // check if that user exist 
        $exists = mysql_query ("SELECT * FROM users WHERE firstname='$firstname'") or die ("the query could not be connected");
        if (mysql_num_rows ($exists) != 0) {
            // update the description in the database
            mysql_query("UPDATE firstname SET surname='$surname' WHERE firstname='$firstname'") or die ("update could not be applied");
            echo "successful";
        } else echo "the name does not edist";  
    } else echo "you need to enter both of the fields try again:";
}
?>  

The error I get is

The query could not be connected

but I tried the query and it is fine.

HTML:

<html>
<head>
<title>update MySql form</title>
</head>
<body>

<div id="pageMiddle">  

    <form action="user1.php" method="POST">
        <div>
        <p>First Name: <input type="text" name="firstname" id="firstname" ></p>
        <p>Surname: <input type="text" name="surname" id="surname"></p>
        <p><input type="submit" name="submit" id="submit" value="Update Description"></p>
        </div>
    </form>

</body>
</html>
Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
user3311898
  • 113
  • 1
  • 3
  • 11

1 Answers1

0

Your table is called 'users' but you are running UPDATE on firstname. Change it to:

UPDATE users SET surname...

Then do

$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'")

in order to isolate whether you have value for that variable. I would recommend printing the query string for testing purposes.

Megan Squire
  • 1,001
  • 7
  • 18