简体   繁体   中英

connect php to mysql database

I'm trying to connect an html form to a mysql database through php. This is my code for the php script:

include_once('db_connect.php');

if(isset($_REQUEST['submit'])) {


$lokotitle=$_POST['lokotitle'];
$description=$_POST['description'];
$category=$_POST['category'];
$showyourname=$_POST['showyourname'];
$yourname=$_POST['yourname'];
$lat=$_POST['lat'];
$lng=$_POST['lng'];


// Will add form validation here 


if ($errorMessage != "" ) {
    echo "<p class='message'>" .$errorMessage. "</p>" ;
}

else{
    //Inserting record in table using INSERT query
    $insqDbtb="INSERT INTO `new_loko`.`web_form`
    (`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
    mysqli_query($link,$insqDbtb) or die(mysqli_error($link));


    ?>
    <script type="text/javascript">window.location = "submit_success.php";</script> 
    <?php


    }
}

My include_once('db_connect.php') is properly working, but the php simply runs this part, checks that the connection is working, which it is, and then stops without actually uploading the data to the database. I can't figure out what's wrong with the code. Also, I had this working earlier and am not sure why it no longer is.

Thanks in advance for the much needed help.

declare your $errorMessage with empty then change value as per validations.

   <?php

    include_once('db_connect.php');

    if(isset($_REQUEST['submit'])) {


    $lokotitle=$_POST['lokotitle'];
    $description=$_POST['description'];
    $category=$_POST['category'];
    $showyourname=$_POST['showyourname'];
    $yourname=$_POST['yourname'];
    $lat=$_POST['lat'];
    $lng=$_POST['lng'];

    $errorMessage="";
    /* some validation

    if(validation)
      $errorMessage="some message";
     -------------------
     -------------------
     -------------------
     // Will add form validation here
     */

    if ($errorMessage != "" ) {
        echo "<p class='message'>" .$errorMessage. "</p>" ;
    }

    else{
        //Inserting record in table using INSERT query
        $insqDbtb="INSERT INTO `new_loko`.`web_form`
        (`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
        mysqli_query($link,$insqDbtb) or die(mysqli_error($link));


        ?>
        <script type="text/javascript">window.location = "submit_success.php";</script>
        <?php


        }
    }

    ?>

It looks like you may have some extra `s in your call database.table call

yours

 $insqDbtb="INSERT INTO `new_loko`.`web_form`
 (`lokotitle`, `description`, `category`, `showyourname`, `yourname`,             `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname',     '$yourname', '$lat', '$lng')";
      mysqli_query($link,$insqDbtb) or die(mysqli_error($link));

try this:

 $insqDbtb="INSERT INTO `new_loko.web_form`
    (`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
    mysqli_query($link,$insqDbtb) or die(mysqli_error($link));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM