Hi guys... I have this page I'm trying to get to work, but no matter what I do it doesn't want to insert a new post into the database. Help? Please?

<?php

//ansluter till databasen
$opendb=mysql_connect("****", "*****", "****") or die(mysql_error());
mysql_select_db("*****") or die(mysql_error());

//skriver information till den angivna tabellen om den är uppdaterad
    if(isset($_POST['submit'])){
    $nypost = mysql_query("INSERT INTO arkentider (typ) VALUES('$_POST[typ]')")

    or die("Kunde inte lägga till ny räkning:<br />".mysql_error());

    }

//Hämtar intervall att lista
    if(isset($_POST['Sok'])){
    $Start=$_POST['Startdatum'];
    $Slut=$_POST['Slutdatum'];
    $today = date("Y-m-d");
    $sql = "SELECT * FROM arkentider WHERE DATE(tid) >= '".$Start."' AND DATE(tid) <= '".$Slut."' ORDER BY tid DESC";
    $result = mysql_query($sql) or die(mysql_error());
    }
    else {
    $sql = "SELECT * FROM arkentider ORDER BY tid DESC";
    $result = mysql_query($sql) or die(mysql_error());
    }

//Lägger till post
    echo "<form action='".$PHP_SELF."' method='post'>";
    echo "<select name='typ'>";
    echo "<option selected>Gått på lunch</option>";
    echo "<option>Kommit tillbaka från lunch</option>";
    echo "<option>Kompat ut</option>";
    echo "</select>";
    echo "<input type='submit' value='Registrera'><input type='Reset' value='Rensa'>";
    echo "</form>";

//Väljer intervall att lista
    echo "<form action='".$PHP_SELF."' method='post'>";
    echo "Startdatum: <input type='date' name='Startdatum' value='".$Start."'>&nbsp;Slutdatum: <input type='date' name='Slutdatum' value='".$Slut."'>&nbsp;<input type='submit' value='Sök' name='Sok'>";
    echo "</form><BR><BR>";


//Listar poster i det angivna intervallet
    echo "<style type='text/css'>tr:hover { background:#3fc9fc; }</style>";
    echo "<table border='1' bordercolor='#3fc9fc' cellspacing='0' cellpadding='3' width='90%'>";
    echo "<tr bgcolor='#3fc9fc'>";
    echo "<td align=center valign=center  style='white-space:nowrap'>";
    echo "Tid";
    echo "</td>";
    echo "<td align=center valign=center  style='white-space:nowrap'>";
    echo "Vad";
    echo "</td>";
    echo "</tr>";

    while($row = mysql_fetch_array( $result ))
    {
    echo "<tr>";
    echo "<td  style='white-space:nowrap'><FONT SIZE=2>";
    echo $row['tid'];
    echo "</FONT></td>";
    echo "<td  style='white-space:nowrap'><FONT SIZE=2>";
    echo $row['text'];
    echo "</FONT></td>";
    echo "</tr>";
    }

    echo "</table>";

//stänger databasen
    mysql_close($opendb);

?>

Is it the double forms that mess me up or what?

Try to cover $_POST in {} as shown below

if(isset($_POST['submit'])){    
    $nypost = mysql_query("INSERT INTO arkentider (typ) VALUES('{$_POST[typ]}')")
    or die("Kunde inte lägga till ny räkning:<br />".mysql_error());
    }

It won't do anything as you have assigned the query to a variable that you are doing anything with so vchange it to

if(isset($_POST['submit'])){
    mysql_query("INSERT INTO arkentider (typ) VALUES('$_POST[typ]')")
    or die("Kunde inte lägga till ny räkning:<br />".mysql_error());
    }
// OR

if(isset($_POST['submit'])){
    $nypost = "INSERT INTO arkentider (typ) VALUES('$_POST[typ]')"
    or die("Kunde inte lägga till ny räkning:<br />".mysql_error());
    mysql_query($nypost);
    }

Lol! It's easy to miss a small, but significant, thing! Thanks!

Hmm... I did the change, but it still doesn't insert anything in the table...

Echo out your query (echo $nypost;) and make sure that the actual query being run looks correct and if so then run it in phpmyadmin and see if you get any errors there.

You have more than one form on the same page, are you clicking the right submit button? None of them are named submit... so $_POST{'submit'] is always false.

Ha! I solved it... By moving the query into a separate php-file! :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.