happypete Posted March 9, 2019 Share Posted March 9, 2019 Hi, I'm trying to edit some database fields, I have text1, text2, text3, text4, text5, text6 etc.. They are displayed on the index.php page, with an edit link so the user can choose which set to edit // Extract details from database $sql = "SELECT * FROM data WHERE id=1"; $stmt = $db->prepare($sql); $stmt->execute(); $e = $stmt->fetch(); <h1><?php echo $e['text1']) ?></h1> <p><?php echo ($e['text2']); ?></p> <p><a href="edit.php">EDIT</a></p> <h1><?php echo $e['text3']) ?></h1> <p><?php echo ($e['text4']); ?></p> <p><a href="edit.php">EDIT</a></p> <h1><?php echo $e['text5']) ?></h1> <p><?php echo ($e['text6']); ?></p> <p><a href="edit.php">EDIT</a></p> edit.php: // Extract details from database $sql = "SELECT * FROM data WHERE id=1"; $stmt = $db->prepare($sql); $stmt->execute(); $e = $stmt->fetch(); <form method="post" action="process.php" enctype="multipart/form-data"> <label>Page Title <input type="text" name="text1" maxlength="90" value="<?php echo $e['text1'] ?>" /> </label> <br> <label>Title Text</label> <textarea name="text2"><?php echo $e['text2'] ?></textarea> <input id="button" type="submit" name="submit" value="Save Changes" /> and then update them: process.php $sql = "UPDATE data SET text1=?, text2=? WHERE id=1 LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['text1'], $_POST['text2'] ) ); $stmt->closeCursor(); Question: How can I pass the form values dynamically from the index.php page so I don't have to hard code text1, text2 etc into the edit.php and process.php page and have a different update & process page for each set of data? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 9, 2019 Share Posted March 9, 2019 You could POST the following. Be sure to validate. Array ( [text1] => foo [text2] => bar [otherField] => bla [fields] => Array ( [0] => text1 [1] => text2 ) ) Quote Link to comment Share on other sites More sharing options...
happypete Posted March 9, 2019 Author Share Posted March 9, 2019 2 minutes ago, NotionCommotion said: You could POST the following. Be sure to validate. Array ( [text1] => foo [text2] => bar [otherField] => bla [fields] => Array ( [0] => text1 [1] => text2 ) ) thanks for taking to the time to answer, but that went straight over my head! I don't understand where to use this? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 9, 2019 Share Posted March 9, 2019 the example you have shown implies you have sets of same meaning data stored in a single row in the database table. this is a bad design, resulting in more code/queries to manage the data. research database normalization to find out how to properly store data. each set of data should be stored in a separate row in the database table, with an auto increment id column. this will establish a unique identifier that you can use when dynamically building the edit links. the id from the link would be used in the edit.php code to retrieve the correct row of data to populate the form fields with. the id would be passed in a hidden field in the form. the process.php code would detect that a post method form has been submitted, enforce any user 'edit' or ownership permissions, validate the input data, then use the input data when executing the update query. Quote Link to comment Share on other sites More sharing options...
happypete Posted March 9, 2019 Author Share Posted March 9, 2019 Thanks for the comments, I will do some more research. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.