Solved

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

Posted on 2007-11-15
13
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

17 Experts available now in Live!

Get 1:1 Help Now