0

I am learning php at the moment so I have set myself a task to create a to do list.

I have got to a point where I can add a record, show that record or records. Now I am wanting to edit that record and also delete that record but am a little confused. So I guess I need to target the unique ID of that record in order to make changes to update or delete.

Here is what I put together to get me started:

mysqli_query($con,"UPDATE items SET content='blah blah blah' WHERE id=id");

if (isset($_POST['content'])) {
   mysqli_query($con,"UPDATE items SET content='".mysqli_real_escape_string($con, $_POST['content'])."' WHERE id='".mysqli_real_escape_string($con, $_POST['id'])."' ");
}

header('Location: index.php');

mysqli_close($con);

That is hooked up to a form:

    <form action="insertupdate.php" method="post">
<input type="hidden" name="id" value ="1">
Content: <input type="text" name="content">
<input type="submit">
</form>

I also created a column with three status's to either 0 = deleted 1 = active 2 = complete

I am a little stuck and just need a pointer more than anything.

user3725879
  • 69
  • 1
  • 1
  • 11

2 Answers2

1

Your query should look like this:

mysqli_query($con,"UPDATE items SET content='blah blah blah' WHERE id=1");

EDIT

if (isset($_POST['content'])) {
   mysqli_query($con,"UPDATE items SET content='".mysqli_real_escape_string($con, $_POST['content'])."' WHERE id=1");
}

However you should learn How to prevent SQL-injection to make your code secure.

EDIT2

<form action="insertupdate.php" method="post">
Content: <input type="text" name="content">
<input type="hidden" name="id" value = "1">
<input type="submit">
</form>

and

if (isset($_POST['content'])) {
   mysqli_query($con,"UPDATE items SET content='".mysqli_real_escape_string($con, $_POST['content'])."' WHERE id=".intval($_POST['id']));
}
Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Oh brilliant so that changed it, now the confusion I really have it that I am setting the content to change within the query, how do I target allow it from my form to change from what I enter in the text box? – user3725879 Jul 11 '14 at 11:27
  • Ok cool, now again to further this, you can see this target id=1, how do I make it target the item that I am targeting? – user3725879 Jul 11 '14 at 11:35
  • You need to have in your form id you want to edit. For example `` and then change in query ` into `$_POST['id]` using the same technique I showed above – Marcin Nabiałek Jul 11 '14 at 11:37
  • That seems to have a caused problem where by now it has changed all the items to the same...there was a > missed off. So now it updates only eh first item, how can I make it target the items Io am wanting to update by their id? – user3725879 Jul 11 '14 at 11:43
  • This code should work, you simple need to put value of record id you want to change. So in PHP you can do `$idToChange = 5;` and then in your form: `` – Marcin Nabiałek Jul 11 '14 at 11:48
  • Im struggling to understand a little :/ so will that target item 5? – user3725879 Jul 11 '14 at 11:50
  • Yes, it should change item with id = 5 – Marcin Nabiałek Jul 11 '14 at 11:51
  • Ok so I dont wanna specifically target an item I need it know that when I click edit item it know it is that one it needs to edit see my post above of the index page. – user3725879 Jul 11 '14 at 11:53
  • Sorry, but I cannot help you more with this. This forum is for help with question and you cannot edit your question 3 or 4 times to get new answers. You should also learn basics of PHP on your own – Marcin Nabiałek Jul 11 '14 at 12:10
1

a varchar type value must be written in single or double quote.

rack_nilesh
  • 553
  • 5
  • 18