Topic awaiting preservation: php Database update |
|
---|---|
Author | Thread |
Bipolar (III) Inmate From: 1393 |
posted 08-25-2005 15:55
Hey all, I'm just learning how to use php to connect to a database and utilize/manipulate the info. I've started coding out a very simple script that allows someone to add a link to their website (like a 'links' page). My database table has four fields, id (auto increment), link, linkName, desc (description). I've managed to connect, add, and display links with no troubles at all; but when I tried to add the ability to update the links things got weird. code: mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $look = "SELECT * FROM test"; $result = mysql_query($look); $num = mysql_numrows($result); mysql_close(); <ul> for ($i = 0; $i < $num; $i++) { $id = mysql_result($result, $i, "id"); $link = mysql_result($result, $i, "link"); $linkName = mysql_result($result, $i, "linkName"); $desc = mysql_result($result, $i, "desc"); echo "<li><a href=\"update.php?id={$id}\"><b>{$linkName}</b> :: Update</a><br> <div style=\"padding-left: 15px; font-size: 11px; color: #333333;\">{$desc}</div></li>"; } </ul>
code: $id = $_GET['id']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query1 = "SELECT * FROM test WHERE id='$id'"; $result = mysql_query($query1); $num = mysql_numrows($result); mysql_close(); <form action="testUpdate.php" method="post"> for ($i = 0; $i < $num; $i++) { $id = mysql_result($result, $i, "id"); $link = mysql_result($result, $i, "link"); $linkName = mysql_result($result, $i, "linkName"); $desc = mysql_result($result, $i, "desc"); echo "<input type=\"hidden\" name=\"ud_id\" value=\"{$id}\"><br> Link Name: <input type=\"text\" name=\"ud_linkName\" value=\"{$linkName}\"><br> Link: <input type=\"text\" name=\"ud_link\" value=\"{$link}\"><br> Short Description: <input type=\"text\" name=\"ud_desc\" value=\"{$desc}\"><br><br>"; } <input type="submit" value="Update"> </form>
code: $ud_id = $_POST['ud_id']; $ud_first = $_POST['ud_link']; $ud_linkName = $_POST['ud_linkName']; $ud_desc = $_POST['ud_desc']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query1 = "UPDATE test SET link='$ud_link', linkName='$ud_linkName' desc='$ud_desc' WHERE id='$ud_id'"; mysql_query($query1); mysql_close();
|
Maniac (V) Inmate From: under the bed |
posted 08-25-2005 18:22
You are missing a comma - code: linkName='$ud_linkName' desc='$ud_desc'
code: linkName='$ud_linkName', desc='$ud_desc'
code: mysql_query($query1);
code: mysql_query($query1) or DIE (mysql_error());
|
Bipolar (III) Inmate From: 1393 |
posted 08-25-2005 18:37
Crap, sorry that comma really is there when I test it, I just messed up when I added it to show... I'll try your idea to test the query, thanks! |
Maniac (V) Inmate From: under the bed |
posted 08-25-2005 19:46
Also, I found this tutorial at the GN invaluable when I first started playing with PHP/MySql - |
Bipolar (III) Inmate From: Australia |
posted 08-26-2005 01:48
Ive noticed if your displaying this error in html or a messagebox etc, you may need to addslasshes for it to appear. |
Maniac (V) Inmate From: Sthlm, Sweden |
posted 08-26-2005 08:40
some things to take note of. code: $query1 = "UPDATE test SET link='$ud_link', linkName='$ud_linkName' desc='$ud_desc' WHERE id='$ud_id'";
code: $query1 = "UPDATE test SET link='".$ud_link."', linkName='".$ud_linkName."', desc='".$ud_desc."' WHERE id='".$ud_id."'";
|
Bipolar (III) Inmate From: Australia |
posted 08-26-2005 11:52
Wont the first one actually do the same thing? If you havent set $ud_link to anything, it will treat it as "" wont it? Infact if you wanted to put $ud_link into your database you would need to do "$"."ud_link" so that it didnt parse it. |
Maniac (V) Inmate From: Sthlm, Sweden |
posted 08-26-2005 13:19
That suggestion from me is more a recommendation than a bugfix in this example. |
Bipolar (III) Inmate From: Australia |
posted 08-26-2005 16:39
yeh, i have started to do that - was just making sure i was thinking right. These days I use single quote, even for outputting html stuff because you dont have to excape the double quotes which i guess would also put less load on the parser. |
Bipolar (III) Inmate From: 1393 |
posted 08-27-2005 06:12
Alright, I've still been unseccesful getting the danged desc to update. BTW all this info (and the tutorial) have been awesome, I'm understanding this a lot better, thanks to everyone! code: <?php require("linkFunctions.php"); ?> <?php $action = $_GET['action']; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>Link updater</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> body, table { background-color: #EEEEEE; font-family: Verdana, sans-serif; font-size: 11px; margin: 30px auto 30px auto; } hr {height: 1px; border: solid #EEEEEE 0px; border-top-width: 1px;} form {margin: 0;} form div { text-align: right; padding: 0 25px 3px 0; } .fancyForm { font-family: Verdana, sans-serif; font-size: 11px; color: #333333; border: 1px solid #333333; } .fancyBtn { font-family: Verdana, sans-serif; font-size: 11px; background-color: #526E35; color: #FFFFFF; border: 1px solid #000000; } table.shell { margin: auto; width: 320px; border: 1px solid #000000; background-color: #FFFFFF; } td.topLinks { background-color: #526E35; border-bottom: 1px solid #000000; padding: 3px; font-weight: bold; text-align: center; } td.topLinks a {padding: 0 3px 0 3px;} td.topLinks a:link {color: #FFFFFF; text-decoration: none;} td.topLinks a:visited {color: #FFFFFF; text-decoration: none;} td.topLinks a:hover {color: #FFFFFF; text-decoration: underline;} td.topLinks a:active {color: #FFFFFF; text-decoration: none;} td.main { background-color: #FFFFFF; padding: 2px; } td.main a {padding: 0 3px 0 3px;} td.main a:link {color: #526E35; text-decoration: none;} td.main a:visited {color: #526E35; text-decoration: none;} td.main a:hover {color: #000000; text-decoration: none; background-color: #EEEEEE; border-bottom: 1px solid #000000;} td.main a:active {color: #526E35; text-decoration: none;} </style> </head> <body> <table class="shell" align="center" cellpadding="0" cellspacing="0"> <tr> <td class="topLinks"><a href="index.php">Display Links</a> | <a href="index.php?action=addForm">Add Link</a> | <a href="index.php?action=updateSelect">Update Link</a> </td> </tr> <tr> <td class="main"> <hr> <?php if ($action != "addForm" && $action != "addLink" && $action != "updateSelect" && $action != "updateForm" && $action != "update") { displayLinks(); } if ($action == "addForm") { echo "<form action=\"index.php?action=addLink\" method=\"post\"> <div>Link Name: <input class=\"fancyForm\" type=\"text\" name=\"linkName\"></div> <div>Link: <input class=\"fancyForm\" type=\"text\" name=\"link\"></div> <div>Short Description: <input class=\"fancyForm\" name=\"desc\" type=\"text\"></div> <hr> <div><input class=\"fancyBtn\" type=\"Submit\" value=\"Add Link\"></div> </form>"; } if ($action == "addLink") { addLink(); } if ($action == "updateSelect") { updateSelect(); } if ($action == "updateForm") { updateForm(); } if ($action == "update") { update(); } ?> <hr> </td> </tr> </table> </body> </html>
code: <?php /////////////////////////////////////////////////////////////////////////// function connectDB() { $host = "*****"; $username = "*****"; $password = "*****"; $database = "*****"; mysql_connect($host, $username, $password); @mysql_select_db($database) or die( "Unable to select database"); } ///////////////////////////////////////////////////////////////////////////// function addLink() { $link = $_POST['link']; $linkName = $_POST['linkName']; $desc = $_POST['desc']; connectDB(); $query = "INSERT INTO test VALUES ('', '$link', '$linkName', '$desc')"; mysql_query($query); mysql_close(); echo "<div align=\"center\"><b>{$linkName}</b> Added Successfully!<br><br><a href=\"index.php\">Display Link(s)</a></div>"; } //////////////////////////////////////////////////////////////////////////// function displayLinks() { connectDB(); $query = "SELECT * FROM test"; $result = mysql_query($query); $num = mysql_numrows($result); mysql_close(); echo "<ul>"; for ($i = 0; $i < $num; $i++) { $id = mysql_result($result, $i, "id"); $link = mysql_result($result, $i, "link"); if (strncmp("http://", $link, 7) == 1 || strncmp("http://", $link, 7) == -1) { if (strncmp("https://", $link, 8) == 0) { $link = $link; } else { $link = "http://".$link; } } $linkName = mysql_result($result, $i, "linkName"); $desc = mysql_result($result, $i, "desc"); echo "<li><a href=\"{$link}\" target=\"_blank\"><b>{$linkName}</b></a><br> <div style=\"padding-left: 15px; font-size: 11px; color: #333333;\">{$desc}</div> </li>"; } echo "</ul>"; } //////////////////////////////////////////////////////////////////////////// function updateSelect() { connectDB(); $query = "SELECT * FROM test"; $result = mysql_query($query); $num = mysql_numrows($result); mysql_close(); echo "<ul>"; for ($i = 0; $i < $num; $i++) { $id = mysql_result($result, $i, "id"); $link = mysql_result($result, $i, "link"); $linkName = mysql_result($result, $i, "linkName"); $desc = mysql_result($result, $i, "desc"); echo "<li><a href=\"index.php?action=updateForm&id={$id}\"><b>{$linkName}</b> > <span style=\"color: #990000;\">Update</span></a><br> <div style=\"padding-left: 15px; font-size: 11px; color: #333333;\">{$desc}</div></li>"; } echo "</ul>"; } //////////////////////////////////////////////////////////////////////////// function updateForm() { $id = $_GET['id']; connectDB(); $query = "SELECT * FROM test WHERE id='$id'"; $result = mysql_query($query); $num = mysql_numrows($result); mysql_close(); for ($i = 0; $i < $num; $i++) { $id = mysql_result($result, $i, "id"); $link = mysql_result($result, $i, "link"); $linkName = mysql_result($result, $i, "linkName"); $desc = mysql_result($result, $i, "desc"); echo "<form action=\"index.php?action=update\" method=\"post\"> <input type=\"hidden\" name=\"ud_id\" value=\"{$id}\"> <div>Link Name: <input class=\"fancyForm\" type=\"text\" name=\"ud_linkName\" value=\"{$linkName}\"></div> <div>Link: <input class=\"fancyForm\" type=\"text\" name=\"ud_link\" value=\"{$link}\"></div> <div>Short Description: <input class=\"fancyForm\" type=\"text\" name=\"ud_desc\" value=\"{$desc}\"></div> <hr> <div><b>Delete Link?</b><br> Yes <input type=\"radio\" name=\"delete\" value=\"yes\"> No <input type=\"radio\" name=\"delete\" value=\"no\" checked></div> <hr> <div><input class=\"fancyBtn\" type=\"submit\" value=\"Update Link\"></div> </fom>"; } } //////////////////////////////////////////////////////////////////////////// function update() { $ud_id = $_POST['ud_id']; $ud_link = $_POST['ud_link']; $ud_linkName = $_POST['ud_linkName']; $ud_desc = $_POST['ud_desc']; $delete = $_POST['delete']; connectDB(); if ($delete == "no") { $query = "UPDATE test SET link = '$ud_link', linkName = '$ud_linkName', desc = '$ud_desc' WHERE id = '$ud_id'"; $display = "<div align=\"center\"><b>{$ud_linkName}</b> Updated!<br><br><a href=\"index.php\">Display Link(s)</a></div>"; } else if ($delete == "yes") { $query = "DELETE FROM test WHERE id='$ud_id'"; $display = "<div align=\"center\"><b>{$ud_linkName}</b> Deleted!<br><br><a href=\"index.php\">Display Link(s)</a></div>"; } mysql_query($query) or DIE (mysql_error()); mysql_close(); echo $display; } ?>
|
Bipolar (III) Inmate From: Australia |
posted 08-27-2005 09:33
What is the type of your $ud_id if its an integer or something then dont put any quotes around it. The same applies for stuff that isnt numbers, varchar, text etc - they must have quotes arround it. So check them . |
Bipolar (III) Inmate From: 1393 |
posted 08-27-2005 15:25
id is an int, everything else is varchar... code: WHERE id = '$ud_id'"; to code: WHERE id = $ud_id"; same error... I also updated updateForm() at code: <input type=\"hidden\" name=\"ud_id\" value=\"{$id}\"> to code: <input type=\"hidden\" name=\"ud_id\" value={$id}> and every combination of these two updates with the same error... |
Bipolar (III) Inmate From: 1393 |
posted 08-31-2005 04:21
Ok, I fixed it by changing 'desc' to 'description' alltogether... I think 'desc' is designated for some kind of a command or something...? Anyways, thanks again for the help. |
Bipolar (III) Inmate From: Australia |
posted 08-31-2005 05:06
with the second change you made you dont need to do that, having the quotation marks there is part of the HTML markup so you should have them. You dont need the squigly brackets though just put code: <input type=\"hidden\" name=\"ud_id\" value=\"".$id."\"> Also if you use single quotations it will save you putting alot of slashes in, you can just do print '<input type="hidden" name="ud_id" value="'.$id.'">';
code: print '<input type="hidden" name="ud_id" value="'.$id.'">' . "\n"; |
Maniac (V) Inmate From: Sthlm, Sweden |
posted 08-31-2005 16:01
in sql "desc" is commonly used in ORDER BY <collumn> DESC |
Paranoid (IV) Inmate From: 127.0.0.1 |
posted 08-31-2005 16:27
You might also want to rip out all of those IF statements in favor of a switch, and replace code: if ($action == "addForm") { echo "<form action=\"index.php?action=addLink\" method=\"post\"> <div>Link Name: <input class=\"fancyForm\" type=\"text\" name=\"linkName\"></div> <div>Link: <input class=\"fancyForm\" type=\"text\" name=\"link\"></div> <div>Short Description: <input class=\"fancyForm\" name=\"desc\" type=\"text\"></div> <hr> <div><input class=\"fancyBtn\" type=\"Submit\" value=\"Add Link\"></div> </form>"; } if ($action == "addLink") { addLink(); } if ($action == "updateSelect") { updateSelect(); } if ($action == "updateForm") { updateForm(); } if ($action == "update") { update(); }
code: switch ($action){ case addForm: echo '<form action="index.php?action=addLink" method="post"> <div>Link Name: <input class="fancyForm" type="text" name="linkName"></div> <div>Link: <input class="fancyForm" type="text" name="link"></div> <div>Short Description: <input class="fancyForm" name="desc" type="text"></div> <hr> <div><input class="fancyBtn" type="Submit" value="Add Link"></div> </form>'; break; case addLink: addLink(); break; case updateSelect: updateSelect(); break; case updateForm: updateForm(); break; case update: update(); break; default: break; } |
Bipolar (III) Inmate From: 1393 |
posted 09-01-2005 21:06
Ah yes, the switch, thanks pugzly. |