-4

I'm trying to make a page where users can add songs to my database. I have some problems with it. When I try it out, the ID, Album Titel and Artiest are added to my database properly, but I only get no value for Duur and 0.00 for Prijs, which is not wat I tried to add.

My code is below:

<form method="post">
<input type="int(5)" name="ID"> ID <br>
<input type="text" name="Titel">Titel<br>
<input type="text" name="Artiest">Artiest<br>
<input type="varchar(6)" name"Duur">Duur<br>
<input type="decimal(5,2)" name"Prijs">Prijs<br>

<input type="submit">
</form>

<?php

$inputid = $_POST['ID'];
$users_name = $_POST['Titel'];
  $users_email = $_POST['Artiest'];
  $users_website = $_POST['Duur'];
  $users_comment = $_POST['Prijs'];





  $query = "
  INSERT INTO `Album`(`ID`, `Album Titel`, `Artiest`, `Duur`, `Prijs`) VALUES ('$inputid', '$users_name',
        '$users_email', '$users_website', '$users_comment');";

  $result = $conn->query($query);;


  echo "<h2>Thank you for your Comment!</h2>";

  mysql_close($con);

?>

The database is linked properly, so that is not the problem. I have the exact same input types a I have in my database structure.

Can somebody help me out? Thanks in advance!

Bram
  • 13
  • 2
  • 5
  • varchar and decimal and int are not input types these are database tables coloum type – Vivek Singh Jun 10 '16 at 07:54
  • @Vicky Okay thanks, do you know where I should change them to in the html form? – Bram Jun 10 '16 at 07:56
  • 1
  • 2
    You should avoid learning or writing new code using PHP's `mysql_*` functions. They have been removed in the latest version and your code won't work in the future. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines Jun 10 '16 at 08:53

2 Answers2

1

List of all available input types: http://www.w3schools.com/html/html_form_input_types.asp

   <form method="post">

    <!-- Add required attribute if you don't want user to leave it blank  -->

    ID: <input type="text" name="id" required /> <br>
    Title: <input type="text" name="title" required /><br>
    Artist<input type="text" name="artist" required /><br>

    <!-- allow user to enter float values between your choice -->
    Duration: <input type="number" min="0" max="10" step="0.01" name="duration" required /><br>


    Price: <input type="number" min="0" max="999.00" step="0.01" required name="price" /><br>

    <input type="submit">
</form>


    <?php

    $id = $_POST['id'];
    $title = $_POST['title'];
    $artist = $_POST['artist'];
    $duration = $_POST['duration'];
    $price = $_POST['price'];

    $query = "INSERT INTO `Album`(`ID`, `Album Titel`, `Artiest`, `Duur`, `Prijs`) VALUES ('$id', '$title',
            '$artist', '$duration', '$price');";

      $result = $conn->query($query);;


      echo "<h2>Thank you for your Comment!</h2>";

      mysql_close($con);

Update: You forgot to put '=' sign in names of last two input fields, thats why they were not getting POSTED.

I have edited my original answer.

Here's a edited version of you code i have tried :

<form method="post">

        <!-- Add required attribute if you don't want user to leave it blank  -->

        ID: <input type="text" name="id" required /> <br>
        Title: <input type="text" name="title" required /><br>
        Artist<input type="text" name="artist" required /><br>

        <!-- allow user to enter max 6 characters -->
        Duration: <input type="number" min="0" max="10" step="0.01" name="duration" required /><br>

        <!-- allow user to enter float values between your choice -->
        Price: <input type="number" min="0" max="999.00" step="0.01" required name="price" /><br>

        <input type="submit">
    </form>

    <?php
      print_r( $_POST );


    ?>

Which gives me this output enter image description here

If you are new to programming, then please use some appropriate variable names so that others can understand your code better.

Hope this solves your issue.

Priyesh Kumar
  • 2,837
  • 1
  • 15
  • 29
  • Firstly, thanks for your help. I feel like it is close. But unfortunately I still get the same problem. There is no value for Duur and the value is always 0.00 voor Prijs, whatever I enter. To clarify it a little. Duur means duration, so I want the length of an album in there with for example 33:45 which means 33 minutes and 45 seconds. Prijs means price and I want a decimal value over there with 2 decimals, for example 7.99 – Bram Jun 10 '16 at 18:07
0

HTML has only following input types http://www.w3schools.com/htmL/html_form_input_types.asp. You have to maintain strict data types only in database not in HTML. For HTML use one of the above enlisted input types.

<input type="number" name="ID"> ID <br>
<input type="text" name="Titel">Titel<br>
<input type="text" name="Artiest">Artiest<br>
<input type="number" name"Duur">Duur<br>
<input type="number" name"Prijs">Prijs<br>
Deepak Adhikari
  • 419
  • 2
  • 4