Hello everyone, thanks for viewing my topic... I'm designing an administration panel for all my jobs that I do on my site. So, I have the basic stuff: add new Job, view current Jobs, and Delete Jobs. I also want to have an edit Job page. I have the basic code set up for it, as you will see below, but when I try it, the code doesn't work... When I click edit jobs it goes to a list of current jobs (okay, that's good so far), then I'll click on a job link, and it will take me to the edit page (still good...), but when I edit the Job, and click the "Update Job ##" button, it redirects to the same page and throws my error phrase at me: "You must specify an ID to view that job information.". And it still doesn't even update my Job info. So, the question is: how do I get the script to not redirect me to an error page and how do I get it to update my Job info? Okay, well rather than tell you what the problem is, I want you to see for yourself. So I'm giving DigitalPoint temporary access to my site's admin panel. There's only one fake job listed so far, so it's not that big of a deal right now. http://jodyheavener.com/admin Username: digitalpoint Password: access Code (markup): Go there, log in, and then click Edit Jobs. Click any current job and it will take you to the edit job page for that Job ID. And this is where it's screwed up: Make some changes to the job and then hit "Update Job ##" and you should see the error "You must specify an ID to view that job information.". And now, here is the code for editjob.php (please not all **** means that I'm just covering up info that's personal and irrelevant. <?php include '****.php'; require './****/****.php'; require './****/****.php'; ?> <?php mysql_connect('****.****.com', '****', '****'); mysql_select_db('****'); if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) { die("You must specify an ID to view that job information."); } else { $id = (int)$_GET['id']; } $result = mysql_query("SELECT * FROM jobs WHERE id='$id'") or print ("Uh oh! There was an error editing this job...<br><i>" . $sql . "</i><br>" . mysql_error()); while ($row = mysql_fetch_array($result)) { $old_date = $row['date']; $old_title = stripslashes($row['title']); $old_description = stripslashes($row['description']); $old_title = str_replace('"', '\'', $old_title); $old_description = str_replace('<br>', '', $old_description); } ?> <?php if (isset($_POST['submit'])) { $id = htmlspecialchars(strip_tags($_POST['id'])); $date = htmlspecialchars(strip_tags($_POST['date'])); $description = $_POST['description']; $title = htmlspecialchars(strip_tags($_POST['title'])); if (!get_magic_quotes_gpc()) { $title = addslashes($title); $description = addslashes($description); $date = addslashes($date); } $result = mysql_query("UPDATE jobs SET title='$title', description='$description', date='$date' WHERE id='$id' LIMIT 1") or print ("Uh oh! There was an error editing this job...<br>" . mysql_error()); header("Location: job.php?id=" . $id) ; } ?> <div id="content"> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <label for="title">Title:</label> <input type="text" id="title" name="title" size="40" value="<?php echo $old_title; ?>"><br> <label for="description">Description:</label><br> <textarea cols="40" rows="10" name="description" id="description"><?php echo $old_description; ?></textarea><br> <label for="date">Date:</label> <input type="text" name="date" id="date" size="12" value="<?php echo $old_date; ?>" maxlength="10"> <p><input type="submit" name="update" id="submit" value="Update Job <?php echo $id; ?>"></p> </form> </div> <?php mysql_close(); ?> <?php include './****/****.php' ?> PHP: So, there you have it. Please can someone throw me some tips or suggestions as to how I can fix this problem? Please and Thanks kindly!
What happens when you <?PHP print_r($_POST); ?> before the output of HTML? Sure the variable is passing correctly?
Put the code at the beginning of your code before anything else, and see what it outputs when you submit the form.
Okay, so I took your suggestion switched up the code so that it had the PHP after the HTML code. But then the $old(item) variables weren't being set, so I had to add the that code before the HTML. Also, on another site I was suggested to remove the "action="<?php echo $_SERVER['PHP_SELF']; ?>"" and just have it as "action=""" and that should be fine. So I tried it, and it stopped giving me the error of no ID. One more thing: I switched up the MySQL query from: $result = mysql_query("UPDATE jobs SET title='$title', description='$description', date='$date' WHERE id='$id' LIMIT 1") or print ("Uh oh! There was an error editing this job...<br>" . mysql_error()); PHP: To: $result = mysql_query("UPDATE jobs SET (title,description,date) VALUES ('$title','$description','$date') WHERE id='$id' LIMIT 1") or print ("Uh oh! There was an error editing this job...<br>" . mysql_error()); PHP: Here is the code now: <?php include '****.php'; require './****/****.php'; require './****/****.php'; ?> <?php mysql_connect('****', '****', '****'); mysql_select_db('****'); if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) { die("You must specify an ID to view that job information."); } else { $id = (int)$_GET['id']; } $result = mysql_query("SELECT * FROM jobs WHERE id='$id'") or print ("Uh oh! There was an error editing this job...<br><i>" . $sql . "</i><br>" . mysql_error()); while ($row = mysql_fetch_array($result)) { $old_date = $row['date']; $old_title = stripslashes($row['title']); $old_description = stripslashes($row['description']); $old_title = str_replace('"', '\'', $old_title); $old_description = str_replace('<br>', '', $old_description); } ?> <div id="content"> <form method="post" action=""> <input type="hidden" name="id" value="<?php echo $id; ?>"> <label for="title">Title:</label> <input type="text" id="title" name="title" size="40" value="<?php echo $old_title; ?>"><br> <label for="description">Description:</label><br> <textarea cols="40" rows="10" name="description" id="description"><?php echo $old_description; ?></textarea><br> <label for="date">Date:</label> <input type="text" name="date" id="date" size="12" value="<?php echo $old_date; ?>" maxlength="10"> <p><input type="submit" name="update" id="submit" value="Update Job <?php echo $id; ?>"></p> </form> </div> <?php if (isset($_POST['submit'])) { $id = htmlspecialchars(strip_tags($_POST['id'])); $date = htmlspecialchars(strip_tags($_POST['date'])); $description = $_POST['description']; $title = htmlspecialchars(strip_tags($_POST['title'])); if (!get_magic_quotes_gpc()) { $title = addslashes($title); $description = addslashes($description); $date = addslashes($date); } $result = mysql_query("UPDATE jobs SET (title,description,date) VALUES ('$title','$description','$date') WHERE id='$id' LIMIT 1") or print ("Uh oh! There was an error editing this job...<br>" . mysql_error()); header("Location: job.php?id=" . $id) ; } ?> <?php mysql_close(); ?> <?php include './****/****.php' ?> PHP: The page still just refreshes and doesn't update the info. I still need help. Can someone please help me out??? Thanks to all that have helped so far.
does your UPDATE query works?? ive tried editing the content and nothing happened even when i relogged in... You didnt use ajax so the problem is inside the query..
I don't think it's my query... I tried alternating the query and changing things about it... still won't update. Is it the way my table is set up??? I have 4 columns set up like this: id (INT NOT NULL AUTO_INCREMENT PRIMARY KEY), title (TEXT NOT NULL), description (TEXT NOT NULL), and date (TEXT NOT NULL). Oh man this is starting to frustrate me.
Could it be you are using GET for the id? if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) { Method is POST. if that's not it.... switch to GET and see what's being passed through or print out the sql statement.
I have no clue how to switch the entire code over to the GET method. I'm just letting everyone know that if you look on the home page of JodyHeavener.com, you'll see that I'm only 14. I've only had maybe a year of coding under my belt. I appreciate all the help Ive been getting, but if you're going to suggest something, please explain it clearly. Thanks.
Change : if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) { TO: if (!isset($_POST['id']) || empty($_POST['id']) || !is_numeric($_POST['id'])) { Explanation: Two ways to send data through a form. Get or Post, Get appends it to the URL. Example... script.php?variable=1 Post sends the data through HTTP (call it magic). It is not visible in the URL and it allows for larger amounts of data to be sent. It's a pretty important concept if you plan to do forms etc. Someone will respond you should just use $_REQUEST, it accepts both POSTs and GETs. And here's the tizag page for it: http://www.tizag.com/phpT/postget.php And the manual page... http://us3.php.net/variables.external
Well, I attempted what you told me to do, and it doesn't work. I give up. I just bought "Build Your Own Database Driven Website Using PHP & MySQL" from SitePoint.com, so I'll read up on it and practice. I think I just need a lot more experience before I start getting into something that challenging for me. If anyone has any suggestions for the script, please let me know, but for now I'm done.