Jump to content

MySQL update


MattD00

Recommended Posts

I am trying to update the content of my website which is all stored in a MySQL database.

 

Currently on the page I am displaying all the data stored in the database using MySQL SELECT and a while loop. Using JavaScript when the text is clicked the element changes into a text box allowing the text to be edited.

 

What I want to do is be able to change the data in the text box and then store it into the database updating the current data.

 

<form method="post" action="editindex.php">
<?php
$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\ name=\"title\">";?>
<input type="submit" value="Save" name="submit">
<input type="hidden" name="id" value="<?php  $row['id'] ?>"
</form>
<?php 
if (isset ($_POST['submit'])) {
$update = "UPDATE home SET title=".$_POST['title']." WHERE id=".$_POST['id']."";
mysql_query($update);
}
?>

 

I have more text being output from the while loop below but I just want to get it working on the title first.

 

Any ideas of why the update is not working?

 

Link to comment
Share on other sites

See if this helps Matt:

 

<form method="post" action="editindex.php">
<?php
//$row["title"] = "TheTitle";
//$row["id"] = "TheId";
$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\" name=\"title\" >
<input ondblclick=\"exchange(this);\" id=\"itm1b\" class=\"replace\" type=\"text\" name=\"title\">
<input type=\"submit\" value=\"Save\" name=\"submit\">
<input type=\"hidden\" name=\"id\" value=".$row['id'].">
";
}
?>
</form>




<?php
if (isset ($_POST['submit']))
{
$update = "UPDATE home SET title = '".$_POST['title']."' WHERE id = '" .$_POST['id']."'";
mysql_query($update);
}
?>

Link to comment
Share on other sites

<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\ name=\"title\">";
}
?>
<input type="submit" value="Save" name="update">
<input type="hidden" name="id" value="<?php  $row['id'] ?>"
</form>
<?php 
if (isset ($_POST['update']))
{
//$row["title"] = "title";
//$row["id"] = "id";
$update = "UPDATE home SET title = '".$_POST['title']."' WHERE id = '" .$_POST['id']."'";
mysql_query($update);
}
?>

Link to comment
Share on other sites

This line:

<input ondblclick=\"exchange(this);\" id=\"itm1b\" class=\"replace\" type=\"text\ name=\"title\">";

is missing a "

<input ondblclick=\"exchange(this);\" id=\"itm1b\" class=\"replace\" type=\"text\" name=\"title\">";

 

And this line:

<input type="hidden" name="id" value="<?php  $row['id'] ?>"

is missing the word echo and the terminator.

<input type="hidden" name="id" value="<?php  echo $row['id'];  ?>"

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.