I have text being loaded onto the page from a MySQL database. When the text is clicked, using JavaScript the element is replaced with a textbox displaying the text. I want to be able to use a MySQL update query to change what is in the textbox and so it will rep;ace the current data held in the database. My problem is that when I press the submit button nothing happens and the data just stays the same. I am not sure where I have gone wrong in my code.

<form method="post" action="editindex.php">
<?php
//$row["title"] = "title";
//$row["id"] = "id";
$sql = "SELECT * FROM home ORDER BY id ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){

echo "<span id=\"itm1\" onclick=\"exchange(this);\">" . $row['title'] . "</span>
<input ondblclick=\"exchange(this);\" id=\"itm1b\" class=\"replace\" type=\"text\<h2></h2> name=\"title\">";?>
<input type="submit" value="Save" name="update1">
<input type="hidden" name="id" value="<?php echo $row['id']
}?>"
</form>
<?php 
if (isset ($_POST['update1']))
{
    //$row["title"] = "title";
    //$row["id"] = "id";
    $update = "UPDATE home SET title = '".$_POST['title']."' WHERE id = '" .$_POST['id']."'";
    mysql_query($update);
}
?>

Maybe you shoud put the update block first, so the script check whether POST data exists and writes changes to the DB (I presume action is set to open the same script).

if (isset ($_POST['update1']))
{
    //$row["title"] = "title";
    //$row["id"] = "id";
    $update = "UPDATE home SET title = '".$_POST['title']."' WHERE id = '" .$_POST['id']."'";
    mysql_query($update);
}

<form method="post" action="editindex.php">

<?php
//$row["title"] = "title";
//$row["id"] = "id";
$sql = "SELECT * FROM home ORDER BY id ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) 
{
    echo "<span id=\"itm1\" onclick=\"exchange(this);\">" . $row['title'] . "</span>
    <input ondblclick=\"exchange(this);\" id=\"itm1b\" class=\"replace\" type=\"text\<h2></h2> name=\"title\">";?>
    <input type="submit" value="Save" name="update1">
    <input type="hidden" name="id" value="<?php echo $row['id']
}?>"

</form>
<?php
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.