I make file details.php that show list of data from MySQL, and I make edit.php for it.
When I click edit.php it's automatically go to edit.php?id=detailsid (it works) but when I click submit on edit data, it's nothing changed and error.
This is my Edit.php
<form method="post" action="editdata.php">
<?php
include 'config.php';
$id=$_GET['id'];
$sqlTampil="select * from data_korban Where kasus_id=$id";
$qryTampil=mysql_query($sqlTampil);
$dataTampil=mysql_fetch_array($qryTampil);
$data=mysql_fetch_array($qryTampil);
?>
This is some of my HTML form
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
<input type="submit" name="submit" value="SUBMIT">
When I click Submit it's automatically call editdata.php but there is no change on my MySQL row ..
This is my editdata.php
<?php
include "config.php";
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
$update = "UPDATE data_korban SET tanggal='$tanggal', namakorban='$namakorban', umurkorban='$umurkorban' WHERE kasus_id = '$id'";
$hasil = mysql_query($update);
if ($hasil)
echo "<center>Update Success<center>";
else
echo "<center><h3><a href=pencarian.php>Back Tampil Data</a></h3></center>";
?>
There is something in my Error_Log
PHP Notice: Undefined index: id in /home/lombokdi/public_html/dbanak/editdata.php on line 4 ------ it's $id=$_GET['id'];
I have change to $_POST['id']; it's still error.
Your form does not seem to include the id named field.
And you are trying to read the non existing field from the request.
Request is formed to send data over post method but you tried to read id parameter using get method. As the value for id is not found, a null is used in where clause and that affected no rows updated.
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
the value for ID is $id=$_GET['id']; ... id come from detail.php?id=24 (example) go to edit.php?id=24 .. If I'm wrong, how to set the ID ??
In edit.php add id named element to the form with value read from the request. And on submitting the form, id goes to editdata.php page.
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="hidden" name="id" value="<?php echo $id['id']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
And in editdata.php:
Change:
$id=$_GET['id'];
To:
$id=$_POST['id'];
It's because you don't have the filed "id" if you switch to editdata.php
add: <input type="hidden" name="id" value="<?php $_GET['id']; ?>">
to your form in Edit.php
and replace
$id=$_GET['id']; with $id= $_POST['id']; in editdata.php.
Try like this
$id=$_GET['id'];
$sql=mysqli_query($con,"Update client set nama='$nama',instansi='$instansi',telepon='$telepon',email='$email' where id = '".$_GET['id']."'");
if($sql)
{
$msg="Your Client updated Successfully";
header('location:manage-client.php');
}
for after body add this query
$sql=mysqli_query($con,"select * from client where id = '".$_GET['id']."'");
while($data=mysqli_fetch_array($sql))
You are using $_GET to get the id. It is best do var_dump($_REQUEST);
Related
When user inputs text in 'ctext' field and press accept, I want to fill the value=" " field with user input, i achieved this but it fills all the value fields of same name in the page, how can i achieve it for different value of different ctext input? Anyone please give me solution with example, Many thanks
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
$i=0;
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here" value="<?php $ctext = false; if(isset($_POST['ctext'])){ $ctext = $_POST['ctext']; } echo $ctext[$i]; ?>"></input>
<input type="hidden" name="id" value="<?php $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
$i++;
endwhile;
endif;
endif;
?>
I hope I understand what you want. You want to access the ctext for each individual $pen when printing the corresponding form.
You just need to name your <input> with a unique name and then access that value when printing. A possible solution is this:
<input type="text" name="ctext[<?php echo $pen['id']; ?>]" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } echo $ctext; ?>"></input>
What does it do?
name="ctext[<?php echo $pen['id']; ?>]" ensures a unique name for each $pen. For a $pen with id 1 this will result in name="ctext[1]".
if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } uses $pen['id'] to look up the corresponding value in $_POST['ctext'].
By the way, when outputting user input you should always escape it, e.g. with htmlspecialchars. This will look like this: echo htmlspecialchars($ctext); That way malicious input like "><script>alert('Hello!')</script> won't execute the javascript.
Update: as requested a solution using session to store data:
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
session_start();
if (isset($_POST['ctext'])) {
$_SESSION['ctext'][$_POST['id']] = $_POST['ctext'];
}
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_SESSION['ctext'][$pen['id']])){ $ctext = $_SESSION['ctext'][$pen['id']]; } echo htmlspecialchars($ctext); ?>"></input>
<input type="hidden" name="id" value="<?php echo $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
endwhile;
endif;
endif;
Note: I removed the now unnecessary counter $i. The session handling is mainly done before the while loop (start a session and store POST data). During output the values from the session are used. The name of the input is not an array anymore.
Change name of an input to an array.like this . When you submit the form you will get these values as an array. Give it a try
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here"></input>
I guess your code is misleading you, your form is in while loop so once any of the ctext input is filled your variable $_POST['ctext'] is set on server side and according to your code it sets all the value of ctext once accept is pressed.
You can have different names as a solution or an array indexing in input field name=“ctext[]” to avoid this.
I have a created an HTML form where users sign up and input there data into an SQL database. I have then retrieved that data in a webpage where they can view their profile. I have created a page where users can edit there profile by creating a form which updates the value in the SQL database for there user id.
I would like the form to use the current value of the SQL cell as the default for that user to make alterations easier. Example: currently user 7 has their city set as New York, when they visits the edit info page, the city field in the form already hase New York as the default value.
I have no problem getting the SQL info and assigning it to a variable, I just don't understand how to set it as the default value. I am aware of how you set default values for input fields though.
My code:
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
echo mysqli_error();
}
}
?>
<input type="text" name="aboutme" defualt="<?php echo $row['aboutme'] ?>" >
There's no default value for html input.
Input can has value, using attribute value:
<input type="text" name="some_name" value="Some value" />
In your case it's
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> />
Input can also has placeholder - some value that is present in an input, but erased when user starts to edit input's content:
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> placeholder="some value" />
How about
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
?>
<input type="text" name="aboutme" value="<?php echo $row['aboutme'] ?>" >
<?php
echo mysqli_error();
}
}
?>
And here is a good example http://www.w3schools.com/php/showphpfile.asp?filename=demo_db_select_pdo
Neither of the answers worked and upon further research and trial and error I created a solution.
I changed the value that was store in the array to just be a normal php variable:
$aboutme = $row['aboutme'];
I then called that variable using the following code:
<input type="text" name="aboutme" value="<?php echo htmlspecialchars($aboutme); ?>" >
Thanks for your help.
I hope you find my answer useful.
Why don't you try using it as a place holder? This will provide editable text.
<input type="text" name="aboutme" placeholder="<?php echo $row['aboutme'];" />
As the title reveals I got an issue with how to update a checkbox that already has data in my SQL database.
My code looks like following:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="inputAll" value="checked" <?php echo $hemsida['All']; ?>/>Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$_POST[inputALL]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
?>
The echo $hemsida['Namn'],['Comment'], and ['All'] just brings up and shows the old data thats in the database, but I do not understand what to do to update the checkbox. I have looked everywhere but I am stuck. Thank you in advance!
If I understand your question correctly, you are looking for a way to have a checkbox be either checked or not checked depending on database info. If so, I would try something like this. At the top of your code where you get your database info, put
if($conditionForCheck){
$inputAll = ' checked="checked"';
}
Then in your form
<input type="checkbox" name="inputAll"<?php echo $inputAll; ?> />
your question is not clear but i think you have a column in your database named "all" ? and perhaps this column can take only 1 value (true or false) !!
then you can test this value in your form, if the value is true : checkbox will be checked, else : checkbox will not be checked :
<input type="checkbox" name="inputAll" checked="<?php if($hemsida['All'] == true) echo checked; ?>" />Alla
dont use value="", use checked instead, then test value of $hemsida['All'] if it's true echo checked else anything to do
for your php code and server side of your application you can just test if checkbox is checked and then you have choice for what do you want to assign to your column in database, for example if checkbox is checked create a variable (for example $value_of_checkbox) and assign a value ("true" for exampel) to this variable, then include this variable in your sql code for update database column :
if (isset($_POST['inputALL'])) {
$value_of_checkbox = true;
}
else {
$value_of_checkbox = false;
}
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$value_of_checkbox' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php")
note : i change also sql code in this part : ALL='$value_of_checkbox'
The way I've structured my form data is by creating them in a while loop, but each time they are created the form will take a unique id.
So my question is, how do I access them individually and update specified data to a MYSQL server.
I've attempted to do it in the code at the end of the script, but I'm not sure how to access the forms individually
<?php
include 'user_data.php';
include 'core.inc.php';
$query = mysql_query("SELECT `post_text` FROM `posts`,`sub_posts` WHERE sub_posts.post_id = posts.id AND sub_posts.user_id='$user_id'");
while($row = mysql_fetch_array($query)){
?><?php echo $row[post_text].'<br>'?>
<form action="<?php $curent_file ?>" method="POST">
<textarea name="answer_field" > </textarea><br />
<input type="submit" value="Submit Answer">
<input type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>" />
</form>
<?php
}//While Loop
if (isset($_POST['answer_field']) && !empty($_POST['answer_field'])){
$answer = mysql_real_escape_string($_POST['answer_field']);
$id = intval($_POST ['post_id']);
$query = "UPDATE `sub_posts` SET `sub_answer`='$answer' WHERE `post_id`='$id'";
}
?>
Only a single form gets posted when clicking the "submit" field. The form name does not get submitted by itself. Instead, you would place the post ID to which the form corresponds as a hidden field:
<input type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>" />
And then later:
$answer = mysql_real_escape_string ($_POST ['answer']);
$id = intval ($_POST ['post_id']);
$query = "UPDATE `sub_posts` SET `sub_answer`='{$answer}' WHERE `post_id`={$id}";
Note that you definitely need to escape the answer before putting it in the query and make sure that the ID is a number. Otherwise, you're opening up your code to SQL injection attacks.
I have the following code:
$sql = mysql_query("UPDATE users SET name='$name' WHERE email='$email'") or die(mysql_error());
echo "<h4>Your information has been updated</h4><br />";
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for='name'>Name</label><br /><br />
<input class="input-text required" name="name" type="text" value="<?php echo $row[name]; ?>" />
</p>
<p><input type="submit" name="update" value="Update" /></p>
</form>
After clicking Update, it updates the information in the database, but the <?php echo $row['name']; ?> still shows the old value. Only after refreshing the page does it show the updated information. I could have it refresh the page after updating by echoing meta refresh but I want it to still show the echo saying "Your info has been updated" which doesn't happen if I set it to refresh. Is there any solution?
You will need to requery the database to repopulate $row or explicitly set the $row variable to your values (I would recommend repopulating it, just to be safe).
Or echo $name, instead of $row[name]
A update of the database is not a new select of the data.
You have 2 options:
after the update, do a new select of the data
instead of writing the row back to the form, write the posted value back