1

I have a data entry php code. I am allowing the user to select a table to be displayed on a separate page for easy access. I can get the code to insert the selected table into the "maintable" table, but I can't get any information out of that table. The insert table file won't even get the current table name from it even though on that same page I am inserting into that database. So I figure if I can fix this problem, I can fix that one, too. The maintable table has just one column with one record in one of my other databases. I am doing this so that the user doesn't have access to this directly since they can see all of the other tables in a different database.

The data-entry-header.php file has my connect statements, $connect being the one I am using for this database connection. What I get as a result is my main-form.php file with no table name. So it isn't throwing any errors, it's just not getting the table name. I know my connect statement and table/column names are correct because I have used the same one for the insert statement.

include 'data-entry-header.php';
    $keys = array();

    /* Query for Main Table Value */
    $string = 'SELECT TableName FROM maintable';
    $resultMain = mysqli_query($connect, $string) or die(mysqli_error($link));
    while ($rowMain = mysqli_fetch_row($resultMain)){
        $table = $rowMain[0];
    }

    /* Display Message or Table */
    if ($table = NULL || $table = "" ){
        echo 'No Main Table has been set. Go to the <a href="set-main-table.php">Set Main Table</a> page to select a Main Table.';
    }
    else {
        include 'main-form.php';
    }

    /* Get footer Contents */
    include "../footer.php";
7
  • Do you have error reporting enabled ? Commented Apr 19, 2013 at 13:49
  • run your query directly in the database and see what it returns. Commented Apr 19, 2013 at 13:51
  • I guess TableName should be fieldname (or columname) ? Commented Apr 19, 2013 at 13:53
  • try { $resultMain = mysqli_query($connect, $string); } catch { echo mysqli_error(); } perhaps ? Commented Apr 19, 2013 at 13:54
  • 2
    @Dave No, mysqli_query does not throw any exceptions. Commented Apr 19, 2013 at 14:01

3 Answers 3

2
if ($table = NULL || $table = "" ){

You're confusing assignment = with comparison ==

You should probably try:

if ($table == NULL || $table == "" ){
Sign up to request clarification or add additional context in comments.

2 Comments

I would prefer using if (empty($table)){}
@lefty55104 - please edit your question to show that you mean if ($table == NULL || $table == "" ){ - just to avoid same thoughts...
0

There are many other faults with your code.

  • Instead of die(), you have to use more convenient trigger_error(); which won't reveal sensitive info to anyone.
  • if you are expecting only one result, there is no point in a loop
  • PHP is a loosely typed language. No need for excessive conditions. just if($table) is enough for most cases
  • using meaningful (and consistent) variable names also helps a lot

    $sql = 'SELECT TableName FROM maintable';
    $res = mysqli_query($connect, $sql) or trigger_error(mysqli_error($connect));
    //                                            $connect, not $link ^
    $row = mysqli_fetch_row($res);
    
    /* Display Message or Table */
    if (!$row){
        echo 'No Main Table has been set. Go to the <a href="set-main-table.php">Set Main Table</a> page to select a Main Table.';
    } else {
        $table = $row[0];
        include 'main-form.php';
    }
    

2 Comments

I'm currently in debugging mode, so error codes are helpful. I plan on putting a message there once I've gone live. I'll check out trigger_error. I'm glad to hear I don't need a loop. I figured there was a way, but I wasn't sure how to go about it.
With trigger_error you will need no special debugging mode at all. Yet you will have your error message every time you need it. That's the point of using this function. Don't blind yourself: you need an error message despite of "mode" or whatever else condition. though echoing it out on a live is a suicide
-1

Instead of or die, just add this below

echo mysqli_error($link);

Also, in your query, wouldn't it be columnName FROM table?

1 Comment

Since I'm not familiar with it, why would I choose the echo over die?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.