Click to See Complete Forum and Search --> : mysql update probelm


LiL|aaron
01-29-2006, 08:40 PM
HI...

I need a little bit of help... i am unable to make my script update the datebase but i am able to read it.

<?
include "config.inc.php";
$dbconn = db_open();
if(!$dbconn) exit;
include "header.inc.php";

$query1 = "select news_date from hyip_news";
$result1 = mysql_query($query1);
$row1=mysql_fetch_array($result1);
$news_date=$row1["news_date"];


$query2 = "select news_content from hyip_news";
$result2 = mysql_query($query2);
$row2=mysql_fetch_array($result2);
$news_content=$row2["news_content"];

?>

<FORM name=news action=news.php method=post>
<center><b>Update News</b></center>
<TABLE cellSpacing=2 cellPadding=2 border=0 width=100%>
<TR>
<TD><span class="genmed">Date:</span></TD>
<TD bgcolor="282f3b"><INPUT class=formelement maxLength=60 size=46 value="<?=$news_date?>" name=date></TD>
</TR>
<TR>
<TD bgcolor="282f3b" valign=top><span class="genmed">Your message</span></TD>
<TD bgcolor="282f3b"><textarea class=formelement name=content rows=8 cols=48><?=$news_content?></textarea></TD>
</TD>
</TR>
<TR>
<TD bgcolor="282f3b">&nbsp;</TD>
<TD bgcolor="282f3b">
<?if(!$submit){?>
<INPUT class=formbutton type=submit name=submit value="Submit">
<?}else{
$error = false;
$query = "update hyip_news set news_date='$news_date', news_content='$news_content'";
if($error){?>
News could not be updated.
<?}else{?>
News has been updated.
<?}}?>
</TD>
</TR>
</TABLE>
</FORM>

<?

include "footer.inc.php";
db_close($dbconn);
?>


The script basically updates it self, so there is no need for id's, there are just 2 fields, news_date and news_content. when admin clicks on update news, the page will open, with the current news in the textboxs and textarea, as already done and operational, but when i have editted the news, and want to save it, it does nothing.

Anyone know where i went wrong?

Kind Regards
Aaron

NogDog
01-29-2006, 08:54 PM
You never execute the update query. (You define the SQL update query, but you never process it through the mysql_query() function.)

chazzy
01-29-2006, 08:56 PM
Umm,

$query = "update hyip_news set news_date='$news_date', news_content='$news_content'";

Are you trying to add a new item or update an existing one, for one thing. Either way, you still have to use mysql_query to send the query, just like you did for the selects.

but do not run the query you have there, as it will overwrite all of the data in your table. you need to have a where condition or turn this into an insert statement.

LiL|aaron
01-30-2006, 06:35 AM
You never execute the update query. (You define the SQL update query, but you never process it through the mysql_query() function.)


I am not sure what ya mean, here... can you give me an example please :)


Are you trying to add a new item or update an existing one


Trying to update the existing one.


but do not run the query you have there, as it will overwrite all of the data in your table.

Thats what i want to do tho :cool:


Heres my new code trying to use the mysql_query() function

<? $query = "update hyip_news set news_date='$news_date', news_content='$news_content'";
$result = mysql_query($query); ?>

But it is still not changing the orginal table.

Thanks

NogDog
01-30-2006, 06:41 AM
Try doing an "insert" instead of an "update".

LiL|aaron
01-30-2006, 06:52 AM
Tried that... still does nothing.

<? $query = "insert into hyip_news (news_date,news_content) values ('$news_date','$news_content')";
$result = mysql_query($query); ?>

:mad:

NogDog
01-30-2006, 06:57 AM
Do some debugging:

$result = mysql_query($query) or die ("Query failed: $query - " . mysql_error());

LiL|aaron
01-30-2006, 07:18 AM
This thing still wont work... do you think its anything to do with the way i have layouted out the code ?

<?
include "config.inc.php";
$dbconn = db_open();
if(!$dbconn) exit;
include "header.inc.php";

$query1 = "select news_date from hyip_news";
$result1 = mysql_query($query1);
$row1=mysql_fetch_array($result1);
$news_date=$row1["news_date"];


$query2 = "select news_content from hyip_news";
$result2 = mysql_query($query2);
$row2=mysql_fetch_array($result2);
$news_content=$row2["news_content"];

?>

<FORM name=news action=news.php method=post>
<center><b>Update News</b></center>
<TABLE cellSpacing=2 cellPadding=2 border=0 width=100%>
<TR>
<TD><span class="genmed">Date:</span></TD>
<TD bgcolor="282f3b"><INPUT class=formelement maxLength=60 size=46 value="<?=$news_date?>" name=news_date></TD>
</TR>
<TR>
<TD bgcolor="282f3b" valign=top><span class="genmed">Your message</span></TD>
<TD bgcolor="282f3b"><textarea class=formelement name=news_content rows=8 cols=48><?=$news_content?></textarea></TD>
</TD>
</TR>
<TR>
<TD bgcolor="282f3b">&nbsp;</TD>
<TD bgcolor="282f3b">
<?if(!$submit){?>
<INPUT class=formbutton type=submit name=submit value="Submit">
<?}else{
$error = false;
$query("insert into hyip_news (news_date,news_content) values ('$news_date','$news_content')");
$result = mysql_query($query) or die ("Query failed: $query - " . mysql_error());
if($error){?>
News could not be updated.
<?}else{?>
News has been updated.
<?}}?>
</TD>
</TR>
</TABLE>
</FORM>

<?



?>


<?

include "footer.inc.php";
db_close($dbconn);
?>

Nothing shows on the debugging either... just news has been updated...

bathurst_guy
01-30-2006, 11:45 AM
$query = ("insert into hyi...
equal sign

LiL|aaron
01-30-2006, 12:18 PM
Hmm... still no change :(

bathurst_guy
01-30-2006, 12:25 PM
hmm missing something small somewhere... not sure. So if you echo out the query string, does it look correct? And so ATM there are no errors being reported, and you are seeing "News has been added" but when you check your db in phpmyadmin there is no new entry.. is this correct?

NogDog
01-30-2006, 12:28 PM
Can you show us the lates version of your code?

LiL|aaron
01-30-2006, 12:33 PM
Ok i just looked now...

It is saving to the database... but not the edited text... its saving the orginal

Ie:

Date: Jan 30th 2006
Edited Date: Jan 31th 2006

What shows in the database:
Date: Jan 30th 2006

but not the new editied version.

this is strange and i bet ur right it will be something simple.

bathurst_guy
01-30-2006, 12:38 PM
well this is an insert query, so its not going to update an existing field, it is going to create a new entry

LiL|aaron
01-30-2006, 01:50 PM
yeah maybe so... but it wouldnt save the same... it should be saving the edited text

LiL|aaron
01-30-2006, 03:22 PM
Man.... finally completed it!!!!

news.php

<?
ob_start();
include "config.inc.php";
$dbconn = db_open();
if(!$dbconn) exit;
include "header.inc.php";

$query1 = "select news_date from hyip_news";
$result1 = mysql_query($query1);
$row1=mysql_fetch_array($result1);
$news_date=$row1["news_date"];

$query2 = "select news_content from hyip_news";
$result2 = mysql_query($query2);
$row2=mysql_fetch_array($result2);
$news_content=$row2["news_content"];
?>

<FORM name=news action=news_submit.php method=post>
<center><b>Update News</b></center>
<TABLE cellSpacing=2 cellPadding=2 border=0 width=100%>
<TR>
<TD><span class="genmed">Date:</span></TD>
<TD bgcolor="282f3b"><INPUT class=formelement maxLength=60 size=46 value="<?=$news_date?>" name=news_date></TD>
</TR>
<TR>
<TD bgcolor="282f3b" valign=top><span class="genmed">Your message</span></TD>
<TD bgcolor="282f3b"><textarea class=formelement name=news_content rows=8 cols=48><?=$news_content?></textarea></TD>
</TD>
</TR>
<TR>
<TD bgcolor="282f3b">&nbsp;</TD>
<TD bgcolor="282f3b">
<INPUT class=formbutton type=submit name=submit value="Submit">
</TD>
</TR>
</TABLE>
</FORM>

<?
include "footer.inc.php";
db_close($dbconn);
ob_end_flush();
?>


news_submit.php

<?
ob_start();
include "config.inc.php";
$dbconn = db_open();
if(!$dbconn) exit;
include "header.inc.php";
include "menu.inc.php";

$news_date = $_POST['news_date'];
$news_content = $_POST['news_content'];

$query = "UPDATE hyip_news
SET news_date='$news_date', news_content='$news_content'";
$result = mysql_query($query);
?>
<center><span class="genmed">News has been updated.</span></center>
<?
include "footer.inc.php";
db_close($dbconn);
ob_end_flush();
?>

Very simple... all i need was the $_post variables

Thanks too all that helped anyways