Jump to content

kenoli

Members
  • Posts

    53
  • Joined

  • Last visited

Posts posted by kenoli

  1. I have defined my database connection data as constants in a separate file for security reasons.  How can I simply access the data stored in those constants.  I have to call the file to get the value of the constants.  If I inadvertently call the file twice in a script  using an include or require statement, I get an error that I am trying to define the constants again.  Can I somehow just call the constants without including the file where they are defined?

    --Kenoli

  2. I am getting the following error message:

    Fatal error: Uncaught Error: Array callback must have exactly two elements in /Users/studio/Sites/BannerProject/b-as/tio-new/__classes/person_form_edit_initial.php:14 Stack trace: #0 /Users/studio/Sites/BannerProject/b-as/tio-new/index.php(78): require() #1 {main} thrown in /Users/studio/Sites/BannerProject/b-as/tio-new/__classes/person_form_edit_initial.php on line 14

    for a line in my code that only has the following code:

    $person_id = $_SESSION('person_id');

    This is a simple array assignment.  Where is the "two elements" requirement?  I"m confused by the "require() #1 {main}" comment in the message.  If it refers to a require call in the index.php file, there are also no functions in that file or a file it requires that require two elements.

    --Kenoli

  3. I'm trying to do this with the code below.  I get an error because explode thinks I'm just sending it one argument since the string is held by an array element.  If I can get the array from the explode function I can insure it into the first and last name fields in my table.

    Any suggestions?

    --Kenoli

    require '__classes/Db.php';
    
    $sql = "SELECT name FROM tbl_persons";
    $stmt = $pdo->query($sql);
    $row = $stmt->fetchall(PDO::FETCH_ASSOC);
    
    foreach ($row as $name) {
    	 $names = explode($name['name']);
    	}

     

  4. 2 hours ago, Barand said:

    You can do with a single INSERT SELECT statement.

    
    
    INSERT INTO Images 
        (name, person_id, filename, description, medium)
        SELECT name
             , table_id
             , image_name
             , description
             , medium  
        FROM tbl_person_data;

    Doing it with a prepared statement, as you are, requires either

    • execute with a numerically indexed array and ? as placeholders, or
    • execute with an associative array and named placeholders matching the array keys

    I'm not sure how to implement this.  When I try, it just pulls out one record and inserts it in the new table.  I have 227 rows I want it to do this to.  Not sure how to construct a statement to do this.

    I was doing it with prepared statements using "?" placeholders as above and thought it should work, but it doesn't.  I runs, throws no errors but inserts nothing into the table.

    --Kenoli

  5. Stuck again on simple code.  I am trying to insert some fields extracted from one table into another.  I'm using code that worked elsewhere.  The SQL statement flies, the script runs, the input array is printed back

     I get an echo back from the end of the script but nothing is added to the table.  Even aded an echo  print_r in the conditional and I know the data is getting to the execute command.    The script follows with a sample of the input array.  I have attached am image of the table I am trying to insert the data into.

    --Kenoli

    The script:
    <?php
    	
    require '__classes/DB.php';
    
    $sql = "SELECT name, table_id, image_name, description, medium  FROM tbl_person_data ";
    
    $stmt = $pdo->query($sql);
    $array1 = $stmt->fetchall(PDO::FETCH_ASSOC);
    	
    	
    $stmt = $pdo->prepare("INSERT INTO Images (name, person_id, filename, description, medium) VALUES (?,?,?,?,?)");
    
        //$pdo->beginTransaction();
        foreach ($array1 as $row)
        {
            $stmt->execute($row);
        }
        
    echo "<pre>";
    print_r ($row);
    echo "</pre>";
        
    		
    echo '<h4>Got to end of file</h4>';
    	
    ?>
    
    $array1: The input array
    
    [0] => Array
            (
                [name] => Carol Lettko
                [table_id] => 21
                [image_name] => Carol_Lettko-DSC_3022.jpg
                [description] => Baby Herons/Brickyard
                [medium] => photo
            )
    
        [1] => Array
            (
                [name] => 
                [table_id] => 22
                [image_name] => Carol_Lettko-DSC_0164.JPG
                [description] => Heron/Brickyard
                [medium] => photo
            )
    
        [2] => Array
            (
                [name] => 
                [table_id] => 23
                [image_name] => Carol_Lettko-IMG_5723.jpg
                [description] => Kayaker/Brickyard
                [medium] => photo
            )
    

     

    table.png

  6. 1 hour ago, mac_gyver said:

    check is a reserved work and should not be used as a column name. either use a different name for that column or you must enclose the column name in back-ticks to cause it to be treated as an identifier.

    Thanks.  That makes sense.  I also changed "select" as I thought that might be a problem and that issue is now gone.  

    I had to fix a couple of other errors that I figured out and it worked!

    --Kenoli

  7. PS -- This is a sample of the execute array as print_r displays it.  It looks right to me:

    Array
    (
    
        [0] => Array
            (
                [0] => 1
                [1] => FALSE
                [2] => Carol Lettko
                [3] => 
                [4] => 
                [5] => TRUE
                [6] => FALSE
                [7] => Carol_Lettko-DSC_3022.jpg
                [8] => Baby Herons/Brickyard
                [9] => photo
                [10] => 
                [11] => 
                [12] => 
            )
    
        [1] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 925-285-0320
                [4] => cjl164@aol.com
                [5] => 
                [6] => 
                [7] => Carol_Lettko-DSC_0164.JPG
                [8] => Heron/Brickyard
                [9] => photo
                [10] => 
                [11] => 
                [12] => 
    )       
         

     

  8. I'm sorry to be back so soon, but I'm up against another mystery.  I'm using the code below to enter a bunch of css data from a spreadsheet into a mysql table.  I think the data file is OK.  The array created by the script checks out with print_r.  (There are many more records than shown.  I truncated it to save space.)

    The problem is that I get this error regarding my sql statement, not the data or anything else:

    Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'check, name, phone, email, entry_fee, print_fee, image_name, description, med...' at line 1 in /Users/studio/Sites/BannerProject/b-as/_test_site/csv_to_array.php:242 Stack trace: #0 /Users/studio/Sites/BannerProject/b-as/_test_site/csv_to_array.php(242): PDO->prepare('INSERT INTO tbl...') #1 {main} thrown in /Users/studio/Sites/BannerProject/b-as/_test_site/csv_to_array.php on line 242

    I've typed it in a dozen times to make sure there are no errors and keep getting the same error.

    I tried running a test file and gradually increasing the number of placeholders and at some point I always end up getting the same error,  I can delete the most recent addition and it works again.  Then I can add another placeholder exactly as before and it works the second time.  It feels like a ghost in the machine.

    Any idea what I am doing wrong?  An I typing something I don't see?

    <?php
    	
    require '__classes/Db.php';
    
    $csvData = '1,FALSE,Carol Lettko,,,TRUE,FALSE,Carol_Lettko-DSC_3022.jpg,Baby Herons/Brickyard,photo,,,
    ,,,925-285-0320,cjl164@aol.com,,,Carol_Lettko-DSC_0164.JPG,Heron/Brickyard,photo,,,
    ,,,,,,,Carol_Lettko-IMG_5723.jpg,Kayaker/Brickyard,photo,,,
    ,,,,,,,,,,,,
    2,FALSE,Louise Williams,,,TRUE,FALSE,Louise_Williams-BirdsOfAFeatherAOPR.jpg,Alligator with Words,Book Excerpt,,,
    ,,,510-232-9547,lkw@louisekwilliams.com,,,Louise_Williams-Hope-TheFairyChickenAOPR.jpg,Hope The Fairy Chicken,,,,
    ,,,The d exatrfrfvct/.*tygrvurr,,,,,,,,,
    ,,,,,,,,,,,,
    3,TRUE,Dorothy Leeland,,lelanddorothy@gmail.com,TRUE,FALSE,DJ_Lee-bridge at dusk 700px width.jpg,Bridge,photo,,,
    ,,,,,,,DJ_Lee-friends 700px width.jpg,Friends,photo,,,
    ,,,,,,,DJ_Lee-hybiscus 700 px wide.jpg,Hibiscus,photo,,,
    ,,,,,,,,,,,,
    4,FALSE,Rita Gardner,,,TRUE,FALSE,Rita_Gardner-Explosion - Gardner photo.JPG,Explosion,photo,,,
    ,,,,tropicrita@msn.com,,,Rita_Gardner-Ferry Point tables and chair - Gardner.JPG,Ferry Point Tables,photo,, ,
    ,,,,,,,Rita_Gardner-Forks - Gardner photo.JPG,Forks,photo,,,
    ,,,,,,,,,,,,
    ';
    
    $lines = explode(PHP_EOL, $csvData);
    $array1 = array();
    foreach ($lines as $line) {
        $array1[] = str_getcsv($line);
    }
    
    $stmt = $pdo->prepare("INSERT INTO tbl_person_data (number, check, name, phone, email, entry_fee, print_fee, image_name, description, medium, select, orient, site) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
    
        foreach ($array1 as $row)
        {
            $stmt->execute('$row');
        }
    
    echo '<pre>';
    print_r($array1);
    echo '</pre>';
    
    
    ?>

     

  9. PPS -- Actually, I just now got both errors when I ran it with the conditional.  The fact that it repeated the error three times as  indicated above would indicate that the fatal error didn't occur that time.  When I just ran it, it did.  The errors returned are inconsistent.

    Here are the two errors:

    Warning: Undefined array key "title" in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 71

    (on the conditional line)

    Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php:72 Stack trace: #0 /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php(72): PDOStatement->execute(Array) #1 {main} thrown in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 72

    (on the "execute" line)

  10. I re-typed everything I had copied and pasted having had the same thoughts you had.  It did get rid of an earlier issue but it still fails.  The errors are described below.  

    I am including my form code below as you requested.  I was not sure exactly how to set it up but the way I did gave me a POST array I could work with.

    I get the following error when I run the script with the conditional as I go it from this discussion.  Line 71 is

    if ($title_elements['title'] != '').

    Warning: Undefined array key "title" in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 71

    Warning: Undefined array key "title" in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 71

    Warning: Undefined array key "title" in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 71

    I get this error when I run it without the conditional.  Line 72 is 

    $stmt->execute($title_elements);

    Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php:72 Stack trace: #0 /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php(72): PDOStatement->execute(Array) #1 {main} thrown in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 72

    <!DOCTYPE HTML>
    <html>
    	
    <head>
    
    <title>Image Input Form</title>
     
    <LINK REL=StyleSheet HREF="../css/css-form_new.css" TYPE="text/css" MEDIA=screen>
    
    </head>
    
    <body>
    	
    <div class="container">
      
    <div class="sectionHeading">Submit Images to Taking it Outdoors</div>
          
    <form action="../__classes/upload_images.php" method = "POST" enctype = "multipart/form-data">
    <input type="hidden" id="custId" name="person_id" value="<?php echo $_POST['person_id'];?>">
      
     <!-- Start Artwork -->
    	<h4>Upload up to three files in JPG, JPEG, PNG or TIF(F) format.</h4>
    	<strong>File size:</strong> We recommend at least a <strong>2MB</strong> file and  no larger than a <strong>20MB</strong> file.  Take into account the speed of your internet connection.  Large files can take a long time to uplaod if you have a slow connection.
    	<p><strong>Larger Files:</strong> For very high resolution larger files (<strong>>100MB</strong>) we recommend using <a href="wetransfer.com">wetransfer.com</a>, a free file transfer service for files up to <strong>2 GB</strong>.</p>
    	
    	<div class="fileUpload" >Image 1:</div>
    		<label for="title1"><span>Artwork Title </span><input type="text" class="input-field" name="1['title']" value="" /></label>
    		<label for="medium1"><span>Medium<span class="required">*</span></span><input type="text" class="input-field" name="1['medium']" value="" /></label>
    		<label for="image1"><span>Image File 1</span><input type="file" name="image1" ></label></span></label>
    	
    	<div class="fileUpload" >Image 2:</div>
    		<label for="title2"><span>Artwork Title </span><input type="text" class="input-field" name="2['title']" value="" /></label>
    		<label for="medium2"><span>Medium<span class="required">*</span></span><input type="text" class="input-field" name="2['medium']" value="" /></label>
    		<label for="image2"><span>Image File 2</span><input type="file" name="image2"></label></span>
    		
    	<div class="fileUpload" >Image 3:</div>	
    		<label for="title3"><span>Artwork Title </span><input type="text" class="input-field" name="3['title']" value="" /></label>
    		<label for="medium3"><span>Medium<span class="required">*</span></span><input type="text" class="input-field" name="3['medium']" value="" /></label>
    		<label for="image3"><span>Image File 3</span><input type="file" name="image3"></label></span>
    		
    	</div>
    <!-- End Artwork -->
    	
    	<p><input type = "submit" name="submit"/>
    	<a href="http://localhost/Sites/BannerProject/b-as/_test_site/index.php"><input type = "button" class="blue" name="Cancel" value="Cancel"/></a></p>		
    
    </form>
     
    </div>
          
    </body>
    </html>

     

  11. 1 hour ago, mac_gyver said:

    odd syntax errors are often caused by copy/pasting code from web pages where it has been 'published' and 'beautified'. it contains non-ascii characters that when treated in a php code context breaks the php code. the solution is to delete and re-type the line of code.

    there should be no quotes in the print_r() output around the associative array index names (just tested.) there's something going in your form field name attributes. what is the code/markup for your form fields?

    I have to do something else just now but was thinking some of what you are thinking and appreciate all the thoughts.  I will work on it as soon as I can get back to it, hopefully today.  I am getting good error reporting.  Again, many thanks.

    --Kenoli

  12. There are the same number of elements in the title array as are indicated in the sql statement.  I noticed that when I echo $titles in print_r the added elements have no quotation marks and the ones that arrived via the $_POST file do.  Does that matter?  Here's a printout of $titles just before it is executed:

    Array
    (
        [0] => Array
            (
                ['title'] => qwerwqe
                ['medium'] => Qwerwqer
                [person_id] => 4
                [filename] => _DSC0080.jpg
            )
    
        [1] => Array
            (
                ['title'] => Qwerweqr
                ['medium'] => Qwerwqer
                [person_id] => 4
                [filename] => _DSC0001.jpg
            )
    
        [2] => Array
            (
                ['title'] => weqrweqr
                ['medium'] => Wqerweqr
                [person_id] => 4
                [filename] =>  reportout.jpg
            )
    
    )

     

  13. 5 minutes ago, kenoli said:

    That's the file and that's that's the line number.  I think the problem may be that there should be two "==" on line 66.  What do you think?  I'll try it.

    --Kenoli

    I get the same error with !== .

    When I take the conditional out, I get:

    Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php:75 Stack trace: #0 /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php(75): PDOStatement->execute(Array) #1 {main} thrown in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_images.php on line 75

    --Kenoli

  14. I'm getting the following error:

    Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in /Users/studio/Sites/BannerProject/b-as/_test_site/__classes/upload_files.php on line 64

    Line 64 comes between the following two lines:

        $sql = "INSERT INTO Images (person_id, title, medium, filename) VALUES (:person_id, :title, :medium, :filename)";
        
        $stmt = $pdo->prepare($sql);

    This follows:

    foreach ($titles as $title_elements) {
        if ($title_elements['title'] != '') {
            $stmt->execute($title_elements);
            }
        }

    --Kenoli
     

  15. Brand & McGyver -- I really appreciate the help.  I'm feeling kind of like an idiot getting stuck so much. I don't do much of this and when I did more 15 years ago, felt a lot more adept.  PDO has gotten me confused and I'm distracted by a bunch of other stuff we're doing with a community art project hanging art banners up around town that makes it hard for me to put big blocks of time into the coding.  I'm trying to code a web site to track our banner making and let artists upload their images so we can turn them into banners. Your help is hugely appreciated. A good friend who codes professionally is lambasting me for staying with php.  He does everything with Javascript, both client and server side using vue.js.  PHP just makes a lot more sense to me.

    Thanks for the heads up on error reporting.  Before I logged back in I was going to ask about it.  You mentioned this before and I checked the php.ini file and got this:

    error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT

    I failed however to see further down that this:

    display_errors=On

    Had been turned off.  As you can see, I turned it on.

    Is this all I need turned on regarding error reporting?

    I had the impression I was getting less error reporting than I had expected.  I will reboot and try the script again to see if I get any error information back from PHP.  I have been getting some errors from PDO, largely if there is a connection issue.  I'm not getting it back on other pdo functions.

    Regarding the bind-param issue, I got confused because I was trying to use the bind-param approach and it wasn't working and when I got feedback that all I had to do was send the array with key-value pairs I did that but left the bind-param code in place, not realizing it wasn't necessary.  I think it has screwed me up in a few other places.  I'm now clear about that.

    --Kenoli

  16. I appreciate the help from both of you.  I've integrated your suggestions into my script and include it below.

    It runs but nothing is entered into the table.

    I have been struggling with PDO. and appreciate your comment Mac_gyver.  It helps clear some things up.

    Any idea why the data is not being entered.

    Thanks,

    --Kenoli

    <?php
    	
    //if(isset($_POST['submit'])){
    	
    	echo '<h4>Got to beginning of upload file</h4><br>';
    	
    	require 'Db.php';
    	
    	$person_id = $_POST['person_id'];
    
    	$titles = $_POST;
    	$images = $_FILES;
    	
    	
    	$index = 1;
    	foreach ($images as $image) {
    	    $titles[$index]['person_id'] = $person_id;
    	    $titles[$index]['filename'] = $image['name'];
    	    $index++;
    	}
    	
    	array_shift($titles);
    	array_pop($titles);
    	
    	$sql = "INSERT INTO Images (person_id, title, medium, filename) VALUES (:person_id, :title, :medium, :filename)";
    	
    	$stmt = $pdo->prepare($sql);
    		
    	foreach ($titles as $title_elements) {
        if ($title_elements['title'] != '') {
            $stmt->execute($title_elements);
    	    }
    	}
         
        echo '<h4>Got to end of upload file</h4>';
    		
    //}	
    	
    	
    ?>

     

  17. Here is the actual script.  I removed the first and last elements using array_shift and array_pop as they were not relevant.

    You can see my attempt to use continue to skip an element in $title if it had an empty array element.  It did not work.  The script simply processed every element of $title.

    --Kenoli

    
    $titles {
    
      [1]=>
      array(4) {
        ["title"]=>
        string(8) "qwerqwer"
        ["medium"]=>
        string(10) "dfsadfasda"
        ["person_id"]=>
        int(3)
        ["name"]=>
        string(12) "_DSC0080.jpg"
      }
      [2]=>
      array(4) {
        ["title"]=>
        string(10) "sadfasdfad"
        ["medium"]=>
        string(8) "Asdfsadf"
        ["person_id"]=>
        int(3)
        ["name"]=>
        string(12) "_DSC0030.jpg"
      }
      [3]=>
      array(4) {
        ["title"]=>
        string(8) "asdfsadf"
        ["medium"]=>
        string(8) "Sadfsedf"
        ["person_id"]=>
        int(3)
        ["name"]=>
        string(12) "_DSC0001.jpg"
      }
    }
    
    
    foreach ($title as $title_elements) {
    	
    	if ($title_elements['title'] == '') { continue; }  //Attempt to skip array elements with and empty first element
    		
    	foreach($title_elements as $key => $value) {
    		$param = ':' . $key;
    		$stmt->bindParam($param, $$value);
    		//echo '$' . "stmt->bindParam($param, $$key)<br>";  // Debug
    	}
    	
    	foreach($title_elements as $key => $value) {
    		$insert[$key] =  $value;
    	}
    	
    	/** Execute statement using $insert array. */
    	
    	$stmt->execute($insert);
    	
    	} // End first foreach

     

  18. I'm sorry about the ambiguity.  It was intended as a (lazy) reminder of the elements in the array.  The real issue was how to loop through the $titles array and exclude the array elements that are missing some of their own values.  If the elements were not arrays, I know how to do it.  I have not been able to figure out how to use an element that is missing a value to cause it to skip the entire $title element.  I spent along time trying everything I could think of.

    --Kenoli

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.