Jump to content
happypete

Passing variable post names with php

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.

Share this post


Link to post
Share on other sites

You could POST the following.  Be sure to validate.

Array
(
    [text1] => foo
    [text2] => bar
    [otherField] => bla
    [fields] => Array
        (
            [0] => text1
            [1] => text2
        )

)

 

Share this post


Link to post
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?

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.