Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If that is just school assignment, and You don't need to worry about security and real world problems with it, then single file will do.</p> <ol> <li><p>Do an IF statement and check if data is submitted (you can look at $_POST variable), use isset() and strlen() functions to make sure data is in the arrays</p></li> <li><p>Use PHP PDO object to connect and talk with database see <a href="http://www.php.net/manual/en/book.pdo.php" rel="nofollow noreferrer">http://www.php.net/manual/en/book.pdo.php</a>, see the example and comments</p></li> <li><p>If there are data submitted, You can save them with $pdo->query('insert ...');, You will need to learn some basic SQL as well, check <a href="http://www.sqlcourse.com/insert.html" rel="nofollow noreferrer">http://www.sqlcourse.com/insert.html</a></p></li> <li><p>at any case, fetch all the data from the database, and display them in a table, with "edit" link. </p></li> <li><p>Let the edit link, target your process.php file, with a param, for example process.php?id=1, ect., the id param should refer to the row id in the database (now You know your rows should have id column)</p></li> <li><p>in Your process.php file, look at the $_GET['id'] param, to see if there is id given. If there is, use $pdo->query("select ... where id = {$_GET['id']}") to fetch the data, check <a href="http://www.sqlcourse.com/select.html" rel="nofollow noreferrer">http://www.sqlcourse.com/select.html</a>, and the pdo php manual on how to use it.</p></li> <li><p>if there was an ID, prepopulate Your form with the data, and add extra hidden input in the form, that will contain the ID of row to update.</p></li> <li><p>In process.php, if there is $_POST and it has an ID, update the row with $pdo->query('update ...').</p></li> <li><p>Look at Your IF statements, arrange them in logical flow, You will always need to open database connection, You will always need to check $_POST and $_GET params, so do those things before IFs</p></li> <li><p>Be sure to read the php manual, read the comments as well. Read the MySql docs. It takes time, but it's time well spent.</p></li> <li><p>Play as You code, that's the best way to learn :)</p></li> </ol> <p>If this is for production, or You want to do things right, use $pdo->quote, to make sure You are not passing something wrong to Your database.</p> <p>EDIT:</p> <p>You are on the right track, now combine it. Look at the following code (You might need to debug it, haven't tested)</p> <pre><code>&lt;?php $host = "localhost"; $username = "root"; $password = ""; $database = "my_db"; $dsn = "mysql:host=$host;dbname=$database"; TRY { $conn = new PDO( $dsn, $username, $password ); $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (isset($_POST['submit'])) { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $gender = $_POST['gender']; $Console = $_POST['Console']; if (isset($_POST['id'])) { //update mode, we have both POST data and ID, update the record $id = $_POST['id']; $sql = "UPDATE userprofile SET" . "firstname=".$conn-&gt;quote($firstname) . ",lastname=".$conn-&gt;quote($lastname) . ",sex=".$conn-&gt;quote($gender) . ",console=".$conn-&gt;quote($Console) . " WHERE id = ".$conn-&gt;quote($id); $userdata = $conn-&gt;query($sql); } else { // insert mode, there is no ID, but there are data, insert them as new $sql = "INSERT INTO userprofile(" . "firstname, lastname, sex, console" . " ) VALUES (" . $conn-&gt;quote($firstname)."," . $conn-&gt;quote($lastname)."," . $conn-&gt;quote($gender)."," . $conn-&gt;quote($Console).")"; $userdata = $conn-&gt;query($sql); } } elseif (isset($_GET['id'])) { // edit mode, no POST data, but there is an ID param, prepopulate the form $userEditDataRows = $conn-&gt;query('SELECT * FROM userdata WHERE id ='.$conn-&gt;quote($_GET['id'])); if (sizeof($userEditDataRows)&gt;0) { $row = $userEditDataRows[0]; $firstname = $row['firstname']; $lastname = $row['lastname']; $gender = $row['sex']; $Console = $row['console']; $id = $_GET['id']; } } else { // set empty data $firstname = ''; $lastname = ''; $gender = ''; $Console = ''; $id = false; } //build the table $sql = "SELECT * FROM userdata"; $userdata = $conn-&gt;query($sql); $table = '&lt;table&gt;'; $table .= '&lt;tr&gt;'; $table .= '&lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;Gender&lt;/th&gt; &lt;th&gt;Console&lt;/th&gt; &lt;th&gt;Edit&lt;/th&gt;'; $table .= '&lt;/tr&gt;'; foreach ($userdata as $userdata) { $table .= '&lt;tr&gt;'; $table .= ' &lt;td&gt;' . $userdata['firstname'] . '&lt;/td&gt;'; $table .= ' &lt;td&gt;' . $userdata['lastname'] . '&lt;/td&gt;'; $table .= ' &lt;td&gt;' . $userdata['gender'] . '&lt;/td&gt;'; $table .= ' &lt;td&gt;' . $userdata['Console'] . '&lt;/td&gt;'; $table .= ' &lt;td&gt;&lt;a href="/process.php?id='.$userdata['id'].'"&gt;Edit&lt;/a&gt;&lt;/td&gt;'; $table .= ' &lt;/tr&gt; '; } $table .= '&lt;/table&gt;'; } catch (PDOException $e) { exit("Connection failed: " . $e-&gt;getMessage()); } ?&gt; &lt;!Doctype html public&gt; &lt;html&gt; &lt;body&gt; Please fill out the following form: &lt;table border="1" cellpadding="10"&gt; &lt;td&gt; &lt;h1&gt; Games Console Survey &lt;/h1&gt; &lt;form action="createProfile.php" method = "post"&gt; First Name: &lt;br /&gt; &lt;input type="text" name="firstname" value="&lt;?php $firstname ?&gt;" /&gt;&lt;br /&gt; &lt;br /&gt; Surname: &lt;br /&gt; &lt;input type="text" name="lastname" value="&lt;?php $lastname ?&gt;" /&gt; &lt;br /&gt; &lt;br /&gt; &lt;u&gt;Gender&lt;/u&gt;: &lt;br /&gt; &lt;br /&gt; &lt;input type="radio" name="gender" value="Male" &lt;?php if($gender == 'Male') echo "checked=checked"; ?&gt; /&gt; Male&lt;br /&gt; &lt;input type="radio" name="gender" value="Female" &lt;?php if($gender == 'Feale') echo "checked=checked"; ?&gt;/&gt; Female &lt;br /&gt; &lt;br /&gt; &lt;u&gt;I Have The Following:&lt;/u&gt; &lt;br /&gt; &lt;br /&gt; &lt;input type="checkbox" name="Console" value="Playstation 3" &lt;?php if($Console == 'Playstation 3') echo "checked=checked"; ?&gt; /&gt; Playstation 3&lt;br /&gt; &lt;input type="checkbox" name="Console" value="Xbox 360" &lt;?php if($Console == 'Xbox 360') echo "checked=checked"; ?&gt; /&gt; Xbox 360 &lt;br /&gt; &lt;input type="checkbox" name="Console" value="Wii" &lt;?php if($Console == 'Wii') echo "checked=checked"; ?&gt; /&gt; Wii &lt;br /&gt; &lt;?php if($id !== false):?&gt; &lt;input type="hidden" name="id" value="&lt;?php echo $id; ?&gt;"/&gt; &lt;?php endif; ?&gt; &lt;br /&gt; &lt;input type="submit" name="submit"/&gt; &lt;/form&gt; &lt;/td&gt; &lt;/table&gt; &lt;h1&gt;Current data:&lt;/h1&gt; &lt;?PHP echo $table ?&gt; &lt;/body&gt; &lt;/html&gt; </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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