Jump to content


Photo

Help With Update on a BLog


  • Please log in to reply
11 replies to this topic

#1 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 04:55 PM

Good Day Everyone, I'm a new PHP coder, and so far I'm liking it a the moment I'm creating a simple blog with a little bit of validation and update and create procedures, to a database that has a table for the blogs created I have created 5 different files:index php, display.php. update.php. create php and auth php, so far my code can insert into the database and can also read from it but when it comes to UPDATE it I'm having a nightmare and I dont understand what i am doing wrong even though I have looked for all types of solutions I will attach my files here, as theres too many of them let me know if someone can help me out any help is kindly apprecciated

Attached File  Question.zip   4.1KB   1 downloads

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 04:57 PM

No offense, but no one wants to look through all of your "too many" files to find your bug. You need to post just the relevant code, and describe the problem besides "it's wrong".
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 07:10 PM

No offense, but no one wants to look through all of your "too many" files to find your bug. You need to post just the relevant code, and describe the problem besides "it's wrong".

I don't really see the offense there unless you understand any of the words you said as offensive, I was in fact doing what is implied by the site, giving an option to attach multiple files. to a question, perhaps that the part I misunderstood

#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 07:12 PM

So, did you bother to read what I said? That's what I get for attempting to be gentle with people. Ignored.

Post the relevant code if you want help. Describe your problem.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 07:25 PM

So, did you bother to read what I said? That's what I get for attempting to be gentle with people. Ignored.

Post the relevant code if you want help. Describe your problem.

Don't get me wrong I'm sure you are the nicest person around here :) , in fact you deserve a price for being concerned of a noobie :) I was just kindly answering your statement.
at any case the part I think if my code is relevant would be :

<div id="theblogs">
<?php
if (isset($_POST))
{
if (!empty($_POST['submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];


$query = ("UPDATE nothingmovestheblog SET title = '$title', content = '$content' WHERE id= {$_GET['id']}");
$results = $db->query($query);
header('Location: index.php');
}else{
?>
<form action = "update.php" method = "post">
<fieldset>
<p>
<legend>Edit Your Post</legend>
<label for= "title">Title</label>
<input name = "title" id = "title" value = <?php echo $row['title']?> />

</p>
<p>
<label for= "content">Content</label>
<textarea id="content" name="content" rows="5" required><?php echo $row['content']?></textarea>
</p>
<p>
<input type = "submit" name = "submit" value = "I'd Like To Fix This Post !"/>
</p>

</fieldset>
</form>
<?php

}
}


?>

I have a query there that works on MyPHPadmin Im running a table on a database so far on my other php files, Im able to write Into the database(post new blogs) and read from it but when i select an update on an actual post to run the update query on it i get an error:
Notice: Undefined index: id in C:\Program Files\.... on line 5
Fatal error: Call to a member function fetch_assoc() on a non-object in C:\Program Files...on line 7

#6 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 07:54 PM

Okay you're not posting the ID at all, so $_POST['id'] isn't set. You need a hidden input with the posts id.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#7 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 08:10 PM

it is yesh :

<?php
require('cunnect.php');
$query = "SELECT title, content FROM nothingmovestheblog WHERE id = {$_GET['id']} ";
$result = $db->query($query);
$row = $result->fetch_assoc();


?>
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title> Blog </title>
<link rel=stylesheet type=text/css href=style.css />
</head>
<body>

<div id="wrapper">
<div id="header">
<h1>
<a href="index.php">My Terrible Awful Blog - New Post</a>
</h1>
</div>
<ul id="menu">
<li>
<a href="index.php">Home</a>
</li>
<li>
<a href="archive.php">Archive</a>
</li>
<li>
<a href="create.php" class="active">New Post</a>
</li>
</ul>


<div id="theblogs">
<?php
if (isset($_POST))
{
if (!empty($_POST['submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];


$query = ("UPDATE nothingmovestheblog SET title = '$title', content = '$content' WHERE id= {$_GET['id']}");
$results = $db->query($query);
header('Location: index.php');
}else{
?>
<form action = "update.php" method = "post">
<fieldset>
<p>
<legend>Edit Your Post</legend>
<label for= "title">Title</label>
<input name = "title" id = "title" value = <?php echo $row['title']?> />

</p>
<p>
<label for= "content">Content</label>
<textarea id="content" name="content" rows="5" required><?php echo $row['content']?></textarea>
</p>
<p>
<input type = "submit" name = "submit" value = "I'd Like To Fix This Post !"/>
</p>

</fieldset>
</form>
<?php

}
}


?>


</div>
<div id = "footer">CopyClown - No Rights To Reserve</div>
</div>


</body>
</html>

Edited by DonkeyKong, 12 February 2013 - 08:11 PM.


#8 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 09:06 PM

Okay you're not posting the ID at all, so $_POST['id'] isn't set. You need a hidden input with the posts id.


My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#9 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 09:43 PM

mmm a hideen input ? mmm you are aware Im calling this from a differen php file correct ?

#10 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 09:49 PM

Clearly I'm wrong since your code works so perfectly. Good luck.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#11 DonkeyKong

DonkeyKong

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 February 2013 - 10:14 PM

God, you are petulant; I was just trying to discuss your hint, obviously I'd like to understand what you are giving me, but sure

#12 l0gic

l0gic

    Advanced Member

  • Members
  • PipPipPip
  • 106 posts
  • LocationChristchurch City, New Zealand

Posted 13 February 2013 - 03:38 AM

Two things..

One: Don't be such an ass, Jessica doesn't have to help you at all. She's a very nice and helpful person on these forums.

Two: Jessica is right. You're not supplying an ID at all.

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

Nothing being passed through the URL/Action/method that $_GET is used with, with is what you're using to try to 'get' the non-passed therefore non-existent ID.

You can either change that to:

<form action = "update.php?id=$row['id']" method = "post">

Or add a hidden input like this:

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


Which pretty much sums up what Jessica hinted at. And please remember this is a 'help' forum, not a 'do it for you or suffer your wrath' forum.

Edit: Forgot this forum uses CODE not PHP for code-blocks.

Edited by l0gic, 13 February 2013 - 03:41 AM.

I start more projects than I finish.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com