Solved

Why this code doesn't update my table with the values in link field

Posted on 2007-11-15
13
256 Views
Last Modified: 2013-12-13
I have this code and its not updating the values ..can someone please help

<?php

require_once('main.php');  
$result = mysql_list_tables($DB);
 
if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
 
while ($row = mysql_fetch_row($result)) {
        if((substr($row[0],0,16) == "table_customer_83452"))
            
        {
     $id = substr($row[0],-5);
       echo $id."<BR>";
       $qu = "Select token from ".$row[0];
       echo $qu."<BR>";
       $tableresult = mysql_query($qu);
       while($tablerow = mysql_fetch_array($tableresult)){

       $link = "http://mywebsite.com/sweetwater1/index.php?sid=".urlencode($id)."&token=".urlencode($tablerow[0])."&lang=en";
     $query = "UPDATE ".$row[0]." SET linkadd = '$link' WHERE token = $tablerow[0]";
               
            //$query = "UPDATE " . $row[0] . " SET firsname='$firstname',lastname='$lastname',number='$number'";
           // $resultUpdate = mysql_query($query);
           // if(!$resultUpdate)
             //   die(mysql_error());
                   mysql_query($query);
     echo mysql_error();
        }
}

It just gives me a blank page.
Thanks
0
Comment
Question by:syedasimmeesaq
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20296161
You need to connect to the database first. I suspect main.php there is no code to connect to the db.

require_once('main.php');  
//provide the appropriate info below
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}

$result = mysql_list_tables($DB);
0
 
LVL 19

Expert Comment

by:Michael701
ID: 20296313

        if((substr($row[0],0,16) == "table_customer_83452"))

this will NEVER be true
"table_customer_83452" is 20 characters long

try
        if((substr($row[0],0,19) == "table_customer_83452"))

btw; YIKES, do you have a different table for each and every customer?
0
 
LVL 19

Expert Comment

by:Michael701
ID: 20296331
oh, lookup the substr it might have to be
if((substr($row[0],0,20) == "table_customer_83452"))

I been coding in too many different languages tonight. sometimes it's length, other times it's ending position.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20296391
I changed it to if((substr($row[0],0,20) == "table_customer_83452"))

still doesn't work
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20296402
just noticed, I have a braces missing {
I fixed that too..still not working
0
 
LVL 82

Expert Comment

by:hielo
ID: 20296501
Inside the while, right before the first if, put:
echo( substr($row[0],0,20) );
to see if there is any "table_customer_83452"
It's possible that the string does not exist in any of those records or may be spelled differently (Uppecase, or mixed case maybe).
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:elfe69
ID: 20296617
Use the die() function every time your run a query, it will show you the SQL errors:

mysql_query($query) or die(mysql_error());
0
 
LVL 11

Expert Comment

by:elfe69
ID: 20296643
You could try using mysql_fetch_assoc() instead of mysql_fetch_row() and mysql_fetch_array().
0
 
LVL 11

Accepted Solution

by:
elfe69 earned 500 total points
ID: 20296653
Here is a portion of code that may help you:

require_once('main.php');  
//provide the appropriate info below
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}

$result = mysql_list_tables($DB);
if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
$found = false;
$tablename = "table_customer_83452";
while ($row = mysql_fetch_assoc($result)) {
      $table = substr($row[0],0,20);
    if(($table == $tablename)) {
            $id = substr($row[0],-5);
            echo $id."<BR>";
            $qu = "Select token from ".$row[0];
            echo $qu."<BR>";
            $tableresult = mysql_query($qu);
            while($tablerow = mysql_fetch_assoc($tableresult)){
                  $link = "http://mywebsite.com/sweetwater1/index.php?sid=".urlencode($id)."&token=".urlencode($tablerow[0])."&lang=en";
                  $query = "UPDATE ".$row[0]." SET linkadd = '$link' WHERE token = $tablerow[0]";
                  mysql_query($query);
                  echo mysql_error();
            }
            $found = true;
      }
}
if (!$found) {
      echo 'table ', $tablename, ' not found';
}
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31409497
Thanks
0
 
LVL 3

Expert Comment

by:KhoiNqq
ID: 20296677
Firstly, a blank page when executing the script may caused by a PHP configuration directive,
in the development phase, you may want to turn it off, place the following in the begin of your script:
<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", "1");
?>
This may help you to see what happend.
0
 
LVL 3

Expert Comment

by:KhoiNqq
ID: 20296680
Oops, my response come last :-), but hope it may help
0
 
LVL 11

Expert Comment

by:elfe69
ID: 20296992
Looks like mysql_fetch_row() was inappropriate...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now