As you can see I get the id from get method, the problem is when I using the $id to update the record it doesnt work but if I replace the $id with a number it works just fine, for example ($query = "UPDATE article SET title='$title_up', utitle='$utitle_up', text='$text_up', image='$image_up' WHERE id=2";)
PHP code:
$id = $_GET['id'];
$sql="SELECT * FROM article WHERE id = '$id'";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
$title = $row['title'];
$utitle = $row['utitle'];
$text = $row['text'];
}
if(isset($_POST['update'])){
$title_up = mysqli_real_escape_string($conn, $_REQUEST['title']);
$utitle_up = mysqli_real_escape_string($conn, $_REQUEST['utitle']);
$image_up = mysqli_real_escape_string($conn, $_REQUEST['image']);
$text_up = mysqli_real_escape_string($conn, $_REQUEST['text']);
$query = "UPDATE article SET title='$title_up', utitle='$utitle_up', text='$text_up', image='$image_up' WHERE id='$id'";
mysqli_query($conn, $query);
if ($conn->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
HTML form code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<label for='title'>Title: </label> <br>
<input type='text' id='title' name='title' value="<?php echo$title;?>"/> <br>
<label for='utitle'>UTitle: </label> <br>
<input type='text' id='utitle' name='utitle' value="<?php echo$utitle;?>"/> <br>
<label for='text'>Text: </label> <br>
<input type='text' id='text' name='text' value="<?php echo$text;?>"/> <br>
<input type='text' id='image' name='image' value="<?php echo$image;?>" /> <br>
<input type="submit" name="update" value="Update" />
</form>
Replace this line :
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
By this :
<form action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>" method="POST">
EDIT :
$_SERVER["PHP_SELF"] // return just file
$_SERVER["REQUEST_URI"] // return file and GET parameters
Related
I have the following code, the variable 'name' and 'project' need to go to session variables on submit, in this case the submit happens through an 'onchange'event. The 'name' variable is POSTed but not the 'project' variable. Any ideas where my issue is?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
</form>
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name[]' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
How to access:
$names = $_POST['name'];
for($i=0;$i<count($names);$i++)
{
echo $names[$i]."<br>";
}
I created a form that populates a database containing customer data. What i'd like to do is that when isset($_POST["submit"]), a GET parameter is appended to the URL, like &customer=<?php echo $last_id ?> where $last_id = mysqli_insert_id($connection);
Is this possible? Would this be a true GET parameter inserted in the GET array? Is also possible to populate the form with this newly added parameter?
Thank you very much!
This is the code, simplified
<?php
if(isset($_POST["submit"]) {
$nome_paciente = mysql_pre($_POST["nome"]);
$query = "INSERT INTO pacientes (";
$query .= "id, nome";
$query .= ") VALUES (";
$query .= "$id, \"{$nome_paciente}\"
$query .= ")";
$result = mysqli_query($connection, $query);
?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
<?php } else { ?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
?php } ?>
but i keep getting in my url:
&paciente=<br%20/><b>Notice</b>:%20%20Undefined%20index:%20id%20in%20<b>C:\xampp\htdocs\...
Thanks
Yes, I know there is a lot of 'Undefined index' questions floating around here and i have been looking through them before asking this question. I copied the codes from those questions to try and test it out but it still doesn't work for my own project. Also, I'm still a beginner in PHP.
So here is my problem. I wanted to try coding a simple edit form after I have finished coding the delete and view form.
This is my code
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
?>
<form action="editform.php" method="GET">
ID:
<?php echo $id; ?><br>
Contract Title<br>
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name = "edit "value="Update" />
</form>
?php
if(isset($_POST['edit']) ){
$id = $_GET['id'];
$upd= "UPDATE `contracts` SET
`contract_title`='".$_POST['contract_title']."',
WHERE `id`='".$_POST['id']."";
if($do_upd = $con->query($upd))
{
echo "Update Success";
}
else
{
echo "Update Fail";
}
}
?>
This is the before the error.
This is the error I received.
In line 3, the id is not retrieved after I clicked the button update.
It didn't retrieved the values.
What mistakes did I do in the coding and how do I fix it? Thanks in advance.
Right below:
<form action="editform.php" method="GET">
Add:
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Update:
Fixed other errors in your code:
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
?>
<form action="editform.php" method="GET">
ID: <?php echo $id; ?><br>
Contract Title<br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name="edit" value="Update" />
</form>
<?php
if(isset($_GET['edit']) ){
// needs escaping!~~~
$upd= "UPDATE `contracts` SET `contract_title` = '".$_GET['contract_title']."' WHERE `id` = '".$id;
if($do_upd = $con->query($upd)) {
echo "Update Success";
} else {
echo "Update Fail";
}
}
Please consider escaping your database input to prevent SQL injection
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$contract_title = $row['contract_title'];
}
} else {
echo "0 results";
}
if(isset($_POST['edit']) ){
$upd = "UPDATE contracts SET contract_title='$contract_title' WHERE id='$id'";
if($do_upd = $con->query($upd))
{
echo "Update Success";
}
else
{
echo "Update Fail";
}
}
?>
<form action="" method="POST">
ID:
<?php echo $id; ?><br>
Contract Title<br>
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name = "edit" value="Update" />
</form>
I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>
I have written an Edit part of my Form but somehow it will not Update the edited Fields. The Code does not give any Errors but it will not update?!
If possible could somebody take a look please?
<?php
include "connect.php";//database connection
if (isset($_GET["id"])) {
$id = intval($_GET["id"]);
if (isset($_POST["edited"]))
{
$update = "UPDATE traumprojekt SET";
$update .= sprintf("quantityProduct='%s', " , mysql_real_escape_string($_POST["quantityProduct"]));
$update .= sprintf("titleProduct='%s', " , mysql_real_escape_string($_POST["titleProduct"]));
$update .= sprintf("informationProduct='%s'", mysql_real_escape_string($_POST["informationProduct"]));
$update .= "WHERE id = '$id'";
mysql_query($update);
}
$sql = "SELECT * FROM `traumprojekt` WHERE id=$id";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
?>
<form method="post" action="edit_form.php?id=<?php echo $row["id"] ?>">
ID: <?php echo $row["id"] ?><br />
Quantity: <input type="text" name="quantityProduct" value="<?php echo $row["quantityProduct"] ?>"><br />
Product Title: <input type="text" name="titleProduct" value="<?php echo $row["titleProduct"] ?>"><br />
Product Information: <input type="text" name="informationProduct" value="<?php echo $row["informationProduct"] ?>"><br />
<input type="submit" name="submit" value="Update"><br />
<input type="hidden" name="edited" value="1">
</form>
<?php
}
}
?>