0

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);
}
?>

Edited by Dani: Fixed code tags

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by broj1
0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.