joostmakaay
joostmakaay

Reputation: 437

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);

Upvotes: 0

Views: 221

Answers (3)

Andy
Andy

Reputation: 50540

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.

Upvotes: 2

Awlad Liton
Awlad Liton

Reputation: 9351

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);

Upvotes: 0

Philipp
Philipp

Reputation: 15629

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!

Upvotes: 0

Related Questions