PDA

View Full Version : Database prob - wont update or insert?


Tidus
08-03-2002, 04:38 PM
Hello!
Another problem - and i have had this one for a long time!

I want to use the code below to edit and add to the news database for our site.
It just seems that whenever i want to edit or insert news it wont communicate with the database or something.

When i go to edit a item, all the information will come into the form fields, but just wont update. Can someone please take a look at the code?


mysql_select_db("news",$db);



if ($submit) {

// here if no ID then adding else we're editing

if ($id) {

$sql = "UPDATE newsdb id='$id',displaydate='$displaydate',displaytitle='$displaytitle',time='$time',by='$by',news='$news', realtitle='$realtitle' WHERE id=$id";

} else {

$sql = "INSERT INTO newsdb (id,displaydate,displaytitle,time,by,news,realtitle) VALUES ('$id','$displaydate','$displaytitle','$time','$by','$news','$realtitle')";

}

// run SQL against the DB

$result = mysql_query($sql);

echo "Record updated/edited!<p>";

} elseif ($delete) {

// delete a record

$sql = "DELETE FROM newsdb WHERE id=$id";

$result = mysql_query($sql);

echo "$sql Record deleted!<p>";

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is not editing

$result = mysql_query("SELECT * FROM newsdb",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<a href=%s?id=%s>%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["displaytitle"], $myrow["last"]);

printf("<a href=%22%25s?id=%s&delete=yes/&quot;>(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

}

}



?>

<P>

<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

<P>

<form method="post" action="<?php echo $PHP_SELF?>">

<?php



if ($id) {

// editing so select a record

$sql = "SELECT * FROM newsdb WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$id = $myrow["id"];

$displaydate = $myrow["displaydate"];

$displaytitle = $myrow["displaytitle"];

$time = $myrow["time"];

$by = $myrow["by"];

$news = $myrow["news"];

$realtitle = $myrow["realtitle"];

// print the id for editing

}

?>

<input type=hidden name="id" value="<?php echo $id ?>" size="20">

<?php

}



?>


Post Date :
<input type="text" name="displaydate" size="26" value="<?php echo $displaydate ?>"><br>
<br>
Display Title: <font face="Verdana" color="#999999" size="2">



<input type="text" name="displaytitle" size="26" value="<?php echo $displaytitle ?>"><br>
<br>
</font>Time : <font face="Verdana" color="#999999" size="2">



<input type="text" name="time" size="26" value="<?php echo $time ?>"><br>
<br>
</font>By :<font face="Verdana" color="#999999" size="2">



<input type="text" name="by" size="26" value="<?php echo $by ?>"><br>
<br>
</font>News Bulk : <font face="Verdana" color="#999999" size="2">



<textarea rows="9" name="news" cols="41"><?php echo $news ?></textarea><br>
<br>
<br>
</font>Real Title: <font face="Verdana" color="#999999" size="2">



<input type="text" name="realtitle" size="26" value="<?php echo $realtitle ?>"></font></font><font face="Verdana" color="#999999" size="2"><font face="Verdana" size="2"><br>
<br>
<br>
</font><br>
<input type="Submit" name="submit" value="Enter information">

</form>


Thankyou!!!

Spookster
08-03-2002, 05:20 PM
I see you selecting the db

mysql_select_db("news",$db);


but I don't see the db handle with the connection.

should look something like this:

$db = @mysql_connect("localhost", "dbusername", "dbpassword") or die("Unable to connect to database at this time");

Tidus
08-04-2002, 03:00 AM
It is there - I just didn't copy it into the post - didn't think it was neccessary.. Sorry!

Anymore suggestions?