Solved

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

Posted on 2007-11-15
13
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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 …

734 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