Jump to content

Passing variable post names with php


happypete

Recommended Posts

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.