Reputation: 189
I have a table which loads data from mysql per user. Each user will be allowed to change his own data. How can I achieve this?
I tried with several possibilities:
1) Using a "$_GET" and pass the variables via URL
<a href="<? echo ''.$web.'?change='.$show[id].'&beschreibung='.$beschreibung.'&testimonial='.$testimonial.'' ?>">
where $beschreibung & $testimonial are variables from a Text Area. But only "$_GET[change]" -> the ID will be passed to my "if $_GET..."
2) I tried with a button:
<td><? echo '<button type="submit" value="' . $show['id']. '" name="change">Ändern?</button>'; ?></td>
But only the ID will be passed. Even if I send via "POST" (because the form is a type: POST) only the ID will be passed. $testimonial, $beschreibung are empty.
I set those two variables like this:
<td><textarea id='testimonial' name ='testimonial' rows='16' cols='30' style="resize: none;"><?php echo utf8_decode($show['testimonial']); ?></textarea></td>
<td><textarea id='beschreibung' name='beschreibung' rows='16' cols='30' style="resize: none;"><?php echo utf8_decode($show['beschreibung']); ?></textarea></td>
How can I pass the input of $testimonial, $beschreibung via URL, SUBMIT to my SQL Statement?
Here is the complete code:
if (isset($_POST['change'])) {
$referenz = $_POST['testimonial'];
$beschreibung = $_POST['beschreibung'];
$id_change = $_POST['change'];
$project = $_POST['project'];
echo $_GET['project'];
$db = mysql_connect("xxxxxx", "xxxx", "xxxx");
mysql_select_db("xxxx",$db);
$sql = "Update wp_awa_upload set beschreibung = '$beschreibung', testimonial = '$referenz', project= '$project' where id = '$id_change'";
$result = mysql_query($sql, $db);
echo $sql;
//$url = 'http://www.austrianweddingaward.at/awa/';
//echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
<table width="800" border="0" align="center" class="box-white">
<thead>
<tr>
<td colspan="4">
</td>
</tr>
<tr>
<th width="96">Kategorie</th>
<th width="299">Beitrag</th>
<th width="100">Name</th>
<th width="299">Referenz</th>
<th width="299">Beschreibung</th>
<th width="299">YouTube</th>
<th width="210"><? if ($rolle == '1') { echo 'Bewertung'; } elseif ($rolle== '9') { echo 'Freischalten';} else { echo 'Status';} ?></th>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
<? while ($show = mysql_fetch_array($abfrage)) { ?>
</tr>
</thead>
<tbody>
<tr>
<td><? echo $show['parent_cat']; ?></td>
<td><a href="<? echo $show['url']; ?>" data-lightbox="<? echo $show['file_name']; ?>" data-title="<? echo $show['file_name']; ?>"><img src="http://www.austrianweddingaward.at/awa/images/Vorschaubild-01.png" width="150" height="150" border="0"><br></a></td>
<td><? echo $show['file_name']; ?></td>
<td><textarea rows='16' cols='30' readonly style="resize: none;"><?php echo utf8_decode($show['testimonial']); ?></textarea></td>
<td><textarea rows='16' cols='30' readonly style="resize: none;"><?php echo utf8_decode($show['beschreibung']); ?></textarea></td>
<td><? echo "<a href='".$show['youtube']."'>Youtube</a>"; ?></td>
<td><? if ($rolle == '1') { ?>
<select name="rating" class="style28" style="width: 120px" id="rating">
<option value="0">0</option>
<option value="5">5</option>
<option value="15">15</option>
<option value="100">100</option>
</select> <? echo '<button type="submit" value="' . $show['id']. '" name="submit">Bewerten?</button>'; }
elseif ($rolle == 9) {
echo '<button type="freigeben" value="' . $show['id']. '" name="freigeben">Freigeben?</button>';
}?></td>
</tr>
</tbody>
<? }
}?>
</table>
</form>
Upvotes: 0
Views: 68
Reputation: 277
Show us the whole code instead of small pieces. Problem may stand somewhere else. anyway, here is my try:
To pass args by GET params in a link, make sure to "urlencode" your content:
<a href="<? echo $web.'?change='.urlencode($show[id]).'&beschreibung='.urlencode($beschreibung).'&testimonial='.urlencode($testimonial).'' ?>">
Then in php, retrieve your data with the $_GET array:
var_dump($_GET);
To pass args by POST in a form, make sure to put all your fields inside the <form></form>
tags.
<form action="" method="POST">
<table><tr>
<td><? echo '<button type="submit" value="' . $show['id']. '" name="change">Ändern?</button>'; ?></td>
<td><textarea id='testimonial' name ='testimonial' rows='16' cols='30' style="resize: none;"><?php echo $show['testimonial']; ?></textarea></td>
<td><textarea id='beschreibung' name='beschreibung' rows='16' cols='30' style="resize: none;"><?php echo $show['beschreibung']; ?></textarea></td>
</tr></table>
</form>
Then in php, retrieve your data with the $_POST array:
var_dump($_POST);
If there is more than one "testimonial" and "beschreibung" fields on the page (looks like you are building an html table in a loop...), you'll have to name those two fields with brackets to make them array:
<td><textarea id='testimonial' name ='testimonial[<?php echo $show['id'] ?>]' rows='16' cols='30' style="resize: none;"><?php echo $show['testimonial']; ?></textarea></td>
<td><textarea id='beschreibung' name='beschreibung[<?php echo $show['id'] ?>]' rows='16' cols='30' style="resize: none;"><?php echo $show['beschreibung']; ?></textarea></td>
Once again, you'll see the structure of the data by dumping it in your php code
var_dump($_POST);
Upvotes: 1
Reputation: 34232
This is what HTML forms are for. Wrap the record into a html form, include the id in a hidden input control and submit the form with a submit button. Since there are descriptive fields in your record, I would suggest you to use post instead of get, just in case you need to send too much data to the server.
<form method="post" action="abc.php">
<input type="hidden" name="userid" value="<?php echo utf8_decode($show[id]) ?>">
<textarea name="beschreibung"><?php echo $show['beschreibung'] ?></textarea>
<textarea name="testimonial"><?php echo utf8_decode($show['testimonial']); ?></textarea>
<input type="submit" value="Senden">
</form>
On the server side (I used abc.php) $_POST
will hold the data submitted by the form, and $_POST['userid']
will hold the id to determine which record to update.
Upvotes: 1
Reputation: 98
You're using some framework or CMS? Your question is a little bit confusing, could you please be more accurate on the details. Like show your "change.php" file contents, or something like this.
Your question header is change the DB, but your question body say to me that you have problem only on sending the data from the form to the php file that saves on the database.
Upvotes: 0