Solved

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

Posted on 2007-11-15
13
258 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 47
How to stop user from going back to a previous page in PHP 3 30
google map tracking website 5 22
php checkbox validation 5 27
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

816 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

11 Experts available now in Live!

Get 1:1 Help Now