PHP Skript Fehlersuche / Hilfe

  • daniel515
    Gefragt am 16. Juni 2024 um 06:35

    Hallo, ich versuche Daten über eine HTTP_Post_page in eine SQL-Datenbank zu übertragen. 

    In einer einfachen Datei ist mir das gelungen. Jetzt habe ich ein etwas komplexeres Projekt. Hier gelingt es mir nicht. Und ich kann den Fehler nicht finden. Kann mir jemand mit dem PHP-Code helfen?


    Wenn ich die Daten in die SQL-Datenbank übertrage, wie bekomme ich die Bilder, die in dem Form hochgeladen wurden, und die Unterschrift, die ich geleistet habe, auf meinen eigenen Server?


    Vielen Dank für Ihre Hilfe :)

  • Mikhail Jotform Support
    Geantwortet am 16. Juni 2024 um 11:50

    Hi Daniel, 

    Thanks for reaching out to Jotform Support. Our German Support agents are busy helping other Jotform users at the moment. I'll try to help you in English using Google Translate, but you can reply in whichever language you feel comfortable using. Or, if you'd rather have support in German, let us know and we can have them do that. But, it might take a while until they're available again.

    Now, coming back to your question, have you checked our guide here on How to Send Submissions to Your MySQL Database Using PHP? If so, can you take a screenshot of what’s happening and send it to us? This guide will show you how to do that. If you are looking for the POST data for the File Upload field, you can enable the Send Post Data option on your form:

    1. In Form Builder, click on Settings in the orange navigation bar at the top.
    2. Click on the Thank You Page on the left side.
    3. Click on Redirect to an external link after submission.
    4. Add your destination URL in URL field.
    5. Then toggle On the Redirect with HTTP POST. PHP Skript Fehlersuche / Hilfe Image 1 Screenshot 20

    Give it a try and let us know if you have any other questions.

  • DANIEL
    Geantwortet am 16. Juni 2024 um 15:47


    Hey Mikhail, 

    Thank you very much for your message and your support.

    Yes, I have tried to follow this guide. As I said, it works for a simple application. When I made it a bit more complex, the challenges started.

    Here is the script I used. It should correspond to what you see on the guide ...

    Thanks for the help

    Bests, Daniel



    MY SCRIPT:



    <?php

    /***

    # How to Send Submissions to Your MySQL Database Using PHP

    MySQL PHP Save Example (v2.0)

    Jotform Inc. 2022 - AP#0031


    This script was built for the following sample form: https://www.jotform.com/222744188444461

    For more information, see: https://www.jotform.com/help/?p=608327

    ***/




    /***

    Display the data keys and values for debugging purposes.

    ***/

    echo '<pre>', print_r($_POST, 1) , '</pre>';




    /***

    Test the data if it's a valid submission by checking the submission ID.

    ***/

    if (!isset($_POST['submission_id'])) {

    die("Invalid submission data!");

    }




    /***

    ## Database Config


    NOTE: 

    Replace the values below with your MYSQL database environment variables 

    to create a valid connection.

    ***/

    $db_host = "xyz.io";

    $db_username = "xyz";

    $db_password = 'xyz';

    $db_name = "xyz";

    $db_table = "xyz";




    /***

    Connect to database.

    ***/

    $mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);

    if ($mysqli->connect_error) {

    die("Connection failed: " . $mysqli->connect_error);

    }




    /***

    ## Data to Save


    Prepare the data to prevent possible SQL injection vulnerabilities to the database.


    NOTE: Add the POST data to save in your database.

    To view the submission as POST data, see: https://www.jotform.com/help/?p=607527

    ***/

    $datum_abholung = $mysqli->real_escape_string(implode(" ", $_POST['datum_abholung']));

    $adresse_bergung = $mysqli->real_escape_string(implode(" ", $_POST['adresse_bergung']));

    $name_verstorbener = $mysqli->real_escape_string(implode(" ", $_POST['name_verstorbener']));

    $geburtsdatum_vers = $mysqli->real_escape_string(implode(" ", $_POST['geburtsdatum_vers']));

    $verbringungsort = $mysqli->real_escape_string(implode(" ", $_POST['verbringungsort']));

    $wertgegenstaende_uebergeben = $mysqli->real_escape_string(implode(" ", $_POST['wertgegenstaende_uebergeben']));

    $name_beamter = $mysqli->real_escape_string(implode(" ", $_POST['name_bemater']));

    $revier_schicht = $mysqli->real_escape_string(implode(" ", $_POST['revier_schicht']));

    $unterschriftbeamterin = $mysqli->real_escape_string(implode(" ", $_POST['unterschriftbeamterin']));

    $schwierigebergung = $mysqli->real_escape_string(implode(" ", $_POST['schwierigebergung']));

    $schw_bergung = $mysqli->real_escape_string(implode(" ", $_POST['schw_bergung']));

    $schreibensie18 = $mysqli->real_escape_string(implode(" ", $_POST['schreibensie18']));

    $schreibensie19 = $mysqli->real_escape_string(implode(" ", $_POST['schreibensie19']));

    $schreibensie21 = $mysqli->real_escape_string(implode(" ", $_POST['schreibensie21']));

    $schreibensie22 = $mysqli->real_escape_string(implode(" ", $_POST['schreibensie22']));

    $schreibensie20 = $mysqli->real_escape_string(implode(" ", $_POST['schreibensie20']));




    /***

    Prepare the test to check if the submission already exists in your database.

    ***/

    $sid = $mysqli->real_escape_string($_POST['submission_id']);

    $result = $mysqli->query("SELECT * FROM $db_table WHERE submission_id = '$sid'");




    /***

    ## Queries to Run


    Perform the test and then UPDATE or INSERT the record

    depending if the submission is already in the database or not.


    NOTE:

    Edit the queries below according to your form and database table structure.

    For more information, see:

    -

    -

    ***/

    if ($result->num_rows > 0) {

    /* UPDATE query */

    $result = $mysqli->query("UPDATE $db_table 

    SET datum_abholung = '$datum_abholung',

    adresse_bergung = '$adresse_bergung',

    name_verstorbener = '$name_verstorbener',

    geburtsdatum_vers = '$geburtsdatum_vers',

    verbringungsort = '$verbringungsort',

    wertgegenstaende_uebergeben = '$wertgegenstaende_uebergeben',

    name_beamter = '$name_beamter',

    reviers_schicht = '$revier_schicht',

    unterschriftbeamterin = '$unterschriftbeamterin',

    schwierigebergung = '$schwierigebergung',

    schw_bergung = '$schw_bergung',

    schreibensie18 = '$schreibensie18',

    schreibensie19 = '$schreibensie19',

    schreibensie21 = '$schreibensie21',

    schreibensie22 = '$schreibensie22',

    schreibensie20 = '$schreibensie20',

     

    WHERE submission_id = '$sid'

    ");

    }

    else {

    /* INSERT query */

    $result = $mysqli->query("INSERT IGNORE INTO $db_table (

    submission_id, 

    datum_abholung,

    adresse_bergung,

    name_verstorbener,

    geburtsdatum_vers,

    verbringungsort,

    wertgegenstaende_uebergeben,

    name_beamter,

    reviers_schicht,

    unterschriftbeamterin,

    schwierigebergung,

    schw_bergung,

    schreibensie18,

    schreibensie19,

    schreibensie21,

    schreibensie22,

    schreibensie20,

    ) VALUES (

    '$sid', 

    '$datum_abholung',

    '$adresse_bergung',

    '$name_verstorbener',

    '$geburtsdatum_vers',

    '$verbringungsort',

    '$wertgegenstaende_uebergeben',

    '$name_beamter',

    '$revier_schicht',

    '$unterschriftbeamterin',

    '$schwierigebergung',

    '$schw_bergung',

    '$schreibensie18',

    '$schreibensie19',

    '$schreibensie21',

    '$schreibensie22',

    '$schreibensie20',

    )

    ");

    }




    /***

    Display the outcome.

    ***/

    if ($result === true) {

    echo "Success!";

    }

    else {

    echo "SQL error:" . $mysqli->error;

    }




    /***

    Close the connection.

    ***/

    $mysqli->close();














  • Sigit Jotform Support
    Geantwortet am 16. Juni 2024 um 18:21

    Hi Daniel, 

    Thanks for getting back to us. Allow me some time to look into this. I'll get back to once I have an update.

  • Sigit Jotform Support
    Geantwortet am 16. Juni 2024 um 21:56

    Hi Daniel, 

    Thank you for your patience. I have received an update from the team regarding your PHP script as follows:

    1. The field `unterschriftbeamterin` in the code, it doesn't exist in the submission data

    2. The use of `implode` to all variables, causing errors for any variable that isn't an array.

    Here's the updated variables, and here, the unknown variable are kept as a comment just in case

    $datum_abholung = $mysqli->real_escape_string(implode(" ", $_POST['datum_abholung']));
    $adresse_bergung = $mysqli->real_escape_string(implode(" ", $_POST['adresse_bergung']));
    $name_verstorbener = $mysqli->real_escape_string(implode(" ", $_POST['name_verstorbener']));
    $geburtsdatum_vers = $mysqli->real_escape_string(implode(" ", $_POST['geburtsdatum_vers']));
    $verbringungsort = $mysqli->real_escape_string($_POST['verbringungsort']);
    $wertgegenstaende_uebergeben = $mysqli->real_escape_string($_POST['wertgegenstaende_uebergeben']);
    $name_beamter = $mysqli->real_escape_string(implode(" ", $_POST['name_beamter']));
    $revier_schicht = $mysqli->real_escape_string($_POST['revier_schicht']);
    // $unterschriftbeamterin = $mysqli->real_escape_string(implode(" ", $_POST['unterschriftbeamterin']));
    $schwierigebergung = $mysqli->real_escape_string($_POST['schwierigebergung']);
    $schw_bergung = $mysqli->real_escape_string($_POST['schw_bergung']);
    $schreibensie18 = $mysqli->real_escape_string($_POST['schreibensie18']);
    $schreibensie19 = $mysqli->real_escape_string($_POST['schreibensie19']);
    $schreibensie21 = $mysqli->real_escape_string($_POST['schreibensie21']);
    $schreibensie22 = $mysqli->real_escape_string($_POST['schreibensie22']);
    $schreibensie20 = $mysqli->real_escape_string($_POST['schreibensie20']);

    3. There are extra commas causing errors in the SQL statements

    The original code looks like this:

    $result = $mysqli->query("INSERT INTO $db_table (var1,var2,var3,) VALUES ($var1,$var2,$var3,)");

    You'll need to remove the extra commas like this:

    $result = $mysqli->query("INSERT INTO $db_table (var1,var2,var3) VALUES ($var1,$var2,$var3)");


    Also, our team noticed that there's no error handling for empty fields. They made the necessary changes and successfully tested it in their local environment. However, if any field is empty, it generates an error like this: Warning: Undefined array key "schreibensie18" in C:\xampp\htdocs\15968843.php on line 40.

    That's it, give it a try and let us know if there's anything else we can do for you.

  • daniel515
    Geantwortet am 17. Juni 2024 um 06:01


    Thank you very much for your support. I was able to realise the first part. I still get an error message. I can't make the changes to point 3 because I can't find them in my code. Where in the code can I find this?

    Thank you for your support!


    => Your code snippets

    3. There are extra commas causing errors in the SQL statements

    The original code looks like this:

    $result $mysqli->query("INSERT INTO $db_table (var1,var2,var3,) VALUES ($var1,$var2,$var3,)");

    You'll need to remove the extra commas like this:

    $result $mysqli->query("INSERT INTO $db_table (var1,var2,var3) VALUES ($var1,$var2,$var3)");






  • Mikhail Jotform Support
    Geantwortet am 17. Juni 2024 um 06:34

    Hi Daniel,

    It's on your last part of your sent code. You may want to check the extra commas on your SQL statements, like the code below:

    PHP Skript Fehlersuche / Hilfe Image 1 Screenshot 40

    PHP Skript Fehlersuche / Hilfe Image 2 Screenshot 51

    PHP Skript Fehlersuche / Hilfe Image 3 Screenshot 62

    Give it a try and let us know if you have any other questions.

  • daniel515
    Geantwortet am 17. Juni 2024 um 12:42

    Fantastic. It worked. Many thanks for your help!


    Now I have one last challenge. How do I automatically get the signature and photos uploaded in the form onto my server, which are then linked to the data record in the SQL database?


    Can you also help me with t

    his?
  • Angelica Jotform Support
    Geantwortet am 17. Juni 2024 um 15:13

    Hi Daniel,

    As for your other question, I've moved that to a new thread. You can check that out here.

    Let us know if you need any more help.


 
Ihre Antwort