Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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

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
syedasimmeesaq
Asked:
syedasimmeesaq
  • 4
  • 3
  • 2
  • +2
1 Solution
 
hieloCommented:
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
 
Michael701Commented:

        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
 
Michael701Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
syedasimmeesaqAuthor Commented:
I changed it to if((substr($row[0],0,20) == "table_customer_83452"))

still doesn't work
0
 
syedasimmeesaqAuthor Commented:
just noticed, I have a braces missing {
I fixed that too..still not working
0
 
hieloCommented:
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
 
elfe69Commented:
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
 
elfe69Commented:
You could try using mysql_fetch_assoc() instead of mysql_fetch_row() and mysql_fetch_array().
0
 
elfe69Commented:
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
 
syedasimmeesaqAuthor Commented:
Thanks
0
 
KhoiNqqCommented:
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
 
KhoiNqqCommented:
Oops, my response come last :-), but hope it may help
0
 
elfe69Commented:
Looks like mysql_fetch_row() was inappropriate...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now