This code is supposed to update my table, but it just refreshes the page. The query works, i tested it. its the submit button but i dont know why>
if(isset($_POST['nameupdate']))
{
echo('<hr>');
echo('Vul nieuwe waarde in');
$did = $jnm;
echo "<form method='post' data-ajax='false' action=''>";
echo "<input type='text' name='nmbox' value='".$did."'>";
echo "<input type='submit' name='nmupdate' value='Update'>";
echo "</form>";
if(isset($_POST['nmupdate']))
{
$opm = $_POST['nmbox'];
$query = "UPDATE users SET name = '$opm' WHERE username = '$jus'";
$stm = $con->prepare($query);
$stm->execute();
header("location:decdprofileedit.php");
}
}
You have to put nested if statement outside the first if statement
Related
I made a user system for my website. If you open it you well see a list of all users and you can lock or unlock them by changing "n_gesperrt" in the database to 1 or 0. 1 means locked.
The form you see here is always sending the last user id (n_id) to the php script on the top. So if I want to change something it only works with the last user shown on the list.
How can I solve that problem?
These are the code pieces:
THE FORM:
while ($zeile = mysqli_fetch_assoc($res))
{
echo "<form name=\"nutzer_sperren\" method=\"POST\" action=\"index.php?page=nutzer_sperren\">";
echo "<input type=\"hidden\" name=\"n_id\" value=\"".$zeile['n_id']."\">";
echo $zeile['n_id'];
if($zeile['n_gesperrt'] == 0){
echo "<input type =\"submit\" name=\"sperren\" value=\"Nutzer sperren\">";
}
else {
echo "<input type =\"submit\" name=\"entsperren\" value=\"Nutzer entsperren\">";
}
echo '</form">';
THE EDITING IN DATABASE:
if(isset($_POST['sperren']))
{
$sperrung = mysqli_real_escape_string($verbinde, 1);
mysqli_query($verbinde, 'UPDATE nutzer SET n_gesperrt ="'.$sperrung.'" WHERE n_id ='.$_POST["n_id"].'') or die(mysqli_error($verbinde));
echo "Daten wurden erfolgreich geändert. <br>Bitte aktualisieren Sie die Seite!";
echo $_POST["n_id"];
echo "<hr>";
}
if(isset($_POST['entsperren']))
{
$entsperren = mysqli_real_escape_string($verbinde, 0);
mysqli_query($verbinde, 'UPDATE nutzer SET n_gesperrt ="'.$entsperren.'" WHERE n_id ='.$_POST["n_id"].'') or die(mysqli_error($verbinde));
echo "Daten wurden erfolgreich geändert. <br>Bitte aktualisieren Sie die Seite!";
echo $_POST["n_id"];
echo "<hr>";
}
There are few things you need to change in your code, such as:
... The form you see here is always sending the last user id (n_id) to the php script on the top. So if I want to change something it only works with the last user shown on the list.
That's because you've assigned the same name attribute for all of your hidden input elements. Change your name attribute in the following way,
... name='n_id[]' ...
Change the name attribute of submit elements to just submit, because in this way you don't have to use two separate if blocks to handle user's response.
Remove the double quote from echo '</form">';, otherwise your form would break.
So your while() loop should be like this:
while ($zeile = mysqli_fetch_assoc($res)){
echo "<form method='POST' action='index.php?page=nutzer_sperren'>";
echo "<input type='hidden' name='n_id[]' value='".$zeile['n_id']."'>";
if($zeile['n_gesperrt'] == 0){
echo "<input type ='submit' name='submit' value='Nutzer sperren'>";
}
else {
echo "<input type ='submit' name='submit' value='Nutzer entsperren'>";
}
echo '</form>';
}
Furthermore, as I said above, you don't have to use two separate if blocks, just use one if block to check whether the form is submitted or not and toggle the n_gesperrt column value in your UPDATE operation, like this:
if(isset($_POST['submit'])){
mysqli_query($verbinde, 'UPDATE nutzer SET n_gesperrt = IF(n_gesperrt = 1, 0, 1) WHERE n_id ='.$_POST["n_id"][0]) or die(mysqli_error($verbinde));
echo "Daten wurden erfolgreich geändert. <br>Bitte aktualisieren Sie die Seite!";
}
bro use name as array
like below and you will get your result in array
while ($zeile = mysqli_fetch_assoc($res))
{
echo "<form name=\"nutzer_sperren\" method=\"POST\" action=\"index.php?page=nutzer_sperren\">";
echo "<input type=\"hidden\" name=\"n_id[]\" value=\"".$zeile['n_id']."\">";
echo $zeile['n_id'];
if($zeile['n_gesperrt'] == 0){
echo "<input type =\"submit\" name=\"sperren[]\" value=\"Nutzer sperren\">";
}
else {
echo "<input type =\"submit\" name=\"entsperren[]\" value=\"Nutzer entsperren\">";
}
echo '</form">';
on submission page get value like
$name=$_POST['entsperren'];
print_r($name);
I have a page that consists of a html table which contains data thats being retrieved from the database with the help of a php function. One of the columns in the table is a edit button. On clicking the edit button it navigates to a update form page. The rows in my database are
para_id=is the parameter number , id= user id , parameter_no = row id, para_date, para_time, reading, type
progpre.php(table page)
<?php
if(!$blood_pressure) {
echo '<td><h4>Nothing To Display. Add In Details<h4><td>';
}
else {
foreach ($blood_pressure as $single) {
echo "<tr>";
echo "<td>$single[para_date]</td>";
echo "<td>$single[para_time]</td>";
echo "<td>$single[reading] $single[type]</td>";
echo "<td><form action='updatepressure.php' method='GET'><input type='hidden' name='edit' value='$single[parameter_no]'><input type='submit' value='Edit' class='btn btn-primary' onclick='updatepressure.php'></form></td>";
echo "</tr>";
}
}
?>
updatepressure.php (Update page)
if(isset($_POST['submit']))
{
$_edit=print_r( $_GET['edit']);
$para_date = print_r($_POST['para_date']);
$para_time = print_r($_POST['para_time']);
$reading = print_r($_POST['reading']);
$type = print_r($_POST['type']);
//$parameter_no = print_r($_POST['edit']);
global $conn;
if ($stmt = $conn->prepare("UPDATE `tracking_para` SET `para_date`=?,`para_time`=?,`reading`=?,`type`=? WHERE `parameter_no`=$_edit ")) echo "hi";
{
$stmt->bind_param("ssss", $para_date, $para_time, $reading, $type);
$stmt->execute();
$result = $stmt->execute();
}
}
?>
idk where i'm going wrong.
Are you sure you want to insert the output of print_r() to your database?
If so, what is wrong here is that you have not set the $output parameter for your calls to print_r(). This parameter defaults to false so it will not return the string you are expecting to insert to your database.
Update your code to look like this:
if(isset($_POST['submit']))
{
$_edit=print_r( $_GET['edit'],true);
$para_date = print_r($_POST['para_date'],true);
$para_time = print_r($_POST['para_time'],true);
$reading = print_r($_POST['reading'],true);
$type = print_r($_POST['type'],true);
//$parameter_no = print_r($_POST['edit'],true);
...
You use POST
if(isset($_POST['submit']))
but send data with GET
echo "<td><form action='updatepressure.php' method='GET'><input type='hidden' name='edit' value='$single[parameter_no]'><input type='submit' value='Edit' class='btn btn-primary' onclick='updatepressure.php'></form></td>";
In this way can't work ..
Try using POST in all code section
First of all i start writing php this week and there may be unnecessary lines:)
I'm just trying figuring out the logic. For now everything went fine(thx to stackoverflow). Until,
In my foreach loop statement i put submit button, I added the db id after button name. So i put the id after $_POST too. But the problem is only the first submit button works. When i click the others nothing happens.
Thanks for your help. (btw i tried all other answers for foreach button issues. didn't help)
if (isset($_POST['arama'])) {
$ara = trim(strip_tags($_POST["ara"]));
$duzelt = trim(strip_tags($_POST["duzelt"]));
$id = $_SESSION["id"];
if (!empty($ara)) {
echo '<div class="form-style-10">';
echo '<table><tr>';
echo "<th>İsim</th><th>Cep Telefonu</th><th>Sabit Telefonu</th> </tr>";
$ara1 = '%'.$ara.'%';
$sql = $db -> query("SELECT * FROM rehber WHERE k_id='".$id."' AND isim LIKE '%$ara%'");
$yok = $sql->rowCount();;
if ($yok != 0) {
echo "<form action='' method='POST'>";
foreach ($sql as $dizi) {
$iden=$dizi[id];
echo "<tr><td><input type='text' name='isim1' value='$dizi[isim]'></td>";
echo "<td><input type='text' name='cep1' value='$dizi[cepno]'></td>";
echo "<td><input type='text' name='ev1' value='$dizi[evno]'></td>";
echo "<input type='hidden' name='id2' value='$iden'>";
echo "<td><input type='submit' name='duzelt".$iden."' value='duzelt'></td></form></tr>";
}
echo "</table>";
echo "</div>";
}else{
echo '<div class="form-style-10" style="background-color:#f04"><div class="section" style="color:#FFFC00">'.$ara.' adında bir kullanıcı kayıtlı değildir.</div></div>';
header("refresh:3;rehber.php?mr=arama");
}
}
else{
echo '<div class="form-style-10" style="background-color:#f04"><div class="section" style="color:#FFFC00">Arama kutusu boş. Lütfen aramak istediğiniz kişinin adını yazınız.</div></div>';
header("refresh:3;rehber.php?mr=arama");
}
}
$buton = "duzelt".$_POST["id2"];
if (isset($_POST[$buton]) && $_POST[$buton]) {
$duz = $_POST[$buton];
if (!empty($duz)) {
echo $_POST["isim1"];
echo $_POST["cep1"];
echo $_POST["ev1"];
echo $_POST["id2"];
echo $buton;
$sql1 = $db -> prepare("UPDATE rehber SET isim = ?, cepno = ?, evno = ? WHERE id = ?");
$sql1 -> execute(array($_POST["isim1"], $_POST["cep1"], $_POST["ev1"], $_POST["id2"]));
echo "Kayıt başarıyla tamamlanmıştır.";
header("refresh:3;rehber.php?mr=duzen");
}else{
echo "kaydedilecek veri yok";
}
}
The only real issue that I can see if that the initial form constructor appears outside of the form element, whilst the form element is closed in the foreach loop. I would move the form constructor into the foreach loop. Also, as a secondary, you are using id as a constant value in your $iden constructor. This is easily resolved, but it seems you really want the index, which you can get from the foreach loop.
Please observe:
foreach ($sql as $iden => $dizi) { // <-- $iden is now the index
//$iden=$dizi[id]; <- no longer required
echo "<form action='' method='POST'>"; // <--form created inside loop
echo "<tr><td><input type='text' name='isim1' value='$dizi[isim]'></td>";
echo "<td><input type='text' name='cep1' value='$dizi[cepno]'></td>";
echo "<td><input type='text' name='ev1' value='$dizi[evno]'></td>";
echo "<input type='hidden' name='id2' value='$iden'>";
echo "<td><input type='submit' name='duzelt".$iden."' value='duzelt'></td></form></tr>";
}
Now you have the form being constructed within the loop, and the index is properly being passed along.
My UPDATE query is failing although the syntax looks fine to me (I have another update query that works fine on the same page).
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("sitename") or die(mysql_error());
$id = $_GET['id'];
if (isset($_POST['submit'])){
$b = mysql_real_escape_string(trim($_POST['body']));
//**You have an error in your SQL syntax;** --> ?
mysql_query ("UPDATE body SET body= $b WHERE id = $id") or die (mysql_error() );
// $b is fine
echo "$b";
}
How the HTML review forms are rendered..
// Puts SQL Data into an array
$q = mysql_query("SELECT * FROM vote") or die (mysql_error());
// Now we loop through the database
echo "<br />";
while ($ratings = mysql_fetch_array($q))
{
//This outputs the doctors's name
echo "Doctor's name:" . $ratings['doctor_name'] ."<br />";
//This outputs a textarea for the user to submit comments
echo "<b>Your Experience: </b>";
echo "<form method='post' action='review_doctors.php'>
<textarea name='body'></textarea>
<input type='submit' name='submit' value='Send' id='submit'/>
</form>
";
echo "<br />";
echo "<p> </p>";
}
Why am I getting a SQL syntax error whenever a comment is submitted?
So, you're setting $id from the $_GET array which will probably not be set on submission of a form via post.
The update query you're running is inside a check for a POST (checking to see if $_POST['submit'] is set).
You probably want to send the value for the $id in the post body and pull it from the post array.
I fixed it to this:
// If submitted
if (isset($_POST['id'])){
//Capture what was typed in textarea
$b = mysql_real_escape_string(trim($_POST['body']));
$id = $_POST['id'];
mysql_query ("UPDATE vote SET body = '$b' WHERE id = $id") or die (mysql_error() );
// $b and $id are still fine
echo "$b";
echo "$id";
}
Also fixed the hidden input value:
while ($ratings = mysql_fetch_array($q))
{
//This outputs the doctors's name
echo "Doctor's name:" . $ratings['doctor_name'] ."<br />";
$id = $_POST['id'];
//This outputs a textarea for the user to submit comments
echo "<b>Your Experience: </b>";
echo "<form method='post' action='review_doctors.php'>
<textarea name='body'></textarea>
<input type='submit' name='submit' value='Send'/>
<input type='hidden' name='id' value='$ratings[id]' />
</form>
";
echo "<br />";
I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>