Note that there are some explanatory texts on larger screens.

plurals
  1. POmySQL php update
    primarykey
    data
    text
    <p>How can I update a row in my mySql database from a HTML form. I have tried every technique and nothing seems to work. I would like that users could update their own profile page information.</p> <p>I have a form on my page but the data doesn't get sent through.</p> <p>What am i missing?</p> <p>Here is my code:</p> <p>------------INDEX.php</p> <pre><code>&lt;?php require_once("inc/database.php"); require_once("inc/query.php"); ?&gt; &lt;div class="wrapper"&gt; &lt;div class="content"&gt; &lt;h1&gt;User Profiles&lt;/h1&gt; &lt;?php while ($row = $results-&gt;fetch()) { $id = ($row["id"]); $name = ($row["name"]); $age = ($row["age"]); $password = ($row["password"]); print '&lt;div ' . 'class= id-' . ($id) . '"&gt;'; print "&lt;p&gt;" . ($name) . "&lt;/p&gt;"; print "&lt;p&gt;" . ($password) . "&lt;/p&gt;"; print "&lt;p&gt;" . ($age) . "&lt;/p&gt;"; print "&lt;/div&gt;"; } ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;form action="inc/addnew.php" method="post"&gt; &lt;p&gt;Name: &lt;input type="text" name="name" required&gt;&lt;/p&gt; &lt;p&gt;ID: &lt;input type="text" name="id" value="&lt;?php echo $id; ?&gt;"&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="submit" value="Lisää"&gt;&lt;/p&gt; &lt;/form&gt; </code></pre> <p>------------QUERY.php</p> <pre><code>&lt;?php try{ $results = $db-&gt;query("SELECT name, password, age, id FROM users"); $results-&gt;execute(); // echo "Our query ran successfully."; } catch (Exception $e){ echo "Data could not be retrived from the database."; exit; } </code></pre> <p>------------DATABASE.php</p> <pre><code>&lt;?php try{ $db = new PDO('mysql:host=localhost;dbname=user_profile;port=8889', 'User_profile','bFeLcZjMmVw4PBaF'); $db-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db-&gt;exec("SET NAMES 'utf8'"); } catch (Exception $e){ echo "Could not connect to the database."; exit; } </code></pre> <p>------------UPDATE.php</p> <pre><code>&lt;?php require_once("database.php"); if( isset( $_POST['name'] ) &amp;&amp; strlen( $_POST['id'] )){ $id = $_POST['id']; $name = $_POST['name']; $results=("UPDATE users SET name='$name' WHERE id=$id"); } header("Location: ../index.php"); } else { //error either $_POST['login'] is not set or $_POST['login'] is empty form field echo 'Name or ID field was empty. Please fill out those fields. &lt;a href="../index.php"&gt;Back to site&lt;/a&gt; &lt;br&gt;'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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