简体   繁体   中英

PHP not inserting into MySQL db

I'm to post to a table in my localhosted MySQL database, but when i run the script the page stays white but there is nothing posted in the table. I'll add my code for clarification. Im using USBWebserver to host the apache and MySQL server on.

    <?php
//connectie leggen met input data
//$con = 

//variablen input invullen (bijv $gebruikeragenda = $_POST['gebruikeragenda'];
$gebruikeragenda = 'Testafspraak'; //hierin specifieren op welke account dit evenement geplaatst moet worden
$tegebruikenagenda = ''; //hierin specifieren we de agenda waarin het evenement geplaatst moet worden
$begintijd = '2014-01-13 15:30:00';         //begintijd specifieren
$eindtijd = '2014-01-13 16:30:00';          //eindtijd specifieren
$medeaanwezige = '';        //emailadressen van andere aanwezige tijdens dit evenement

//connectie leggen met MySQL(aanpasbaarnaar SQLite indien nodig)
$con=mysqli_connect("localhost:3307","root","usbw","baikal");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//input van hierboven ombouwen en in de database van Baikal bouwen
$calendardata = $gebruikeragenda;                       
$uri = 'default';
$lastmodified = $begintijd;     //laatst gemodificeerd
$calenderid = '1';              //het id van de agenda waarbij de event hoort
$etag = 'niks';
$size = '200';                      //grootte   
$componenttype = '';            //wordt er gebruik gemaakt van een notificatie, voer hier dan VTODO in
$firstoccurence = $begintijd;   //begintijd van evenement
$lastoccurence = $eindtijd;     //eindtijd van evenement

//gegevens in de database plaatsen
mysqli_query($con,"INSERT INTO calendarobjects (calendardata, uri, calendarid, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence) VALUES (".$calendardata.", ".$uri.", 
".$lastmodified.",".$etag.",".$size.",".$componenttype.",".$firstoccurence.",".$lastoccurence.")");
mysqli_close($con);

Your columns and values do not match. You are attempting to insert 8 values into 9 columns.

Specifically, it appears you are missing a calendarid .

Second, your strings need to be wrapped in quotes.

Finally, you are vulnerable to SQL injections. Consider using prepared statements.

You have to wrap your string's in single or double quotes and keep sure, the number of params match the number of values..( calendarid is missing..)

mysqli_query($con,"INSERT INTO calendarobjects
    (calendardata, uri, calendarid, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence)
  VALUES
    ('$calendardata', '$uri', 'missing calendarid', '$lastmodified', '$etag','$size','$componenttype','$firstoccurence','$lastoccurence')");
mysqli_close($con);

Btw.. your code is vulnerable to SQL injections, so you have to use real_escape_string (link) or better prepared statements !

You need to use quotation for values.

You have 9 attributes and 8 values. You have missed one value in your query.Set another value in your query and Try this:

 <?php
    //connectie leggen met input data
    //$con = 

    //variablen input invullen (bijv $gebruikeragenda = $_POST['gebruikeragenda'];
    $gebruikeragenda = 'Testafspraak'; //hierin specifieren op welke account dit evenement geplaatst moet worden
    $tegebruikenagenda = ''; //hierin specifieren we de agenda waarin het evenement geplaatst moet worden
    $begintijd = '2014-01-13 15:30:00';         //begintijd specifieren
    $eindtijd = '2014-01-13 16:30:00';          //eindtijd specifieren
    $medeaanwezige = '';        //emailadressen van andere aanwezige tijdens dit evenement

    //connectie leggen met MySQL(aanpasbaarnaar SQLite indien nodig)
    $con=mysqli_connect("localhost:3307","root","usbw","baikal");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //input van hierboven ombouwen en in de database van Baikal bouwen
    $calendardata = $gebruikeragenda;
    $uri = 'default';
    $lastmodified = $begintijd;     //laatst gemodificeerd
    $calenderid = '1';              //het id van de agenda waarbij de event hoort
    $etag = 'niks';
    $size = '200';                      //grootte   
    $componenttype = '';            //wordt er gebruik gemaakt van een notificatie, voer hier dan VTODO in
    $firstoccurence = $begintijd;   //begintijd van evenement
    $lastoccurence = $eindtijd;     //eindtijd van evenement
    $sql = "INSERT INTO calendarobjects (calendardata, uri, calendarid, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence) VALUES ('$calendardata', '$uri',
    '$lastmodified','$etag','$size','$componenttype','$firstoccurence','$lastoccurence')";
    //gegevens in de database plaatsen
   if( mysqli_query($con,$sql)){
    echo "successully inserted";  
   }else{
    echo "something wrong";
   }
    mysqli_close($con);

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