473,683 Members | 3,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php/mysql update problem and switch execution question

osward
38 New Member
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 2878
pbmods
5,821 Recognized Expert Expert
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 New Member
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=\"EventsA dd\">

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
11758
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
35
8338
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 for '5'. So in his switch statement, he omits a case for '5':
8
11597
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 be updated when a group of items is copied. I can successfully do this with cursors, but am experimenting with a way to do it with a single update statement. I have verified that each row being returned to the Update statement (in an...
11
5443
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 increment the column & another one decrement the column). Is the postgres support the concurrent access to update database? I got the following errors: test=# ERROR: deadlock detected ERROR: deadlock detected ERROR: deadlock detected .....
3
2016
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 is connected gigabit over fiber to a Cisco 3548 switch in another building. Server 2 (Slave) is connected to the Cisco 3548 on a 100MB switched port.
3
11843
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 ungly code ... <input type="checkbox" name="delta" <?php if ($delta==1) { echo "checked"; }?>> Delta<br> <input type="checkbox" name="mastercard" <?php if ($mastercard==1) { echo "checked"; }?>> Mastercard<br> <input type="checkbox" name="solo"...
0
11698
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 java.sql.SQLException: Deadlock found when trying to get lock; Try restarting transaction message from server: "Lock wait timeout exceeded; try restarting transaction"; We get such errors generally on inserts or updates while applying a
39
5853
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 ISO-8859-1 -t UTF-8 mydb.sql mydb_utf8.sql mysqlCREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;
0
8504
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8753
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6429
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4519
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2934
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2195
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.