Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
php variable in a sql update table
Old 09-18-2006, 11:59 PM php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
to put it simple, her is what i have:

PHP Code:
 mysql_query("UPDATE layout SET `set` = '$set', 
that works, but what i want to do is this:

PHP Code:
 mysql_query("UPDATE $layout SET `set` = '$set', 
this dose not work, what can i do?
atomicshockwave is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-19-2006, 07:17 AM Re: php variable in a sql update table
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Try surrounding your vars with{}

I.E.
mysql_query("UPDATE layout SET `set` = '{$set}',

ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 09-19-2006, 12:20 PM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
no that dident work for the tabel, i need the tabel name to be a var like:

mysql_query("UPDATE $layout SET `set` = '$set',
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 02:06 PM Re: php variable in a sql update table
Extreme Talker

Posts: 246
Trades: 3
You should be able to build this string just like any other string. Make sure the variables are assigned before you build the string.

What do you get if you :
PHP Code:
echo "UPDATE $layout SET `set` = '$set'"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 03:13 PM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
it echos the resalts from the var $layout

but it still dosent work in the normal script.
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 03:30 PM Re: php variable in a sql update table
Extreme Talker

Posts: 246
Trades: 3
What it sounds like you are saying is you can "hard-code" the sql statement, and it works, but when you substitute a variable for one word in the sql statement, it does not work.

if (hard_coded_statement === code_generated_string) {
you won't get an error
}

Are you getting an error message? Is the variable $layout returning a SQL keyword? Is code before or after this line affecting something? Sometimes it seems that the error must be in one line, when in fact, it's in another.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by CouponGuy; 09-19-2006 at 03:32 PM..
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 04:12 PM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
no, there is no error, it just dosent work. whin I use the var it simply dosent update the tabel. the var works evrywhere else just not for the tabel name. here I will post my whole update code, maybe that would help.

PHP Code:
 function update_db($data_array$col_check)
{

  foreach(
$data_array AS $set => $items)
  {
     
$i 0;
     foreach(
$items AS $item)
     {
       
$item mysql_escape_string($item);
       
$set  mysql_escape_string($set);
       
       
mysql_query("UPDATE $username SET `set` = '$set', `order` = '$i'  WHERE `item` = '$item$col_check");
       
$i ++;
     }
  }


Last edited by atomicshockwave; 09-19-2006 at 04:15 PM.. Reason: wrog info
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 04:24 PM Re: php variable in a sql update table
Extreme Talker

Posts: 246
Trades: 3
I think you are going about it correctly. And this is a very good way to do it. But you just need to make sure that the string you are sending to mysql_query() is proper SQL syntax, and has the correct (existing) tablename, etc.

I understand the need to protect code, but if you are really stuck, then the best way to get help is to show a little code, the error message (if any), and in this example, show the SQL string that gets echoed (after PHP replaced the variable $layout with the string "user", or whatever the case may be).

Also, if $layout is the prefix to a SQL table, then you definitely want to surround it with {}, like ibbo said, so it isn't looking for a different variable.

PHP Code:
//set var
$layout "user"

//this will NOT return "user_layout_table";
echo "$layout_password_table";

//this WILL return "user_layout_table";
echo "{$layout}_password_table"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 09-19-2006, 04:41 PM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
this is not a big part of my scripting job I am working on, just a little side job so here is all the affected sql update/post script.

PHP Code:
$username layout;

mysql_connect('localhost''username''password');
mysql_select_db('database');
function 
parse_data($data)
{
  
$containers explode(":"$data);
  foreach(
$containers AS $container)
  {
      
$container str_replace(")"""$container);
      
$i 0;
      
$lastly explode("("$container);
      
$values explode(","$lastly[1]);
      foreach(
$values AS $value)
      {
        if(
$value == '')
        {
            continue;
        }
        
$final[$lastly[0]][] = $value;
        
$i ++;
      }
  }
    return 
$final;
}

function 
update_db($data_array$col_check)
{

  foreach(
$data_array AS $set => $items)
  {
     
$i 0;
     foreach(
$items AS $item)
     {
       
$item mysql_escape_string($item);
       
$set  mysql_escape_string($set);
       
       
mysql_query("UPDATE '{$username}' SET `set` = '$set', `order` = '$i'  WHERE `item` = '$item$col_check");
       
$i ++;
     }
  }
}

// Lets setup Sajax
require_once('Sajax.php');
sajax_init();
// $sajax_debug_mode = 1;

function sajax_update($data)
{
  
$data parse_data($data);
  
update_db($data"AND (`set` = 'sajax1' OR `set` = 'sajax2'  OR `set` = 'sajax3')");
  return 
'y';
}

sajax_export("sajax_update");
sajax_handle_client_request(); 
why I dont understand, is because it dose work here:

PHP Code:
 <ul id="sajax3" class="sortable boxy">
  <?php
$r 
mysql_query("SELECT * FROM $username WHERE `set` = 'sajax3' ORDER BY `order` ASC");
while(
$rw mysql_fetch_array($r))
{
  echo 
'<li id="'.$rw['item'].'">'.$rw['item'].'</li>';
}
?>

</ul>

Last edited by atomicshockwave; 09-19-2006 at 05:49 PM.. Reason: more info
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 09-20-2006, 01:42 AM Re: php variable in a sql update table
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
How about changing to:

mysql_query("UPDATE `$username` SET `set` = '$set', `order` = '$i' WHERE `item` = '$item' $col_check");
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 09-20-2006, 07:37 AM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
nope, still no go. :-(
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 09-20-2006, 08:08 AM Re: php variable in a sql update table
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
Got It!!!!!

here is the gode that did it:

PHP Code:
function update_db($data_array$col_check)
{

  foreach(
$data_array AS $set => $titles)
  {
     
$i 0;
     foreach(
$titles AS $title)
     {
       
$title mysql_escape_string($title);
       
$set  mysql_escape_string($set);
       
$username layout;
       
       
mysql_query("UPDATE `$username` SET `set` = '$set', `order` = '$i'  WHERE `title` = '$title$col_check");
       
$i ++;
     }
  }

atomicshockwave is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to php variable in a sql update table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34164 seconds with 12 queries