473,394 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

php/mysql update problem and switch execution question

osward
38
Hello there,

I am using phpnuke 8.0 to build my website, knowing little on php programing.

I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful.

Here is the 1st problem I encounter:

I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.

Expand|Select|Wrap|Line Numbers
  1. function EventsEdit($eid) {
  2.         global $bgcolor2, $bgcolor4, $prefix, $db, $multilingual, $admin_file, $AllowableHTML;
  3.         include("header.php");
  4.         GraphicAdmin();
  5.         OpenTable();
  6.         echo "<center><font class=\"title\"><b>"._EDITEVENT."</b></font></center>";
  7.         CloseTable();
  8.         echo "<br>";
  9.         $eid = intval($eid);
  10.         $result = $db->sql_query("select * from ".$prefix."_event_cat where eid=$eid");
  11.         while ($row = $db->sql_fetchrow($result)) {
  12.         $code = filter($row['code'], "nohtml");
  13.         $code1 = $code;
  14.         $name = filter($row['name'], "nohtml");
  15.         $desc = $row['desc'];
  16.         $date = filter($row['date'], "nohtml");
  17.         $time = filter($row['time'], "nohtml");
  18.         $place = filter($row['place'], "nohtml");
  19.         $cost = filter($row['cost'], "nohtml");
  20.         $host = filter($row['host'], "nohtml");
  21.         $vacancy = intval($row['vacancy']);
  22.         $active = intval($row['active']);
  23.         OpenTable();
  24.         echo "<br>";
  25.         echo "<table border=\"0\" width='100%'>"
  26.         ."<form action=\"".$admin_file.".php\" method=\"post\">"
  27.         ."<tr><td>"._CODE.":</td><td>"
  28.         ."<input type=\"text\" name=\"code\" value=\"$code\" size=\"30\" maxlength=\"60\"></td></tr>"
  29.         ."<tr><td>"._EVENTNAME."</td><td>"
  30.         ."<input type=\"text\" name=\"name\" value=\"$name\" size=\"30\" maxlength=\"60\"></td></tr>";
  31.         echo "<tr><td>"._DESC.":</td>"
  32.         ."<td><textarea wrap=\"virtual\" cols=\"70\" rows=\"15\" name=\"desc\">$desc</textarea><br>"._255CHARMAX."</td></tr>";
  33.         echo "<tr><td>"._EVENTDATE.":</td>"
  34.         ."<td><input type=\"text\" name=\"date\" value=\"$date\" size=\"30\" maxlength=\"60\"></td></tr>"
  35.         ."<tr><td>"._EVENTTIME.":</td>"
  36.         ."<td><input type=\"text\" name=\"time\" value=\"$time\" size=\"30\" maxlength=\"60\"></td></tr>"
  37.         ."<tr><td>"._PLACE.":</td>"
  38.         ."<td><input type=\"text\" name=\"place\" value=\"$place\" size=\"30\" maxlength=\"60\"></td></tr>";
  39.         echo "<tr><td>"._COST.":</td>"
  40.         ."<td><input type=\"text\" name=\"cost\" value=\"$cost\" size=\"30\" maxlength=\"60\"></td></tr>"
  41.         ."<tr><td>"._HOST.":</td>"
  42.         ."<td><input type=\"text\" name=\"host\" value=\"$host\" size=\"30\" maxlength=\"60\"></td></tr>"
  43.         ."<tr><td>"._VACANCY.":</td>"
  44.         ."<td><input type=\"text\" name=\"vacancy\" value=\"$vacancy\" size=\"30\" maxlength=\"100\"></td></tr>";
  45.         if ($active == 1) {
  46.             $sel1 = "checked";
  47.             $sel2 = "";
  48.         } elseif ($active == 0) {
  49.             $sel1 = "";
  50.             $sel2 = "checked";
  51.         }
  52.         echo "<tr><td>"._EVENTACTIVE."</td><td><input type=\"radio\" name=\"active\" value=\"1\" $sel1>"._YES." &nbsp;&nbsp;"
  53.         ."<input type=\"radio\" name=\"active\" value=\"0\" $sel2>"._NO."</td></tr>";
  54.         echo "<tr><td&nbsp;</td><td>&nbsp;</td></tr>";    
  55.         echo "<tr><td>&nbsp;</td>"
  56.         ."<td><input type=\"hidden\" name=\"eid\" value=\"$eid\">"
  57.         ."<input type=\"hidden\" name=\"op\" value=\"EventsEditSave\">"
  58.         ."<input type=\"submit\" value=\""._SAVECHANGES."\"></td></tr></table></form>";
  59.         CloseTable();
  60.         include("footer.php");
  61.         }
  62.     }        
  63.  
  64.     function EventsEditSave($eid, $code, $name, $desc, $date, $time, $place, $cost, $host, $vacancy, $active) {
  65.         global $prefix, $db, $admin_file, $stop;
  66.  
  67.  
  68.         $db->sql_query("UPDATE ".$prefix."_event_cat SET code='$code', name='$name', desc='$desc', date='$date', time='$time', place='$place', cost='$cost', host='$host', vacancy='$vacancy', active='$active' WHERE eid=$eid") or die(mysql_error());            
  69.         Header("Location: ".$admin_file.".php?op=EventsAdmin");
  70.     }
  71.  
  72.  
  73.  
I had cut out the error check section of the function EventsEditSave for easy reading purpose.

The "or die(mysql_error());" is what I place after the update statement to identify the problems

and here is the mysql_error() returns
quote of error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='Test', date='2007-11-30', time='18:00-20:00', place='Beijing', cost='50', ' at line 1
unquote of error

I had spent hours to alter the Update statement but with no luck. Anyone may spot the error would be nice.

My 2nd problem is to use how to use the define switch

Expand|Select|Wrap|Line Numbers
  1.  
  2.     switch($sortby) {
  3.  
  4.        case "A_date":
  5.      $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by date ASC LIMIT 0, 20");
  6.        break;
  7.  
  8.        case "A_code":
  9.       $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by code ASC LIMIT 0, 20");
  10.        break;
  11.  
  12.        case "D_date":
  13.       $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by date DESC LIMIT 0, 20");
  14.        break;
  15.  
  16.        case "D_code":
  17.       $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by code DESC LIMIT 0, 20");
  18.        break;
  19.  
  20.        default:
  21.       $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by date ASC LIMIT 0, 20");
  22.  
  23. }
  24.  
orginally it was the default case in the switch to get the information from the database table and display it as a table, I want to add function by putting a link in the table heading to let user sort 2 coloums of the display table. I create the switch but I don't know how to call it, particular in the area that needs the default statement to reterieve the data. I am not sure I made myself clear or not

I enclosed the orginal section of code below
Expand|Select|Wrap|Line Numbers
  1. function EventsMain($user) {
  2.         global $bgcolor2, $bgcolor1,$stop, $prefix, $db, $user;
  3.  
  4.         include("header.php");
  5.             OpenTable();
  6.             echo "<center><font class=\"title\"><b>"._EVENTCAT."</b></font></center>";
  7.         echo "<br>";
  8.         // Display Event List
  9.  
  10.         echo "<table width=\"100%\" border=\"1\">"
  11.         ."<tr><td colspan=\"9\" bgcolor=\"$bgcolor2\"><center>"._EVENTLIST."</font></center></td></tr>"
  12.         ."<tr><td align=\"center\" bgcolor=\"$bgcolor2\">"._EVENTDATE."</td>"
  13.             ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._CODE."</td>"
  14.             ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._EVENTNAME."</td>"
  15.         ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._HOST."</td>"
  16.         ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._EVENTTIME."</td>"
  17.             ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._PLACE."</td>"
  18.         ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._COST."</td>"
  19.             ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._AVAIL."</td>"
  20.             ."<td align=\"center\" bgcolor=\"$bgcolor2\">"._STATUS."</td>"
  21.               ."</tr>";
  22.  
  23.  
  24.             $now = Date(Y-m-d);
  25. /*** This is where I need to call the switch or replace with the switch***/
  26.  
  27.             $result = $db->sql_query("select eid, code, name, date, time, place, cost, host, vacancy, active from ".$prefix."_event_cat WHERE date > $now ORDER by date DESC LIMIT 0, 20");
  28.  
  29. /*********************************************/
  30.  while ($row = $db->sql_fetchrow($result)) {
  31.             $eid = intval($row['eid']);
  32.             $code = filter($row['code'], "nohtml");
  33.             $name = filter($row['name'], "nohtml");
  34.             $date = filter($row['date'], "nohtml");
  35.             $time = filter($row['time'], "nohtml");
  36.             $place = filter($row['place'], "nohtml");
  37.             $cost = filter($row['cost'], "nohtml");
  38.             $host = filter($row['host'], "nohtml");
  39.             $vacancy = intval($row['vacancy']);
  40.             $active = intval($row['active']);
  41.             $result1 = $db->sql_query("select eid, code, name from ".$prefix."_event_user where eid=$eid");
  42.             $confirm = $db->sql_numrows($result1);
  43.             $result1 = $db->sql_query("select eid, code, name from ".$prefix."_event_user_temp where eid=$eid");
  44.             $temp = $db->sql_numrows($result1);
  45.             $available = $vacancy-($confirm+$temp);
  46.             echo "<tr>"
  47.             ."<td align=\"center\">$date</td>"
  48.             ."<td align=\"center\">$code</td>"
  49.             ."<td align=\"center\">$name</td>"
  50.             ."<td align=\"center\">$host</td>"
  51.             ."<td align=\"center\">$time</td>"
  52.             ."<td align=\"center\">$place</td>"
  53.             ."<td align=\"center\">$cost</td>"
  54.             ."<td align=\"center\">$available</td>";
  55.             echo "<td align=\"center\"><font class=\"content\">&nbsp;<a href=\"".$admin_file.".php?op=EventsEdit&amp;eid=$eid\"><img src=\"images/edit.gif\" alt=\""._EDITEVENT."\" title=\""._EDITEVENT."\" border=\"0\" width=\"17\" height=\"17\"></a>";
  56.             //echo "<a href=\"".$admin_file.".php?op=EventsDelete&amp;eid=$eid\"><img src=\"images/delete.gif\" alt=\""._DELETEEVENT."\" title=\""._DELETEEVENT."\" border=\"0\" width=\"17\" height=\"17\"></a>  ";
  57.  
  58.             $event_act = $active;
  59.             if ($active == 1) {
  60.                 $active = "<img src=\"images/active.gif\" alt=\""._EVENTACTIVE."\" title=\""._EVENTACTIVE."\" border=\"0\" width=\"16\" height=\"16\">";
  61.                 $change = "<img src=\"images/inactive.gif\" alt=\""._EVENTNOTACTIVE."\" title=\""._EVENTNOTACTIVE."\" border=\"0\" width=\"16\" height=\"16\">";
  62.             } elseif ($active == 0) {
  63.                 $active = "<img src=\"images/inactive.gif\" alt=\""._EVENTNOTACTIVE."\" title=\""._EVENTNOTACTIVE."\" border=\"0\" width=\"16\" height=\"16\">";
  64.                 $change = "<img src=\"images/active.gif\" alt=\""._EVENTACTIVE."\" title=\""._EVENTACTIVE."\" border=\"0\" width=\"16\" height=\"16\">";
  65.                 }
  66.             echo "<td align=\"center\"><font class=\"content\">&nbsp;$active</a> </td></tr>";
  67.                     }
  68.             echo "</table>"
  69.             ."<br><br>";
  70.             CloseTable();
  71.             echo "<br><br>";
  72.             include("footer.php");
  73.             }
  74.  
I hope I made myself clear on why I want to do and please help

Thanks in advance
Oct 20 '07 #1
2 2861
pbmods
5,821 Expert 4TB
Heya, osward. Welcome to TSDN!

You're getting the first error because 'desc' is a MySQL keyword.

Try enclosing your column and field names in backticks (`).

E.g., `desc` instead of desc.

Using backticks is a good habit to get into, as it makes your code easier to read, and consequently easier to decipher three months later when you have to come back to something that you don't remember writing.
Oct 20 '07 #2
osward
38
Thanks for the tips, as I mentioned I am not a "programer" but try to assemby blocks of code to make things happens.

I will remember to avoid using description or desc for any table field name but content instead. I might change the field name in the database table as well to avoid any problem.

I search over the web and found some handy javascript for sorting coloum table that might solved my 2nd problem. However, I am still keen to learn how to call the switch. I know I call the switch in sending form like

<input type=\"hidden\" name=\"op\" value=\"EventsAdd\">

where this is common amoung the code I read within phpnuke's module but could not figure out how am I going to use the switch in my 2nd problem. Anyone could tip me or point me to a reference

Thanks
Oct 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
11
by: Durai | last post by:
Hi All, I tested "concurrent testing" in MySQL. It works fine. But I couldn't do in PostgreSQL 7.3.4 on HPUX IPF. I got deadlock problem. I used the PHP script to update table( one script...
3
by: Chad J. Lemmer | last post by:
We purchased 2 new HP DL385 servers that will be used for MySQL 5 in a replication environment. Server 1 (Master) is connected gigabit to our Cisco 3550 core switch The Cisco 3550 core switch...
3
by: elyob | last post by:
Okay, I'm about to add some checkboxes to a form, and then store the data in a column on a MySQL database. Just wanted to know best practice, at the moment I'm writing and thinking .. God that's...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.