Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input value change

Tags:

html

php

input

I have a PHP update page in which I am showing a text field containing a value from the database. It is like this, and it is working,

<input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>

Now I need to put this updated value back in the database! I have used the code like this, but it's not updating:

$title=$_POST['title'];
$v_id = $_GET['v_id'];
$sql = mysql_query("update vehicles set title = '$title' where v_id = '$v_id'");

In detail... an input field is there. It's showing a value contained in $title (retrieved from the database) and that value is to be edited and updated.

From my side my code is working perfectly without showing any error, but the value that I give $title is giving the same one without any change.

Is there any other way to show a value in an input field without putting in a "value" tag? Two things wants to happen in a single input field!

like image 655
Wazan Avatar asked Feb 18 '26 13:02

Wazan


1 Answers

You'll need to post your form HTML as well.

Unless your form looks like the following, that code won't work

<form method='post' action='page.php?v_id=1'>
   <input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>
</form>

This is because you're using $_GET to get the id field and $_POST to get the value field

EDIT Your edit has muddied the water a bit more. I'm going to assume all you want to do is show a title and let the user update the title

<?php
   if ($_POST) {
        $title = mysql_escape_string($_POST['title']);
        $id    = intval($_GET['v_id']);
        $sql   = "update vehicles set title = '$title' where v_id = '$id'";
        mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
   } 
   if (isset($_GET['v_id'])) {
       $id      = intval($_GET['v_id']);
       $sql     = 'SELECT title FROM vehicles WHERE v_id = ' . $id;
       $rs      = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
       $row     = mysql_fetch_assoc($rs);
       $title   = htmlspecialchars($row['title']);
   }
?>

<form method='post' action='?v_id=<?php echo $id?>'>
   <input type="text" name="title" id="title" class="text_box" value="<?php echo $title ?>"/>
   <input type='submit' value='update'>
</form>

This should work for you. I haven't error tested obviously, but the idea is sound. You should also add any other input screening you feel necessary.

like image 101
JohnP Avatar answered Feb 20 '26 03:02

JohnP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!