Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First and foremost, you should be warned that your code is completely vulnerable against <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="nofollow">sql injections</a>. <a href="http://us2.php.net/manual/en/function.mysql-real-escape-string.php" rel="nofollow">Escaping</a> your POST data before inserting it into the database is a good start in protecting your database.</p> <p>Also, learning the <a href="http://us2.php.net/manual/en/ref.mysql.php" rel="nofollow">mysql extension</a> is useless for new systems because it is deprecated. You might think about looking into the <a href="http://www.php.net/manual/en/book.pdo.php" rel="nofollow">PDO</a> interface or the <a href="http://us3.php.net/manual/en/book.mysqli.php" rel="nofollow">mysqli</a> extension. There are many <a href="http://webdevrefinery.com/forums/topic/4995-beginners-guide-to-pdo/" rel="nofollow">beginner tutorials</a> for both and you will gain much more. </p> <h3>Now, as for your error</h3> <p>Make sure you are defining which ID you want to update in your database. In your second block of code you have:</p> <pre><code>//get the variables we transmitted from the form $id = $_POST['']; </code></pre> <p>needs to change to:</p> <pre><code>$id = $_POST['id']; </code></pre> <p>You said you get the error even if you change post to <code>$id = $_POST['ID']</code>, but if you look at your form, the id input has <code>name = 'id'</code> and PHP is case sensitive.</p> <p>Now, in your sql query, all of those back ticks are unnecessary. Also, there is no point in specifying which table ID because this is all being done in ONE table, TestTable. </p> <pre><code>//replace TestTable with the name of your table $sql = "UPDATE TestTable SET FName = '$fname',LName = '$lname', PHON = '$phon' WHERE ID = '$id' LIMIT 1"; </code></pre> <p><strong>EDIT:</strong> Although the query above is syntactically correct, you should consider using mysqli or PDO due to reasons mentioned above. Below are examples using mysqli and PDO. </p> <h2>Mysqli</h2> <p><a href="http://php.net/manual/en/book.mysqli.php" rel="nofollow">mysqli Manual</a> </p> <pre><code>/* connect to the database */ $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli-&gt;connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli-&gt;connect_errno . ") " . $mysqli-&gt;connect_error; } /* build prepared statement */ $stmt = $mysqli-&gt;prepare("UPDATE TestTable SET FName=?, LName=?, PHON=? WHERE ID=?)"); /* bind your parameters */ $stmt-&gt;bind_param('sssi', $fname, $lname, $phon, $id); /* execute prepared statement */ $stmt-&gt;execute(); /* close connection */ $stmt-&gt;close(); </code></pre> <h2>PDO</h2> <p><a href="http://php.net/manual/en/book.pdo.php" rel="nofollow">PDO Manual</a></p> <pre><code>/* connect to the database */ $dbh = new PDO('mysql:host=localhost;dbname=database', $user, $pass); /* build prepared statement */ $stmt = $dbh-&gt;prepare("UPDATE TestTable SET FName = :fname, LName = :lname, PHON = :phon WHERE ID = :id"); /* bind your parameters */ $stmt-&gt;bindParam(':fname', $fname); $stmt-&gt;bindParam(':lname', $lname); $stmt-&gt;bindParam(':phon', $phon); $stmt-&gt;bindParam(':id', $id); /* update one row */ $fname = 'John'; # or use your $_POST data $lname = 'Doe'; $phon = '123-456-7890'; $id = 1; /* execute prepared statement */ $stmt-&gt;execute(); /* use it again!1! */ $fname = 'Jane'; $lname = 'Doe'; $phon = '123-456-7890'; $id = 2; /* execute prepared statement */ $stmt-&gt;execute(); /* close connection */ $dbh = null; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload