Jump to content


Photo

Updating MySQL with PHP


  • Please log in to reply
4 replies to this topic

#1 Riyoku

Riyoku
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationNunya

Posted 12 March 2006 - 06:40 PM

Yes, me again, sorry ^^"

Okay, mabey I've got the error retreival set up incorrectly? I don't know. What I do know is when I hit submit - the page claims it was successful. But when I go to retrieve the data later - it's completely unchanged. I've tried the code in the library but not only can I not.. understand it*so ashamed* but it causes HUGE strain on my website(at least I think it does, it takes about a minute to load a page right after I tried it) and gives me errors anyway... ._. So I switched back to the code I was using before.

mysql_query("UPDATE news SET `type`='$p_type', `title`='$p_title', `month`='$p_month', `day`='$p_day', `year`='$p_year', `body`='$p_body', `by`='$p_by' WHERE id='$n_id'") or die("<b>Update error : </b>". mysql_error());

I can't post the error I'm getting as it wont give me one with that code. All I can say is I'm stumped that it just plain doesn't work... >>"

Thanks again to the people who helped me last time. I learned my lesson, to set my pride aside and let people help - saves me a lot of time and frustration - and I can learn better that way too. Can't learn if I have no clue where I'm going wrong.

EDIT - Sorry, I realised I left out a bit of information on my table and where the info is comming from. The table name is news and it has 8 rows; id(auto-number), type, title, month, day, year, body, and by.
The new information is being retrieved by a form on the page. I'm only including the update statement as I'm assuming that's where the problem origonates.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 March 2006 - 07:09 PM

Can you post some more of your code? One statement out of context is not a help.

That being said, I would change your code to be:
<?php
$q = "UPDATE news SET `type`='$p_type', `title`='$p_title', `month`='$p_month', `day`='$p_day', `year`='$p_year', `body`='$p_body', `by`='$p_by' WHERE id='$n_id'";
echo '<pre> Query: ' . $q . '</pre><br />'; // for debugging
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());
?>

Ken

#3 Riyoku

Riyoku
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationNunya

Posted 12 March 2006 - 07:12 PM

[!--quoteo(post=354257:date=Mar 12 2006, 02:09 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 12 2006, 02:09 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Can you post some more of your code? One statement out of context is not a help.

That being said, I would change your code to be:
<?php
$q = "UPDATE news SET `type`='$p_type', `title`='$p_title', `month`='$p_month', `day`='$p_day', `year`='$p_year', `body`='$p_body', `by`='$p_by' WHERE id='$n_id'";
echo '<pre> Query: ' . $q . '</pre><br />'; // for debugging
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());
?>

Ken
[/quote]

Oh, I'm sorry. I thought my problem was with just that statement so I didn't include more...

<?php include "db.php";
$n_id = $_GET['id'];

$result=mysql_query("SELECT * FROM news WHERE id='$n_id'")
or die("<b>Query error : </b>". mysql_error());

if (($REQUEST_METHOD=='POST')) {
$p_type = $_POST['p_type'];
$p_title = $_POST['p_title'];
$p_month = $_POST['p_month'];
$p_day = $_POST['p_day'];
$p_year = $_POST['p_year'];
$p_body = $_POST['p_body'];
$p_by = $_POST['p_by'];

$q = "UPDATE news SET `type`='$p_type', `title`='$p_title', `month`='$p_month', `day`='$p_day', `year`='$p_year', `body`='$p_body', `by`='$p_by' WHERE id='$n_id'";

echo '<pre> Query: ' . $q . '</pre><br />'; // for debugging
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());}

else {while($row = mysql_fetch_array( $result )) {
$f_type = $row['type'];
$f_title = $row['title'];
$f_month = $row['month'];
$f_day = $row['day'];
$f_year = $row['year'];
$f_body = $row['body'];
$f_by = $row['by'];?>


<!--FORM-->
<form action="nedt.php" method="post"><table align="center">
<tr><td><b>Type</b></td>
<td><select style="width:188" name="p_type">
    <option value="news">News</option>
    <option value="updt">Update</option>
    <option value="rmnr">Reminder</option></select></td></tr>
<tr><td><b>Title</b></td><td><input type="text" style="width:188" name="p_title" value="<?echo $f_title;?>"></td></tr>
<tr><td><b>Date</b></td>
    <td><input type="text" style="width:60" name="p_month" value="<?echo $f_month;?>">
    <input type="text" style="width:60" name="p_day" value="<?echo $f_day;?>">
    <input type="text" style="width:60" name="p_year" value="<?echo $f_year;?>"></td></tr>
<tr><td colspan=2><textarea style="width:226" name="p_body"><?echo $f_body;?></textarea></td></tr>
<tr><td><b>Sign</b></td><td><input type="text" style="width:188" name="p_by" value="<?echo $f_by;?>"></td></tr>
<tr><td colspan=2 align="right"><input type="submit" value="Submit"><input type="reset" value="Reset"></td></tr>
</table></form>
<? }} ?>

It's retrieving the info from a form on the same page :P


EDIT - added the whole thing just to be sure... and I THINK I figured out the problem thanks to your error retrieval - it isn't passing the ID along with the form!

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Query: UPDATE news SET `type`='news', `title`='Test', `month`='01', `day`='01', `year`='06', `body`='blah blah', `by`='Riyoku' WHERE id=''[/quote]

would a hidden field do it?

EDIT AGAIN - Apparently, it would! WOOOOOO! Thank you again for your help! Without your input, I would have never figured out where I went wrong. :3 *finishes up her pages now that she's got all the functions figured out. X3* <3

#4 comicrebel

comicrebel
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 13 August 2008 - 03:46 AM

Thanks to this error reporting code, (and after HOURS of would be debugging) I was also able to identify the problem quickly (I wasn't defining my variables-duh)

$result = mysql_query($sql) or die('Problem with query: ' . $sql . '<br />' . mysql_error());

Thank you Riyoku!

#5 dezkit

dezkit
  • Members
  • PipPipPip
  • Advanced Member
  • 1,237 posts

Posted 13 August 2008 - 05:20 AM

SORRY FOR GETTING OFF TOPIC BUT HOW DOES THIS THREAD HAVE 1000 VIEWS? LOL
why hello thar




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users