What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » MySQL

MySQL
Author: Message:
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. MySQL
Hello guys, i need some more web development help, I have been working on a database for my website and can get everything to work except the UPDATE. As this is something that will have to be edited when browsing i want it to be done using a form, here is the code i am currently using..

code:
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","username","password");
//select which database you want to edit
mysql_select_db("database");
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from news order by id");
//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$title=$r["title"];//take out the title
$id=$r["id"];//take out the id
//make the title a link
echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
echo "<br>";
}
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM news WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
Title <INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
Message <TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["message"] ?></TEXTAREA><br>
Who <INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["who"] ?>" SIZE=30><br>
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
<? } ?>
<?
if ($_POST["$submit"])
{
$title = $_POST["title"];
$message = $_POST["message"];
$who = $_POST["who"];
$sql = "UPDATE news SET title='$title',message='$message',who='$who' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>


the "database", "username" and "password" sections have been changed for privacy and security reasons.

Can anybody notice any faults that would stop that from working, or any reasons for it not working??

Also if you have any advice on a better method or tutorial for this issue please provide a link, thanks!

This post was edited on 06-20-2008 at 06:44 AM by Eddie.
...there used to be a signature here :)
06-20-2008 06:44 AM
Profile PM Web Find Quote Report
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
RE: MySQL
Make the following changes

mysql_connect("localhost","username","password");
mysql_select_db("database");

becomes

@mysql_connect(...) or die("Unable to connect to database.");
@mysql_select_db(...) or die("Unable to select correct database.");

Edit: where (...) is your parameters above.

Do this for all the queries as well, but you could leave out the @ on them. If you're using it on a local server you may also wish to do . mysql_error()); which prints out a nice error telling you the exact problem.

Of course, this might not be where the problem is. What doesn't work?

This post was edited on 06-20-2008 at 07:41 AM by andrewdodd13.
[Image: AndrewsStyle.png]
06-20-2008 07:40 AM
Profile E-Mail PM Web Find Quote Report
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. RE: MySQL
I get no MySQL errors after testing the page, but it simply does not edit the data entered through the form.
...there used to be a signature here :)
06-20-2008 07:44 AM
Profile PM Web Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: MySQL
$sql = "UPDATE `news` SET `title`='$title', `message`='$message', `who`='$who' WHERE `id`=$id";

i think it may be an issue with it not understanding it properly... the `` help alot... you should learn to do this with your mysql queries...

EDIT: also i dont see you using a mysql_close(); anywhere... this probably wont be the issue but its good to close you connections

This post was edited on 06-21-2008 at 01:01 AM by NanaFreak.
06-20-2008 08:01 AM
Profile PM Find Quote Report
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
RE: MySQL
Further to the above, echo $sql as a debugging step, make sure it's correct.
[Image: AndrewsStyle.png]
06-20-2008 03:29 PM
Profile E-Mail PM Web Find Quote Report
Volv
Skinning Contest Winner
*****

Avatar

Posts: 1233
Reputation: 31
34 / Male / Flag
Joined: Oct 2004
RE: MySQL
And escape your variables.
06-20-2008 03:34 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On